application/octet-stream
•
3.44 KB
•
95 lines
;;; 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 disabled #f)
(defn disable! (v)
:export
(set! disabled (if (nil? v) #t v)))
(export reset (def reset "\e[0m"))
(export fg-reset (def fg-reset "\e[0;39m"))
(export bg-reset (def bg-reset "\e[49m"))
(export ansi-fg (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"]))
(export ansi-bg (def ansi-bg ["\e[40m"
"\e[41m"
"\e[42m"
"\e[43m"
"\e[44m"
"\e[45m"
"\e[46m"
"\e[47m"]))
(defn wrap (code string)
"Wrap STRING in the ansi color CODE"
:export
(cat (or disabled (ref ansi-fg code))
string
(or disabled reset)))
(defn black args :export (wrap 0 (join args)))
(defn dark-red args :export (wrap 1 (join args)))
(defn dark-green args :export (wrap 2 (join args)))
(defn brown args :export (wrap 3 (join args)))
(defn dark-blue args :export (wrap 4 (join args)))
(defn purple args :export (wrap 5 (join args)))
(defn teal args :export (wrap 6 (join args)))
(defn dark-gray args :export (wrap 7 (join args)))
(defn gray args :export (wrap 8 (join args)))
(defn red args :export (wrap 9 (join args)))
(defn green args :export (wrap 10 (join args)))
(defn yellow args :export (wrap 11 (join args)))
(defn blue args :export (wrap 12 (join args)))
(defn pink args :export (wrap 13 (join args)))
(defn cyan args :export (wrap 14 (join args)))
(defn white args :export (wrap 15 (join args)))
(defn rainbow args
"Wrap ARGS in the colors of the rainbow!"
:export
(let* (def count 0)
(cat (join (map (split (join args) "")
(fn (a)
(set! count (bit-and (+ 1 count) #x7))
(cat (or disabled (ref ansi-fg (if (zero? count) 7 (+ count 8)))) a))) "")
(or disabled fg-reset))))
(defn rainbow-bg args
"Wrap ARGS in the colors of the rainbow!"
:export
(def count 0)
(def colored-list (map (split (join args) "")
(fn (a)
(set! count (bit-and (+ 1 count) #x7))
(cat (or disabled (ref ansi-fg (bit-xor count #x7))) (or disabled (ref ansi-bg count)) a))))
(cat (join colored-list "")
(or disabled reset)))
(defn reprint-line (text width)
:export
(when-not width (set! width 20))
(print "\r")
(dotimes (i width)
(print " "))
(print "\r")
(print text))