Login
7 branches 0 tags
Ben (X13/Arch) Fixed string escaping to be more in-line with JSON 7a6f561 3 years ago 509 Commits
nujel / stdlib / string / ansi.nuj
; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
; This project uses the MIT license, a copy should be included under /LICENSE

;; This File contains various functions generating ansi escape sequences for colorful output


[def ansi/disabled #f]

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

[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-reset "\e[0m"]

[def ansi-bg #[
	"\e[40m"
	"\e[41m"
	"\e[42m"
	"\e[43m"
	"\e[44m"
	"\e[45m"
	"\e[46m"
	"\e[47m"
]]


[defun ansi-wrap [code string]
	"Wrap STRING in the ansi color CODE"
	[cat [or ansi/disabled [array/ref ansi-fg code]]
             string
             [or ansi/disabled ansi-reset]]]

[defun ansi-black args
	"Wrap ARGS in black"
	[ansi-wrap 0 [apply cat args]]]

[defun ansi-dark-red args
	"Wrap ARGS in dark red"
	[ansi-wrap 1 [apply cat args]]]

[defun ansi-dark-green args
	"Wrap ARGS in dark green"
	[ansi-wrap 2 [apply cat args]]]

[defun ansi-brown args
	"Wrap ARGS in brown"
	[ansi-wrap 3 [apply cat args]]]

[defun ansi-dark-blue args
	"Wrap ARGS in dark blue"
	[ansi-wrap 4 [apply cat args]]]

[defun ansi-purple args
	"Wrap ARGS in purple"
	[ansi-wrap 5 [apply cat args]]]

[defun ansi-teal args
	"Wrap ARGS in teal"
	[ansi-wrap 6 [apply cat args]]]

[defun ansi-dark-gray args
	"Wrap ARGS in dark gray"
	[ansi-wrap 7 [apply cat args]]]

[defun ansi-gray args
	"Wrap ARGS in gray"
	[ansi-wrap 8 [apply cat args]]]

[defun ansi-red args
	"Wrap ARGS in red"
	[ansi-wrap 9 [apply cat args]]]

[defun ansi-green args
	"Wrap ARGS in green"
	[ansi-wrap 10 [apply cat args]]]

[defun ansi-yellow args
	"Wrap ARGS in yellow"
        [ansi-wrap 11 [apply cat args]]]

[defun ansi-blue args
	"Wrap ARGS in blue"
	[ansi-wrap 12 [apply cat args]]]

[defun ansi-pink args
	"Wrap ARGS in pink"
	[ansi-wrap 13 [apply cat args]]]

[defun ansi-cyan args
	"Wrap ARGS in cyan"
	[ansi-wrap 14 [apply cat args]]]

[defun ansi-white args
	"Wrap ARGS in white"
	[ansi-wrap 15 [apply cat args]]]

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

[defun ansi-rainbow-bg args
        "Wrap ARGS in the colors of the rainbow!"
        [def count 0]
	[def colored-list [map [split [apply cat args] ""]
			  [\ [a]
			     [set! count [logand [+ 1 count] #x7]]
			     [cat [or ansi/disabled [array/ref ansi-fg [logxor count #x7]]] [or ansi/disabled [array/ref ansi-bg count]] a]]]]
        [cat [join colored-list ""]
             [or ansi/disabled ansi-reset]]]

[defun reprint-line [text width]
       [when-not width [set! width 20]]
       [print "\r"]
       [for [i 0 width]
              [print " "]]
       [print "\r"]
       [print text]]