Login
7 branches 0 tags
Ben (X13/Arch) Some more documentation for the stdlib c8c25d9 2 years ago 942 Commits
nujel / stdlib_modules / 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 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 [array/ref ansi-fg code]]
           string
           [or disabled reset]]]

[defn black      args :export [wrap  0 [apply cat args]]]
[defn dark-red   args :export [wrap  1 [apply cat args]]]
[defn dark-green args :export [wrap  2 [apply cat args]]]
[defn brown      args :export [wrap  3 [apply cat args]]]
[defn dark-blue  args :export [wrap  4 [apply cat args]]]
[defn purple     args :export [wrap  5 [apply cat args]]]
[defn teal       args :export [wrap  6 [apply cat args]]]
[defn dark-gray  args :export [wrap  7 [apply cat args]]]
[defn gray       args :export [wrap  8 [apply cat args]]]
[defn red        args :export [wrap  9 [apply cat args]]]
[defn green      args :export [wrap 10 [apply cat args]]]
[defn yellow     args :export [wrap 11 [apply cat args]]]
[defn blue       args :export [wrap 12 [apply cat args]]]
[defn pink       args :export [wrap 13 [apply cat args]]]
[defn cyan       args :export [wrap 14 [apply cat args]]]
[defn white      args :export [wrap 15 [apply cat args]]]

[defn rainbow args
      "Wrap ARGS in the colors of the rainbow!"
      :export
      [let* [def count 0]
            [cat [join [map [split [apply cat args] ""]
                            [fn [a]
                                [set! count [bit-and [+ 1 count] #x7]]
                              [cat [or disabled [array/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 [apply cat args] ""]
                             [fn [a]
                                 [set! count [bit-and [+ 1 count] #x7]]
                               [cat [or disabled [array/ref ansi-fg [bit-xor count #x7]]] [or disabled [array/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]]

[deftest "\e[0;33mabc\e[0m" [ansi/wrap 3 "abc"]]
[deftest "\e[0;30m123\e[0m" [ansi/black "123"]]
[deftest "\e[0;31m123\e[0m" [ansi/dark-red "123"]]
[deftest "\e[0;32m123\e[0m" [ansi/dark-green "123"]]
[deftest "\e[0;33m123\e[0m" [ansi/brown "123"]]
[deftest "\e[0;34m123\e[0m" [ansi/dark-blue "123"]]
[deftest "\e[0;35m123\e[0m" [ansi/purple "123"]]
[deftest "\e[0;36m123\e[0m" [ansi/teal "123"]]
[deftest "\e[0;37m123\e[0m" [ansi/dark-gray "123"]]
[deftest "\e[1;30m123\e[0m" [ansi/gray "123"]]
[deftest "\e[1;31m123\e[0m" [ansi/red "123"]]
[deftest "\e[1;32m123\e[0m" [ansi/green "123"]]
[deftest "\e[1;33m123\e[0m" [ansi/yellow "123"]]
[deftest "\e[1;34m123\e[0m" [ansi/blue "123"]]
[deftest "\e[1;35m123\e[0m" [ansi/pink "123"]]
[deftest "\e[1;36m123\e[0m" [ansi/cyan "123"]]
[deftest "\e[1;37m123\e[0m" [ansi/white "123"]]
[deftest "\e[1;31mt\e[1;32me\e[1;33ms\e[1;34mt\e[1;35me\e[1;36mr\e[1;37ml\e[0;37me\e[0;39m" [ansi/rainbow "testerle"]]
[deftest "\e[0;36m\e[41mt\e[0;35m\e[42me\e[0;34m\e[43ms\e[0;33m\e[44mt\e[0;32m\e[45me\e[0;31m\e[46mr\e[0;30m\e[47ml\e[0;37m\e[40me\e[0m" [ansi/rainbow-bg "testerle"]]