Carp/core/Color.carp

81 lines
1.7 KiB
Plaintext
Raw Normal View History

2018-08-06 18:19:39 +03:00
(defmodule Color
(hidden table)
2019-06-20 10:03:03 +03:00
(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 to-int [k]
(to-int @(the (Ref Char) (Unsafe.coerce (the (Ref Id) &k)))))
(defn hash [k]
2019-06-21 01:29:01 +03:00
(to-int (the Id @k)))
2019-06-20 10:03:03 +03:00
(defn = [a b]
2019-06-21 01:29:01 +03:00
(= (to-int (the Id @a)) (to-int (the Id @b))))
2019-06-20 10:03:03 +03:00
2018-08-06 18:19:39 +03:00
(def table
2019-06-20 10:03:03 +03:00
{(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"})
2018-08-06 18:19:39 +03:00
(doc color "generates ANSI coloration based on a color name `cname`.")
2019-06-20 10:03:03 +03:00
(defn color [cid]
(let [n (Map.get &table &cid)]
(String.append "\x1b[" &(String.append &n "m"))))
2018-08-06 18:19:39 +03:00
2019-06-20 10:03:03 +03:00
(doc colorize "wraps a string `s` in ANSI coloration based on a color id `cid` and prints it.
It will reset the color afterwards.")
2019-06-20 10:03:03 +03:00
(defn colorize [cid s]
(String.append &(color cid) &(String.append s &(color (Reset)))))
2018-08-06 18:19:39 +03:00
)
(defmodule IO
2019-06-20 10:03:03 +03:00
(doc color "sets the output color using ANSI coloration based on a color id `cid`.")
(defn color [cid]
(print &(Color.color cid)))
2018-08-06 18:19:39 +03:00
2019-06-20 10:03:03 +03:00
(doc colorize "wraps a string in ANSI coloration based on a color name `cid` and prints it.")
(defn colorize [cid s]
(print &(Color.colorize cid s)))
2018-08-06 18:19:39 +03:00
)