-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathColor.carp
More file actions
81 lines (75 loc) · 1.7 KB
/
Color.carp
File metadata and controls
81 lines (75 loc) · 1.7 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
(doc Color "provides ANSI color operations.")
(defmodule Color
(hidden table)
(deftype Id
Black
Red
Green
Yellow
Blue
Magenta
Cyan
White
Reset
None
Bold
Italic
Underline
BlinkSlow
BlinkRapid
BgBlack
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite)
(use Id)
(defn hash [k]
(get-tag k))
(implements hash Color.hash)
(defn = [a b]
(= (hash (the (Ref Color.Id) a)) (hash b)))
(implements = Color.=)
(def table
{(Black) @"30"
(Red) @"31"
(Green) @"32"
(Yellow) @"33"
(Blue) @"34"
(Magenta) @"35"
(Cyan) @"36"
(White) @"37"
(Reset) @"0"
(None) @"0"
(Bold) @"1"
(Italic) @"3"
(Underline) @"4"
(BlinkSlow) @"5"
(BlinkRapid) @"6"
(BgBlack) @"40"
(BgRed) @"41"
(BgGreen) @"42"
(BgYellow) @"43"
(BgBlue) @"44"
(BgMagenta) @"45"
(BgCyan) @"46"
(BgWhite) @"47"})
(doc color "generates ANSI coloration based on a color name `cname`.")
(defn color [cid]
(let [n (Map.get &table &cid)]
(String.append "\x1b[" &(String.append &n "m"))))
(doc colorize "wraps a string `s` in ANSI coloration based on a color id `cid` and prints it.
It will reset the color afterwards.")
(defn colorize [cid s]
(String.append &(color cid) &(String.append s &(color (Reset)))))
)
(defmodule IO
(doc color "sets the output color using ANSI coloration based on a color id `cid`.")
(defn color [cid]
(print &(Color.color cid)))
(doc colorize "wraps a string in ANSI coloration based on a color id `cid` and prints it.")
(defn colorize [cid s]
(print &(Color.colorize cid s)))
)