aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/color/terminal.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/library/lux/data/color/terminal.lux')
-rw-r--r--stdlib/source/library/lux/data/color/terminal.lux103
1 files changed, 103 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/data/color/terminal.lux b/stdlib/source/library/lux/data/color/terminal.lux
new file mode 100644
index 000000000..1f368de0b
--- /dev/null
+++ b/stdlib/source/library/lux/data/color/terminal.lux
@@ -0,0 +1,103 @@
+... https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
+(.require
+ [library
+ [lux (.except with)
+ [abstract
+ [equivalence (.only Equivalence)]]
+ [data
+ ["[0]" text (.use "[1]#[0]" equivalence)
+ ["[0]" char]
+ ["%" \\format]]]
+ [math
+ [number
+ ["n" nat]]]
+ [meta
+ [macro
+ ["[0]" template]]
+ [type
+ ["[0]" primitive]]]]]
+ ["[0]" // (.only Color)
+ ["[0]" rgb]])
+
+(primitive.def .public Command
+ [Text Text]
+
+ (def .public equivalence
+ (Equivalence Command)
+ (implementation
+ (def (= reference it)
+ (let [[beforeR afterR] (primitive.representation Command reference)
+ [beforeI afterI] (primitive.representation Command it)]
+ (and (text#= beforeR beforeI)
+ (text#= afterR afterI))))))
+
+ (def command
+ Text
+ (%.format (text.of_char char.escape) "["))
+
+ (def .public (with command text)
+ (-> Command Text
+ Text)
+ (let [[before after] (primitive.representation Command command)]
+ (%.format before
+ text
+ after)))
+
+ (with_template [<code> <name>]
+ [(def <name>
+ Text
+ (%.format ..command (%.nat <code>) "m"))]
+
+ [39 default_foreground_color]
+ [49 default_background_color]
+ )
+
+ (with_template [<fg> <bg> <color>]
+ [(`` (def .public (,, (template.symbol [<color> "_foreground"]))
+ Command
+ (|> [(%.format ..command (%.nat <fg>) "m")
+ ..default_foreground_color]
+ (primitive.abstraction Command))))
+
+ (`` (def .public (,, (template.symbol [<color> "_background"]))
+ Command
+ (|> [(%.format ..command (%.nat <bg>) "m")
+ ..default_background_color]
+ (primitive.abstraction Command))))]
+
+ [030 040 black]
+ [031 041 red]
+ [032 042 green]
+ [033 043 yellow]
+ [034 044 blue]
+ [035 045 magenta]
+ [036 046 cyan]
+ [037 047 white]
+
+ [090 100 bright_black]
+ [091 101 bright_red]
+ [092 102 bright_green]
+ [093 103 bright_yellow]
+ [094 104 bright_blue]
+ [095 105 bright_magenta]
+ [096 106 bright_cyan]
+ [097 107 bright_white]
+ )
+
+ (with_template [<command> <name> <reset>]
+ [(def .public (<name> it)
+ (-> Color Command)
+ (let [it (//.rgb it)]
+ (|> [(%.format ..command
+ <command>
+ ";" (%.nat (rgb.number (the rgb.#red it)))
+ ";" (%.nat (rgb.number (the rgb.#green it)))
+ ";" (%.nat (rgb.number (the rgb.#blue it)))
+ "m")
+ <reset>]
+ (primitive.abstraction Command))))]
+
+ ["38;2" foreground ..default_foreground_color]
+ ["48;2" background ..default_background_color]
+ )
+ )