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

@ -17,6 +17,8 @@
(import (bjoterm line))
(import/extern
(temp-directory (: System.IO.Path.GetTempPath (-> string)))
(process-id (: System.Environment.ProcessId int #:get #:no-raise))
(command-line (: System.Environment.GetCommandLineArgs (-> (Array string)) #:no-raise)))
(: (-> string bool))
@ -25,14 +27,21 @@
(:acc d (summing (cond ((= c #\() 1) ((= c #\)) -1) (else 0)))))
0))
(: (-> void))
(defun (child)
;; A notice when the parent has made the file `flag`, once.
(: (-> string (Vec string)))
(defun (notice flag)
(if (file-exists? flag)
(begin (ignore (file-delete flag)) ["notice"])
[]))
(: (-> string void))
(defun (child flag)
(match (terminal-open)
((Err e) (println (str "open: " (terminal-error-message e))))
((Ok term)
(def ed (make-line-editor #:input-done? (Some balanced?)))
(let go ()
(match (line-read ed term "> ")
(match (line-read ed term "> " #:notices (Some (fun () (notice flag))))
((LineText text) (println (str "read: " (written text))) (go))
(LineCancelled (println "cancelled") (go))
(LineEof (println (str "eof, raw: " (if (terminal-raw? term) "yes" "no")))))))))
@ -40,14 +49,16 @@
(: (-> void))
(defun (parent)
(def self (str "dotnet " (array-ref (command-line) 0)))
(def flag (path-combine (temp-directory) (str "bjoterm-notice-" (int->string process-id))))
;; Each step waits, so that the child reads each key on its own.
(def keys (string-join ["sleep 2" "printf 'one\\r'" "sleep 0.3"
"printf '(a'" "sleep 0.2" "printf '\\r'" "sleep 0.2" "printf 'b)\\r'" "sleep 0.3"
"printf 'gone'" "sleep 0.2" "printf '\\003'" "sleep 0.3"
"printf '\\020'" "sleep 0.2" "printf '\\020'" "sleep 0.2" "printf '!\\r'" "sleep 0.3"
"printf 'par'" "sleep 0.3" (str "touch " flag) "sleep 0.6" "printf 't\\r'" "sleep 0.3"
"printf '\\004'" "sleep 1"]
"; "))
(def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " self " child' /dev/null"))
(def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " self " child " flag "' /dev/null"))
(def out (match (run/output '(sh "-c" ,command))
((Ok (Tuple _ text)) (string-replace text "\r" ""))
((Err e) (run-error-message e))))
@ -61,6 +72,13 @@
(expect-true "and leaves the input with ^C" (string-contains? out "^C\ncancelled"))
;; C-p, not Up: Up in the two lines of the newer entry moves up a line.
(expect-true "C-p twice shows an older entry" (string-contains? out "read: \"one!\""))
(expect-true "a notice is written above the input"
(string-contains? out "notice\n"))
(expect-true "and the input is drawn again below it"
(match (string-split-once out "notice\n")
((Some (Tuple _ after)) (string-contains? after "> par"))
(None #f)))
(expect-true "the input goes on after it" (string-contains? out "read: \"part\""))
(expect-true "C-d on an empty line is end of input, and raw mode is over"
(string-contains? out "eof, raw: no"))
(unless (string-contains? out "eof, raw: no")
@ -68,7 +86,7 @@
(defun (main args)
(cond
((= args ["child"]) (child))
((and (= (vec-length args) 2) (= (vec-ref args 0) "child")) (child (vec-ref args 1)))
((file-exists? "/usr/bin/script") (parent))
(else (println "skipped: no script command")))
0)