Login
7 branches 0 tags
Ben (Win10) Proper Stacktraces!!! 7f3aaa1 4 years ago 220 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"
]]

[defun ansi-wrap [code string]
	"Wrap STRING in the ansi color CODE"
	[cat [ansi-fg code] string 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
                        [\ [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 [+ 1 i]]
       ]
       [print "\r"]
       [print text]
]

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