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:
parent
590ac3b853
commit
5a8a441cbc
2 changed files with 59 additions and 13 deletions
44
src/line.bjo
44
src/line.bjo
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue