Login
7 branches 0 tags
Ben (RPI 4) [test-run] outputs OS/ARCH, made OS more informative 2809890 4 years ago 166 Commits
nujel / stdlib / ansi.nuj
;; This File contains various functions generating ansi escape sequences for colorful output

[def ansi-fg #[
        "\e[0;30m"
        "\e[0;31m"
        "\e[0;32m"
        "\e[0;33m"
        "\e[0;34m"
        "\e[0;35m"
        "\e[0;36m"
        "\e[0;37m"
        "\e[1;30m"
        "\e[1;31m"
        "\e[1;32m"
        "\e[1;33m"
        "\e[1;34m"
        "\e[1;35m"
        "\e[1;36m"
        "\e[1;37m"
]]
[def ansi-bg #[
        "\e[40m"
        "\e[41m"
        "\e[42m"
        "\e[43m"
        "\e[44m"
        "\e[45m"
        "\e[46m"
        "\e[47m"
]]

[def ansi-names #["black"
                 "dark red"
                 "dark green"
                 "brown"
                 "dark blue"
                 "purple"
                 "teal"
                 "dark gray"
                 "gray"
                 "red"
                 "green"
                 "yellow"
                 "blue"
                 "pink"
                 "cyan"
                 "white"
]]

[def ansi-reset "\e[0m"]
[def ansi-fg-reset "\e[0;39m"]
[def ansi-bg-reset "\e[49m"]

[defun ansi-wrap [code docstring]
                 "Wrap STRING in the ansi color CODE"
                 [def docstring [cat "Wrap ...STRING in a " [ansi-names code] " foreground"]]
                 [eval* `[λ [...string]
			   ,docstring
		           [apply cat `[[ansi-fg ,code] ,@...string ansi-fg-reset]]
	         ]]
]

[defun ansi-bg-wrap [code]
                    "Wrap STRING in the ansi color CODE"
                    [def docstring [cat "Wrap ...STRING in a " [ansi-names code] " background"]]
                    [eval* `[λ [...string]
			      ,docstring
		              [apply cat `[[ansi-bg ,code] ,@...string ansi-bg-reset]]
		    ]]
]
[def ansi-black      [ansi-wrap 0 ]]
[def ansi-dark-red   [ansi-wrap 1 ]]
[def ansi-dark-green [ansi-wrap 2 ]]
[def ansi-brown      [ansi-wrap 3 ]]
[def ansi-dark-blue  [ansi-wrap 4 ]]
[def ansi-purple     [ansi-wrap 5 ]]
[def ansi-teal       [ansi-wrap 6 ]]
[def ansi-dark-gray  [ansi-wrap 7 ]]
[def ansi-gray       [ansi-wrap 8 ]]
[def ansi-red        [ansi-wrap 9 ]]
[def ansi-green      [ansi-wrap 10]]
[def ansi-yellow     [ansi-wrap 11]]
[def ansi-blue       [ansi-wrap 12]]
[def ansi-pink       [ansi-wrap 13]]
[def ansi-cyan       [ansi-wrap 14]]
[def ansi-white      [ansi-wrap 15]]

[def ansi-bg-gray       [ansi-bg-wrap 0]]
[def ansi-bg-red        [ansi-bg-wrap 1]]
[def ansi-bg-green      [ansi-bg-wrap 2]]
[def ansi-bg-yellow     [ansi-bg-wrap 3]]
[def ansi-bg-blue       [ansi-bg-wrap 4]]
[def ansi-bg-pink       [ansi-bg-wrap 5]]
[def ansi-bg-cyan       [ansi-bg-wrap 6]]
[def ansi-bg-white      [ansi-bg-wrap 7]]

[defun ansi-rainbow [...args]
        "Wrap ARGS in the colors of the rainbow!"
        [let* [def count 0]
              [cat [join [map
                        [λ [a]
                           [set! count [logand [+ 1 count] #x7]]
                           [cat [ansi-fg [if [zero? count] 7 [+ count 8]]] a]
                        ]
                        [split [apply cat ...args] ""]] ""]
                   ansi-fg-reset]
        ]
]

[defun ansi-rainbow-bg [...args]
        "Wrap ARGS in the colors of the rainbow!"
        [def count 0]
	[def split-args [split [apply cat ...args] ""]]
	[def colored-list [map
			  [λ [a]
			     [set! count [logand [+ 1 count] #x7]]
			     [cat [ansi-fg [logand [+ 2 count] #x7]] [ansi-bg count] a]
			  ] split-args]]
        [cat [join colored-list ""] ansi-reset]
]