Carp/core/Color.carp
Jorge Acereda df72627974 Fix doc
2019-09-11 20:24:09 +02:00

81 lines
1.7 KiB
Plaintext

(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 to-int [k]
(to-int @(the (Ref Char) (Unsafe.coerce (the (Ref Id) &k)))))
(defn hash [k]
(to-int (the Id @k)))
(defn = [a b]
(= (to-int (the Id @a)) (to-int (the Id @b))))
(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)))
)