5 Draw a Frame🔗ℹ

Calling one put- function at a time works, but a frame usually has many parts. A common pattern is to build the bytes with format- and write them at once:

(put-format-bytes
 (format-screen-clear)
 (format-cursor-move 2 3)
 (format-rgb-fg 255 128 0 "status"))

put-* and format-* usually take the same arguments: put-at corresponds to format-content-at, and put-rgb-fg corresponds to format-rgb-fg. put-format-bytes simply joins the parts and writes them.

Styles give colors and attributes a name so they can be reused:

(style-define! 'app-title (color-fg* 45 6) attr-bold)
(style-define! 'app-muted (color-fg* 244 8))
 
(put-styled 'app-title "My App")
(put-styled-at 3 3 'app-muted "secondary text")

color-fg* provides both a 256-color and a 16-color value. The library also provides built-in styles such as title, subtitle, error, warning, info, success, and status-bar, ready for put-styled.

#lang racket
 
(require tui)
 
(style-define! 'app-title (color-fg* 45 6) attr-bold)
(style-define! 'app-ok (color-fg* 46 2) attr-bold)
(style-define! 'app-muted (color-fg* 244 8))
 
(define (wait-for-escape)
  (let loop ()
    (define ev (read-event))
    (unless (and (key-event? ev)
                 (eqv? (key-event-key ev) 'escape))
      (loop))))
 
(with-tui
 (λ ()
   (cursor-hide)
   (put-format-bytes
    format-screen-clear
    (format-cursor-move 2 3)
    (format-styled 'app-title "Frame and styles")
    (format-cursor-move 4 3)
    (format-styled 'app-ok "OK   ")
    (format-styled 'app-muted "connected")
    (format-cursor-move 6 3)
    (format-styled 'error "ERROR ")
    (format-styled 'app-muted "something failed")
    (format-cursor-move 8 3)
    "Press Esc to quit.")
   (wait-for-escape)))

Related functions: format-styled / put-styled for named styles; style->bytes to get the style bytes; use-256color! / use-16color! to switch color depth manually.