bjoterm: the line editor, (bjoterm line)
- line-read with the keys of Emacs and readline: moving by character, word and line, a kill buffer, history with C-r search, Tab completion through #:completer, and input of several lines with #:input-done?. - Grapheme clusters and display widths in glue/Text.cs: the editor moves and deletes by cluster, and wraps lines by width. - Each key is a pure step, so tests/line.bjo runs the keys without a terminal through simulate-line. tests/line-terminal.bjo runs line-read on a pseudo-terminal. - History files, line-history-save and line-history-load!. - examples/repl.bjo: a small REPL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
fe42387a4c
commit
6863503fa7
7 changed files with 1235 additions and 2 deletions
74
tests/line-terminal.bjo
Normal file
74
tests/line-terminal.bjo
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
;; `line-read` on a real terminal: what it returns, and what it leaves on
|
||||
;; the screen.
|
||||
;;
|
||||
;; bjo test tests/line-terminal.bjo
|
||||
;;
|
||||
;; The test runs itself again under `script`, and writes keys to it with
|
||||
;; pauses between them, as tests/terminal.bjo does.
|
||||
|
||||
(import (std prelude))
|
||||
(import (std simpletest))
|
||||
(import (std run))
|
||||
(import (bjoterm terminal))
|
||||
(import (bjoterm line))
|
||||
|
||||
(import/extern
|
||||
(command-line (: System.Environment.GetCommandLineArgs (-> (Array string)) #:no-raise)))
|
||||
|
||||
(: (-> string bool))
|
||||
(defun (balanced? text)
|
||||
(<= (loop (:for c (in-vec (string->vec text)))
|
||||
(:acc d (summing (cond ((= c #\() 1) ((= c #\)) -1) (else 0)))))
|
||||
0))
|
||||
|
||||
(: (-> void))
|
||||
(defun (child)
|
||||
(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 "> ")
|
||||
((LineText text) (println (str "read: " (written text))) (go))
|
||||
(LineCancelled (println "cancelled") (go))
|
||||
(LineEof (println (str "eof, raw: " (if (terminal-raw? term) "yes" "no")))))))))
|
||||
|
||||
(: (-> void))
|
||||
(defun (parent)
|
||||
(def self (str "dotnet " (array-ref (command-line) 0)))
|
||||
;; 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 '\\004'" "sleep 1"]
|
||||
"; "))
|
||||
(def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " self " child' /dev/null"))
|
||||
(def out (match (run/output '(sh "-c" ,command))
|
||||
((Ok (Tuple _ text)) (string-replace text "\r" ""))
|
||||
((Err e) (run-error-message e))))
|
||||
(expect-true "the terminal opens" (not (string-contains? out "open:")))
|
||||
(expect-true "a line" (string-contains? out "read: \"one\""))
|
||||
(expect-true "Enter in open brackets starts a new line" (string-contains? out "read: \"(a\\nb)\""))
|
||||
(expect-true "the continuation prompt is as wide as the prompt" (string-contains? out "\n b)"))
|
||||
(expect-true "C-c cancels" (string-contains? out "cancelled"))
|
||||
;; The cursor goes to the end of the input with an escape sequence, so
|
||||
;; "gone" and "^C" are not next to each other in the output.
|
||||
(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 "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")
|
||||
(println out)))
|
||||
|
||||
(defun (main args)
|
||||
(cond
|
||||
((= args ["child"]) (child))
|
||||
((file-exists? "/usr/bin/script") (parent))
|
||||
(else (println "skipped: no script command")))
|
||||
0)
|
||||
Loading…
Add table
Add a link
Reference in a new issue