;; 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` with input taller than the screen: it draws only the rows ;; that fit, and never goes up more rows than the screen has. ;; ;; bjo test tests/tall.bjo ;; ;; The test runs itself again under `script` on a screen of 20 columns and ;; 6 rows. `script` does not keep a screen, so the test checks the escape ;; sequences and the markers the editor writes. (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))) (: (-> void)) (defun (child) (match (terminal-open) ((Err e) (println (str "open: " (terminal-error-message e)))) ((Ok term) (match (line-read (make-line-editor) term "> ") ((LineText text) (println (str "result: read " (int->string (string-length text))))) (_ (println "result: no text")))))) ;; The largest `n` of the cursor-up sequences ESC [ n A in `out`. (: (-> string int)) (defun (most-rows-up out) (def parts (string-split out "\u{1B}[")) (match (loop (:for part (in-vec parts)) (:acc most (maximizing (match (string-split-once part "A") ((Some (Tuple digits _)) (match (string->int digits) ((Some n) n) (None 0))) (None 0))))) ((Some n) n) (None 0))) (: (-> void)) (defun (parent) (def self (str "dotnet " (array-ref (command-line) 0))) ;; "> " and 150 characters take 8 rows of 20. C-a goes to the start of ;; the line, so the window moves to the first rows. (def keys (str "sleep 2; printf '" (string-pad-left "" 150 #:with #\x) "'; sleep 0.5; " "printf '\\001'; sleep 0.5; printf '\\r'; sleep 0.5")) (def command (str "(" keys ") | script -qec 'stty cols 20 rows 6; " 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 input is read" (string-contains? out "result: read 150")) (expect-true "at the end, a marker for the rows above" (string-contains? out "\u{2191} 3 more rows")) (expect-true "at the start, a marker for the rows below" (string-contains? out "\u{2193} 3 more rows")) (expect-true "it never goes up more rows than the screen has" (< (most-rows-up out) 6)) (unless (string-contains? out "result: read") (println out))) (defun (main args) (cond ((= args ["child"]) (child)) ((file-exists? "/usr/bin/script") (parent)) (else (println "skipped: no script command"))) 0)