diff --git a/Colorized.lean b/Colorized.lean index 0ee84f9..bcc1f05 100644 --- a/Colorized.lean +++ b/Colorized.lean @@ -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. -/ @@ -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 diff --git a/Example.lean b/Example.lean index 09024c1..92dc5f2 100644 --- a/Example.lean +++ b/Example.lean @@ -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!")