;; 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` when the terminal gets narrower: how far up it goes to draw ;; the input again. ;; ;; bjo test tests/reflow.bjo ;; ;; The test runs itself again under `script`. The child makes its terminal ;; narrower with `stty cols` while `line-read` waits in another ;; bjoroutine. `script` does not reflow, so the test checks the escape ;; sequences the editor writes, not the screen. (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))) ;; ---- the child ------------------------------------------------------------- (: (-> void)) (defbjo (narrow-later) (sync (timeout (milliseconds 3000))) (ignore (sync (blocking (fun () (run/output '(sh "-c" "stty cols 20 LineEditor Terminal string)) (defbjo (read-and-narrow ed term) (def narrowing (bjo (narrow-later))) (def result (line-read ed term "> ")) (ignore (sync (promise-join narrowing))) (match result ((LineText text) (str "read " (int->string (string-length text)))) (LineEof "eof") (LineCancelled "cancelled"))) (: (-> bool void)) (defun (child reflow?) (match (terminal-open) ((Err e) (println (str "open: " (terminal-error-message e)))) ((Ok term) (def ed (make-line-editor #:reflow? (Some reflow?))) (match (sync/blocking (promise-join (bjo (read-and-narrow ed term)))) ((Ok text) (println (str "result: " text))) ((Err e) (println (str "raised " (.-Message e)))))))) ;; ---- the parent ------------------------------------------------------------ ;; The output of the child, with the terminal 40 columns wide at the start. (: (-> string string)) (defun (run-child mode) (def self (str "dotnet " (array-ref (command-line) 0))) ;; "> " and 50 characters: a full row of 40 and a row of 12, the cursor ;; at its end. At 20 columns the first row takes 2 rows, so the cursor is ;; 2 rows below the start of the input, not 1. (def keys (str "sleep 2; printf '" (string-pad-left "" 50 #:with #\x) "'; sleep 2.5; printf '\\r'; sleep 0.5")) (def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " self " " mode "' /dev/null")) (match (run/output '(sh "-c" ,command)) ((Ok (Tuple _ text)) (string-replace text "\r" "")) ((Err e) (run-error-message e)))) ;; Whether the first drawing at 20 columns comes right after going up `up` ;; rows and clearing. (: (-> string int bool)) (defun (first-narrow-draw-after? out up) ;; Only a drawing at 20 columns has a row of "> " and 18 x. (def row (str "> " (string-pad-left "" 18 #:with #\x) "\n")) (match (string-split-once out row) ((Some (Tuple before _)) (string-ends-with? before (str "\u{1B}[" (int->string up) "A\u{1B}[J"))) (None #f))) (: (-> void)) (defun (parent) (def reflowing (run-child "reflow")) (expect-true "the input is read" (string-contains? reflowing "result: read 50")) (expect-true "after a reflow it goes up the 2 rows of the wrapped input" (first-narrow-draw-after? reflowing 2)) (def fixed (run-child "fixed")) (expect-true "the input is read" (string-contains? fixed "result: read 50")) (expect-true "without a reflow it goes up the 1 row as drawn" (first-narrow-draw-after? fixed 1)) (unless (string-contains? reflowing "result: read") (println reflowing))) (defun (main args) (cond ((= args ["reflow"]) (child #t)) ((= args ["fixed"]) (child #f)) ((file-exists? "/usr/bin/script") (parent)) (else (println "skipped: no script command"))) 0)