mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
107 lines
2.3 KiB
Plaintext
107 lines
2.3 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 @k))
|
|
(defn = [a b]
|
|
(= (to-int @a) (to-int @b)))
|
|
(defn str [cid]
|
|
(match cid
|
|
Black @"Black"
|
|
Red @"Red"
|
|
Green @"Green"
|
|
Yellow @"Yellow"
|
|
Blue @"Blue"
|
|
Magenta @"Magenta"
|
|
Cyan @"Cyan"
|
|
White @"White"
|
|
Reset @"Reset"
|
|
None @"None"
|
|
Bold @"Bold"
|
|
Italic @"Italic"
|
|
Underline @"Underline"
|
|
BlinkSlow @"BlinkSlow"
|
|
BlinkRapid @"BlinkRapid"
|
|
BgBlack @"BgBlack"
|
|
BgRed @"BgRed"
|
|
BgGreen @"BgGreen"
|
|
BgYellow @"BgYellow"
|
|
BgBlue @"BgBlue"
|
|
BgMagenta @"BgMagenta"
|
|
BgCyan @"BgCyan"
|
|
BgWhite @"BgWhite"))
|
|
|
|
|
|
(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 name `cid` and prints it.")
|
|
(defn colorize [cid s]
|
|
(print &(Color.colorize cid s)))
|
|
)
|