Login
7 branches 0 tags
Ben (X13/Arch) Fixed benchmark script a45156b 2 years ago 1056 Commits
nujel / stdlib_modules / core / term.nuj
(defn put-char (char)
      :export
      (stdout 'char-write char))

(defn put-string (text)
      :export
      (stdout 'block-write text))

(defn clear-screen ()
      :export
      (stdout 'block-write "\e[2J"))

(defn clear-line ()
      :export
      (stdout 'block-write "\e[2K"))

(defn hide-cursor ()
      :export
      (stdout 'block-write "\e[?25l"))

(defn show-cursor ()
      :export
      (stdout 'block-write "\e[?25h"))

(defn start ()
      :export
      (stdin :raw!)
      (stdout 'block-write "\e[?1049h"))

(defn stop ()
      :export
      (stdout 'block-write "\e[?1049l"))

(defn move-cursor (column line)
      :export
      (stdout 'block-write (fmt "\e[{line};{column}H")))

(def fg-colors { :default "\e[0m"

                 :black "\e[30m"
                 :red "\e[31m"
                 :green "\e[32m"
                 :yellow "\e[33m"
                 :blue "\e[34m"

                 :pink "\e[35m"
                 :magenta "\e[35m"
                 :purple "\e[35m"

                 :cyan "\e[36m"
                 :teal "\e[36m"

                 :white "\e[37m"})

(def bg-colors { :default "\e[49m"

                 :black "\e[40m"
                 :red "\e[41m"
                 :green "\e[42m"
                 :yellow "\e[43m"
                 :blue "\e[44m"

                 :pink "\e[45m"
                 :magenta "\e[45m"
                 :purple "\e[45m"

                 :cyan "\e[46m"
                 :teal "\e[46m"

                 :white "\e[47m"})

(defn set-fg-color (color-name)
      :export
      (def v (ref fg-colors color-name))
      (when-not v (exception "Unknown color" color-name))
      (stdout 'block-write v))

(defn set-bg-color (color-name)
      :export
      (def v (ref bg-colors color-name))
      (when-not v (exception "Unknown color" color-name))
      (stdout 'block-write v))

(defn set-color (fg-color-name bg-color-name)
      :export
      (when fg-color-name (set-fg-color fg-color-name))
      (when bg-color-name (set-bg-color bg-color-name)))

(defn get-size ()
      :export
      (stdout 'block-write "\e 7")
      (move-cursor 9999 9999)
      (stdout 'block-write "\e[6n")
      (stdout 'flush-output)
      (def buf (buffer/allocate 128))
      (def i 0)
      (while #t
        (def c (stdin 'char-read))
        (when (= c #\R)
          (stdout 'block-write "\e 8")
          (def s (split (cut (buffer->string buf i) 2) ";"))
          (return { :width (read/int (cadr s))
                    :height (read/int (car s))}))
        (set! buf i c)
        (inc! i)))

(defn get-width ()
      :export
      (ref (get-size) :width))

(defn get-height ()
      :export
      (ref (get-size) :height))

(defn poll-input ()
      :export
      (def c (stdin 'char-read))
      (if (= c :end-of-file)
          0
          c))

(defn draw-box (color pos-x pos-y width height)
      :export
      (set-bg-color color)
      (dotimes (y height)
               (move-cursor pos-x (+ y pos-y))
               (dotimes (x width)
                        (put-char 32)))
      (set-bg-color :default))