Login
7 branches 0 tags
Ben (X13/Arch) Moved some code around 3bb943a 4 years ago 192 Commits
nujel / stdlib / ansi.nuj
;; This File contains various functions generating ansi escape sequences for colorful output

[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"
]]

[def ansi-wrap [λ [code string]
	"Wrap STRING in the ansi color CODE"
	[cat [ansi-fg code] string ansi-reset]
]]

[def ansi-black [λ [...args]
	"Wrap ARGS in black"
	[ansi-wrap 0 [apply cat ...args]]
]]

[def ansi-dark-red [λ [...args]
	"Wrap ARGS in dark red"
	[ansi-wrap 1 [apply cat ...args]]
]]
[def ansi-dark-green [λ [...args]
	"Wrap ARGS in dark green"
	[ansi-wrap 2 [apply cat ...args]]
]]

[def ansi-brown [λ [...args]
	"Wrap ARGS in brown"
	[ansi-wrap 3 [apply cat ...args]]
]]

[def ansi-dark-blue [λ [...args]
	"Wrap ARGS in dark blue"
	[ansi-wrap 4 [apply cat ...args]]
]]

[def ansi-purple [λ [...args]
	"Wrap ARGS in purple"
	[ansi-wrap 5 [apply cat ...args]]
]]

[def ansi-teal [λ [...args]
	"Wrap ARGS in teal"
	[ansi-wrap 6 [apply cat ...args]]
]]

[def ansi-dark-gray [λ [...args]
	"Wrap ARGS in dark gray"
	[ansi-wrap 7 [apply cat ...args]]
]]

[def ansi-gray [λ [...args]
	"Wrap ARGS in gray"
	[ansi-wrap 8 [apply cat ...args]]
]]

[def ansi-red [λ [...args]
	"Wrap ARGS in red"
	[ansi-wrap 9 [apply cat ...args]]
]]

[def ansi-green [λ [...args]
	"Wrap ARGS in green"
	[ansi-wrap 10 [apply cat ...args]]
]]

[def ansi-yellow [λ [...args]
	"Wrap ARGS in yellow"
	[ansi-wrap 11 [apply cat ...args]]
]]

[def ansi-blue [λ [...args]
	"Wrap ARGS in blue"
	[ansi-wrap 12 [apply cat ...args]]
]]

[def ansi-pink [λ [...args]
	"Wrap ARGS in pink"
	[ansi-wrap 13 [apply cat ...args]]
]]

[def ansi-cyan [λ [...args]
	"Wrap ARGS in cyan"
	[ansi-wrap 14 [apply cat ...args]]
]]

[def 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
                        [λ [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 [logxor count #x7]] [ansi-bg count] a]
			  ] split-args]]
        [cat [join colored-list ""] ansi-reset]
]

[defun reprint-line [text width]
       [when-not width [set! width 20]]
       [print "\r"]
       [def i 0]
       [while [< i width]
              [print " "]
              [set! i [++ i]]
       ]
       [print "\r"]
       [print text]
]

[defun test-reprint-line []
       [def i 0]
       [print "\r\n"]
       [while [i < 100000]
              [reprint-line [string i]]
              [set! i [++ i]]
       ]
       [print " Done!\r\n"]
]