line-read: #:notices writes lines above the input

line-read calls the function every 200 ms while it waits for a key. The
lines it returns are written above the input, which is drawn again below
them. For a shell's "[1] Done" lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 09:50:09 +01:00
parent 590ac3b853
commit 5a8a441cbc
2 changed files with 59 additions and 13 deletions

View file

@ -206,17 +206,26 @@
;; - Enter accepts (see `#:input-done?`), M-Enter and C-j start a new line,
;; C-l clears the screen, C-z stops the program (see `#:suspend?`),
;; C-c cancels, and C-d on an empty buffer is end of input.
(: (-> LineEditor Terminal string (#:continuation (Option string)) LineResult))
(defun (line-read ed term prompt #:continuation None)
;; - `#:notices`: a function that `line-read` calls every
;; `notice-poll-ms` while it waits for a key. The lines it returns are
;; written above the input, which is then drawn again below them. For a
;; shell's "[1] Done" lines. It must not write to the terminal itself.
(: (-> LineEditor Terminal string (#:continuation (Option string)) (#:notices (Option (-> (Vec string))))
LineResult))
(defun (line-read ed term prompt #:continuation None #:notices None)
(def cont (match continuation
((Some c) c)
(None (string-pad-left "" (text-width prompt)))))
(if (terminal-raw? term)
(edit-loop ed term prompt cont)
(edit-loop ed term prompt cont notices)
;; A `fun` is ordinary code, so the suspending copy passes a
;; bjoroutine.
(by-colour (#:sync (with-raw-mode term (fun () (edit-loop ed term prompt cont))))
(#:bjo (with-raw-mode term (bjoroutine () (edit-loop ed term prompt cont)))))))
(by-colour (#:sync (with-raw-mode term (fun () (edit-loop ed term prompt cont notices))))
(#:bjo (with-raw-mode term (bjoroutine () (edit-loop ed term prompt cont notices)))))))
;; How often `line-read` calls its `#:notices` function, in milliseconds.
(: notice-poll-ms int)
(def notice-poll-ms 200)
(: (-> LineEditor State))
(defun (start-state ed)
@ -224,8 +233,8 @@
(hist-at (vec-length (record-ref ed history))) (draft [])
(search None) (tabbed? #f)))
(: (-> LineEditor Terminal string string LineResult))
(defun (edit-loop ed term prompt cont)
(: (-> LineEditor Terminal string string (Option (-> (Vec string))) LineResult))
(defun (edit-loop ed term prompt cont notices)
(def out (parameter-ref current-output-port))
(def reflow? (match (record-ref ed reflow?)
((Some r) r)
@ -266,9 +275,28 @@
(defun (leave shown)
(write-string out (str (cursor-down (- (last-row shown) (record-ref shown cursor-row))) "\n"))
(set! drawn None))
;; The next event. With `notices`, writes their lines above the input
;; while it waits, and draws `st` again below them.
(: (-> State TermEvent))
(defun (next-event st)
(match notices
(None (read-event term))
((Some f)
(let poll ()
(match (read-event/timeout term notice-poll-ms)
((Some ev) ev)
(None
(def lines (f))
(unless (vec-empty? lines)
(def (Tuple width _) (terminal-size term))
(write-string out (str "\r" (cursor-up (rows-up width)) clear-below
(string-join (vec-map (fun (l) (str l "\n")) lines) "")))
(set! drawn None)
(ignore (draw st #f)))
(poll)))))))
(let go ((st (start-state ed)))
(def lay (draw st #f))
(match (line-step ed st (read-event term))
(match (line-step ed st (next-event st))
((Edited next) (go next))
((Bell next) (write-string out "\u{7}") (go next))
((ClearScreen next)