Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions Colorized.lean
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module

namespace Colorized

public section

/-! Colorized library for adding color and style to text output. This library provides functionality
to change the foreground and background colors, as well as to apply various text styles. -/

Expand Down Expand Up @@ -64,19 +68,28 @@ class Colorized (α : Type) where
bgColor := colorize Section.Background
color := colorize Section.Foreground

end

/-- Constant string representing the beginning of an ANSI escape sequence. -/
private def const := "\x1b["

/-- Constant string for resetting text formatting. -/
private def reset := "\x1b[0m"

instance : Colorized String where
colorize sec col str :=
let sectionNum :=
match sec with
| Section.Foreground => "9"
| Section.Background => "4"
s!"{const}{sectionNum}{repr col}m{str}{reset}"
/-- Applies a section/color to a string. -/
@[inline]
public def colorizeString (sec : Section) (col : Color) (str : String) : String :=
let secNum :=
match sec with
| .Foreground => "9"
| .Background => "4"
s!"{const}{secNum}{repr col}m{str}{reset}"

/-- Applies a style to a string. -/
@[inline]
public def stylizeString (sty : Style) (str : String) : String :=
s!"{const}{repr sty}m{str}{reset}"

style sty str :=
s!"{const}{repr sty}m{str}{reset}"
public instance : Colorized String where
colorize := Colorized.colorizeString
style := Colorized.stylizeString
4 changes: 3 additions & 1 deletion Example.lean
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module

import «Colorized»
open Colorized

def main : IO Unit := do
public def main : IO Unit := do
-- | Simple style
IO.println (Colorized.style Style.Underline "Hello, world!")

Expand Down