2026-11-30 20:49:26 +01:00
|
|
|
;; 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
|
2026-12-01 09:50:09 +01:00
|
|
|
(temp-directory (: System.IO.Path.GetTempPath (-> string)))
|
|
|
|
|
(process-id (: System.Environment.ProcessId int #:get #:no-raise))
|
2026-11-30 20:49:26 +01:00
|
|
|
(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))
|
|
|
|
|
|
2026-12-01 09:50:09 +01:00
|
|
|
;; 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)
|
2026-11-30 20:49:26 +01:00
|
|
|
(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 ()
|
2026-12-01 09:50:09 +01:00
|
|
|
(match (line-read ed term "> " #:notices (Some (fun () (notice flag))))
|
2026-11-30 20:49:26 +01:00
|
|
|
((LineText text) (println (str "read: " (written text))) (go))
|
|
|
|
|
(LineCancelled (println "cancelled") (go))
|
2026-12-01 13:07:56 +01:00
|
|
|
((LineKey _ _) (go))
|
2026-11-30 20:49:26 +01:00
|
|
|
(LineEof (println (str "eof, raw: " (if (terminal-raw? term) "yes" "no")))))))))
|
|
|
|
|
|
|
|
|
|
(: (-> void))
|
|
|
|
|
(defun (parent)
|
|
|
|
|
(def self (str "dotnet " (array-ref (command-line) 0)))
|
2026-12-01 09:50:09 +01:00
|
|
|
(def flag (path-combine (temp-directory) (str "bjoterm-notice-" (int->string process-id))))
|
2026-11-30 20:49:26 +01:00
|
|
|
;; 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"
|
2026-12-01 09:50:09 +01:00
|
|
|
"printf 'par'" "sleep 0.3" (str "touch " flag) "sleep 0.6" "printf 't\\r'" "sleep 0.3"
|
2026-11-30 20:49:26 +01:00
|
|
|
"printf '\\004'" "sleep 1"]
|
|
|
|
|
"; "))
|
2026-12-01 09:50:09 +01:00
|
|
|
(def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " self " child " flag "' /dev/null"))
|
2026-11-30 20:49:26 +01:00
|
|
|
(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!\""))
|
2026-12-01 09:50:09 +01:00
|
|
|
(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\""))
|
2026-11-30 20:49:26 +01:00
|
|
|
(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
|
2026-12-01 09:50:09 +01:00
|
|
|
((and (= (vec-length args) 2) (= (vec-ref args 0) "child")) (child (vec-ref args 1)))
|
2026-11-30 20:49:26 +01:00
|
|
|
((file-exists? "/usr/bin/script") (parent))
|
|
|
|
|
(else (println "skipped: no script command")))
|
|
|
|
|
0)
|