3 Coordinates and the Cursor🔗ℹ

#lang racket
 
(require tui)
 
(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)
   (screen-clear)
 
   (put-at 2 3 "Columns 1..6:")
   (for ([c (in-range 6)])
     (put-at 2 (+ 10 (* c 3)) (format "~a" (add1 c))))
 
   (put-at 4 3 "A")
   (put-at 6 10 "B")
   (put-at 8 7 "C")
 
   (put-at 10 3 "put-at leaves the cursor position alone")
   (put-at! 12 3 "put-at! updates the cursor position")
   (put-at 14 3 (format "cursor = (~a, ~a)"
                        current-cursor-row current-cursor-col))
 
   (cursor-move 16 3)
   (cursor-right 4)
   (cursor-down 1)
   (put-at 18 3 "Press Esc to quit.")
   (wait-for-escape)))

put-at and put-at! both write at an absolute position. The difference is the tracked cursor:

  • put-at leaves the tracked cursor unchanged, which is useful for refreshing a fixed position.

  • put-at! moves the tracked cursor to the written position.

Related functions:

Purpose

 

Functions

read the cursor

 

current-cursor-row current-cursor-col get-cursor

move the cursor

 

cursor-move cursor-up cursor-down cursor-left cursor-right cursor-home

save/restore

 

put-cursor-save put-cursor-restore

Note: put-string and put-at do not advance the tracked cursor. When you mix printing and relative movement, position explicitly with cursor-move or set-cursor! first.