-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathColorized.lean
More file actions
82 lines (70 loc) · 2.09 KB
/
Colorized.lean
File metadata and controls
82 lines (70 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
namespace Colorized
/-! 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. -/
/-- The `Section` type represents whether the color applies to the foreground or background. -/
inductive Section
| Foreground
| Background
/-- The `Color` type represents different color options available for text. -/
inductive Color
| Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White
| Default
instance : Repr Color where
reprPrec
| Color.Black, _ => "0"
| Color.Red, _ => "1"
| Color.Green, _ => "2"
| Color.Yellow, _ => "3"
| Color.Blue, _ => "4"
| Color.Magenta, _ => "5"
| Color.Cyan, _ => "6"
| Color.White, _ => "7"
| Color.Default, _ => "9"
/-- The `Style` type represents different text styles that can be applied. -/
inductive Style
| Normal
| Bold
| Faint
| Italic
| Underline
| SlowBlink
| ColoredNormal
| Reverse
instance : Repr Style where
reprPrec
| Style.Normal, _ => "0"
| Style.Bold, _ => "1"
| Style.Faint, _ => "2"
| Style.Italic, _ => "3"
| Style.Underline, _ => "4"
| Style.SlowBlink, _ => "5"
| Style.ColoredNormal, _ => "6"
| Style.Reverse, _ => "7"
/-- The `Colorized` class defines an interface for colorizing and styling text. It provides methods
for applying color to the foreground or background, and for applying different text styles.
-/
class Colorized (α : Type) where
colorize : Section → Color → α → α
style : Style → α → α
bgColor := colorize Section.Background
color := colorize Section.Foreground
/-- 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}"
style sty str :=
s!"{const}{repr sty}m{str}{reset}"