changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/lib/cli/tests/ansi.lisp

changeset 689: 2e7d93b892a5
author: Richard Westhaver <ellis@rwest.io>
date: Tue, 01 Oct 2024 22:29:08 -0400
permissions: -rw-r--r--
description: cli shell tests init
1 ;;; ansi.lisp --- ANSI Tests
2 
3 ;;
4 
5 ;;; Code:
6 (in-package :cli/tests)
7 (in-suite :cli)
8 
9 (defun ansi-t01 ()
10  (erase)
11  (cursor-position 0 0)
12  (princ "0")
13  (cursor-position 2 2)
14  (princ "1")
15  (cursor-position 5 15)
16  (princ "test")
17  (cursor-position 10 15)
18  (force-output)
19  (with-input-from-string (in (format nil "test~%~%"))
20  (let ((a (read-line in)))
21  (cursor-position 12 15)
22  (princ a)
23  (force-output))))
24 
25 (defun ansi-t02 ()
26  (print "normal")
27  (.sgr 1)
28  (print "bold")
29  (.sgr 4)
30  (print "bold underline")
31  (.sgr 7)
32  (print "bold underline reverse")
33  (.sgr 22)
34  (print "underline reverse")
35  (.sgr 24)
36  (print "reverse")
37  (.sgr 27)
38  (print "normal")
39  (.sgr 1 4 7)
40  (print "bold underline reverse")
41  (.sgr 0)
42  (print "normal")
43  (force-output))
44 
45 (defun ansi-t03 ()
46  "Display the 256 color palette."
47  (clear)
48  (loop for i from 0 to 255 do
49  (.sgr 48 5 i)
50  (princ #\space))
51  (terpri)
52  (.sgr 0)
53  (loop for i from 0 to 255 do
54  (.sgr 38 5 i)
55  (princ "X"))
56  (.sgr 0)
57  (force-output)
58  ;; (sleep 3)
59  (.ris)
60  (force-output))
61 
62 (defun ansi-t04 ()
63  "Hide and show the cursor."
64  (princ "Cursor visible:")
65  (force-output)
66  ;; (sleep 2)
67  (terpri)
68  (princ "Cursor invisible:")
69  (hide-cursor)
70  (force-output)
71  ;; (sleep 2)
72  (terpri)
73  (princ "Cursor visible:")
74  (show-cursor)
75  (force-output)
76  ;; (sleep 2)
77  )
78 
79 (defun ansi-t05 ()
80  "Switch to and back from the alternate screen buffer."
81  (princ "Normal screen buffer. ")
82  (force-output)
83  ;; (sleep 2)
84  (save-cursor-position)
85  (use-alternate-screen-buffer)
86  (clear)
87  (princ "Alternate screen buffer.")
88  (force-output)
89  ;; (sleep 2)
90  (use-normal-screen-buffer)
91  (restore-cursor-position)
92  (princ "Back to Normal screen buffer.")
93  (force-output)
94  ;; (sleep 1)
95  )
96 
97 (defun ansi-t06 ()
98  "Set individual termios flags to enable raw and disable echo mode.
99 
100 Enabling raw mode allows read-char to return immediately after a key is pressed.
101 
102 In the default cooked mode, the entry has to be confirmed by pressing enter."
103  (set-tty-mode t :ignbrk nil
104  :brkint nil
105  :parmrk nil
106  :istrip nil
107  :inlcr nil
108  :igncr nil
109  :icrnl nil
110  :ixon nil
111  :opost nil
112  :echo nil
113  :echonl nil
114  :icanon nil
115  :isig nil
116  :iexten nil
117  :csize nil
118  :parenb nil
119  :vmin 1
120  :vtime 0)
121  (erase)
122  (cursor-position 1 1)
123  (force-output)
124  (let ((a (read-char)))
125  (cursor-position 10 5)
126  (princ a)
127  (force-output))
128 
129  (set-tty-mode t :echo t
130  :brkint t
131  :ignpar t
132  :istrip t
133  :icrnl t
134  :ixon t
135  :opost t
136  :isig t
137  :icanon t
138  :veol 0))
139 
140 (defun ansi-t07 ()
141  "Use combination modes that consist of several individual flags.
142 
143 Cooked and raw are opposite modes. Enabling cooked disbles raw and vice versa."
144  (set-tty-mode t :cooked nil)
145  (erase)
146  (cursor-position 1 1)
147  (force-output)
148  (let ((a (read-char)))
149  (cursor-position 3 1)
150  (princ a)
151  (force-output))
152  (set-tty-mode t :raw nil))
153 
154 (defun ansi-t08 ()
155  "Why doesnt calling the stty utility work?"
156  (uiop:run-program "stty raw -echo" :ignore-error-status t)
157  (erase)
158  (cursor-position 1 1)
159  (force-output)
160  (let ((a (read-char)))
161  (cursor-position 2 1)
162  (princ a)
163  (force-output))
164  (uiop:run-program "stty -raw echo" :ignore-error-status t))
165 
166 (defun ansi-t09 ()
167  "Query terminal size with ANSI escape sequences."
168  ;; Put the terminal into raw mode so we can read the "user input"
169  ;; of the reply char by char
170  ;; Turn off the echo or the sequence will be displayed
171  (set-tty-mode t :cooked nil :echo nil)
172  (save-cursor-position)
173  ;; Go to the bottom right corner of the terminal by attempting
174  ;; to go to some high value of row and column
175  (cursor-position 999 999)
176  (let (chars)
177  ;; The terminal returns an escape sequence to the standard input
178  (device-status-report)
179  (force-output)
180  ;; The reply isnt immediately available, the terminal does need
181  ;; some time to answer
182  (sleep 0.1)
183  ;; The reply has to be read as if the user typed an escape sequence
184  (loop for i = (read-char-no-hang *standard-input* nil)
185  until (null i)
186  do (push i chars))
187  ;; Put the terminal back into its initial cooked state
188  (set-tty-mode t :raw nil :echo t)
189  (restore-cursor-position)
190  ;; Return the read sequence as a list of characters.
191  (nreverse chars)))
192 
193 (deftest ansi ()
194  (with-input-from-string (in (format nil "~%~%"))
195  (ansi-t01)
196  (ansi-t02)
197  (ansi-t03)
198  (ansi-t04)
199  (ansi-t05)))