3 Image saving does not work! If you want to save an image after using SmartConsole
4 you have to execute the line below, otherwise the reference to the dll is saved
5 which will cause a crash after loading the saved image and using SmartConsole.
7 CursesConsole traits lib: Nil.
11 UnitTests addPrototype: #SmartConsole derivedFrom: {Cloneable}.
12 "Not a real unit test, it's an object to dispatch on for a few
13 methods that test SmartConsole/Terminal functionality with human
16 _@(UnitTests SmartConsole traits) streamedCommandProcessor
18 SmartConsole new sessionDo: [| :console terminal editor reader |
19 terminal: (Terminal newBasedOn: console).
20 editor: terminal commandEditor.
21 editor prompt: 'Prompt> '.
22 reader: editor reader.
24 terminal ; 'This test should echo all lines committed by enters, and exit if a \'q\' character is encountered'.
28 terminal nextPut: char.
29 "char printOn: terminal &radix: 16."
36 _@(UnitTests SmartConsole traits) commandProcessor
38 SmartConsole new sessionDo: [| :console terminal editor |
39 terminal: (Terminal newBasedOn: console).
40 editor: terminal commandEditor.
41 editor prompt: 'Prompt> '.
43 terminal ; 'This test should echo lines ending with a dot and committed with enters; quit with the \'quit.\' command'.
45 console writer ; 'Also demonstrates editing at any column: '.
48 [ line: editor readLine.
49 ["console writeCursorPositionAt: (console width // 2) , (console height // 2)."
50 line size = 0 \/ [(line at: line indexLast) ~= $. ]]
51 whileTrue: [line: (editor continue)].
54 terminal ; '\'' ; line ; '\''.
57 on: Stream Exhaustion do: [| :_ |
58 terminal ; '\nStream closed\n'].
60 line ~= Nil /\ [line = 'quit.']
65 _@(UnitTests SmartConsole traits) dimensionInfo
67 SmartConsole new sessionDo: [| :console |
69 console writer ; 'This test should write the screen dimentions in the center; quit with the Escape key'.
72 writePosition: (console width , console height)
73 at: ((console width // 2) , (console height // 2)).
75 console events next keyName = #Escape
80 _@(UnitTests SmartConsole traits) draw
82 SmartConsole new sessionDo: [| :console |
83 console ; 'Move with arrows, scroll with space, change color with 1-8, type chars, Esc quits\n'.
85 console events do: [| :event |
86 event keyName caseOf: {
88 #Enter -> [console moveCursorToBONL].
89 #LeftArrow -> [console moveCursorLeft].
90 #RightArrow -> [console moveCursorRight].
91 #UpArrow -> [console moveCursorUp].
92 #DownArrow -> [console moveCursorDown].
94 event char ifNotNil: [
95 ((event char as: Integer) between: 16r31 and: 16r40)
96 ifTrue: [console foregroundColor: (event char as: Integer) - 16r31]
99 ifTrue: [console scroll]
100 ifFalse: [console writer nextPut: event char]]]].
101 console writeCursorPositionAt: {console width // 2. console height // 2}.
106 _@(UnitTests SmartConsole traits) echoEvent: event@(SmartConsole ResizeEvent traits) on: terminal
108 console: terminal console.
109 terminal ; 'Console resized to: ' ; (console width as: String) ; 'x' ; (console height as: String).
115 _@(UnitTests SmartConsole traits) echoEvent: event@(SmartConsole KeyEvent traits) on: terminal
117 console: terminal console.
118 terminal ; 'Key code: ' ; (event keyCode as: String) ; ' - 16r' ;
119 (event keyCode printString &radix: 16).
123 terminal ; 'Key name: '.
125 ifNil: [terminal ; 'Nil']
126 ifNotNil: [terminal ; (event keyName as: terminal collectionType)].
130 terminal ; 'Character: '.
132 ifNil: [terminal ; 'Nil']
133 ifNotNil: [terminal nextPut: event char].
137 terminal ; 'Controls: ' ; event controls printString.
141 console writeCursorPositionAt: {console width // 2. console height // 2}.
143 event keyName = #Escape
146 t@(UnitTests SmartConsole traits) echoEvents
148 SmartConsole new sessionDo: [| :console terminal |
149 terminal: (Terminal newBasedOn: console).
150 terminal ; 'Press keys and their event will be printed, quit with Esc\n'.
153 " console moveCursorTo: (0 , 1)."
154 t echoEvent: console events next on: terminal
159 _@(UnitTests SmartConsole traits) echoRawCursesEvents
161 CursesConsole new sessionDo: [| :console terminal |
162 terminal: (Terminal newBasedOn: console).
163 terminal ; 'Press keys and raw key codes will be printed, quit with Enter\n'.
166 keyCode: (console primitives nextEvent applyTo: {-1}).
167 terminal ; (keyCode as: String) ; ' - 16r' ;
168 (keyCode printString &radix: 16).
171 keyCode = 10 \/ [keyCode = 13]
176 _@(UnitTests SmartConsole traits) echoRawWindowsEvents &unicode: unicode
179 [WindowsConsole isAvailable ifTrue: [available: True]]
181 available ifFalse: [error: 'Windows console plugin not loaded or otherwise not available'. ^ Nil].
182 unicode ifNil: [unicode: False].
184 WindowsConsole clone sessionDo: [| :c record dword |
185 c ; 'Printing out raw console events coming from Windows, quit with Esc\n'.
186 c ; 'Codepage returned by GetConsoleCP: ' ; Windows Kernel GetConsoleCP do printString ; '\n'.
188 record: Windows INPUT_RECORD clone.
189 dword: (ByteArray new &capacity: #[Windows DWORD byteSize]).
193 ifTrue: [method: Windows Kernel ReadConsoleInputW]
194 ifFalse: [method: Windows Kernel ReadConsoleInputA].
195 method applyTo: {c consoleIn. record. 1. dword}.
196 record EventType = #[Windows KEY_EVENT]
199 "record printOn: c writer.
201 c ; 'Down ' ; record Event_KeyEvent_bKeyDown printString
202 ; ', Repeat ' ; record Event_KeyEvent_wRepeatCount printString
203 ; ', VKeyCode ' ; record Event_KeyEvent_wVirtualKeyCode printString
204 ; ', VScanCode ' ; record Event_KeyEvent_wVirtualScanCode printString.
206 ifTrue: [c ; ', Unicode ' ; (record Event_KeyEvent_uChar_UnicodeChar printString &radox: 16)]
208 c ; ', Ascii ' ; (record Event_KeyEvent_uChar_AsciiChar printString &radix: 16).
209 (record Event_KeyEvent_uChar_AsciiChar between: 0 and: 255)
212 c writer nextPut: (record Event_KeyEvent_uChar_AsciiChar as: ASCIICharacter).
214 c ; ', Controls ' ; (record Event_KeyEvent_dwControlKeyState printString &radix: 2) ; ' ('.
225 } inject: 0 into: [| :bitCount :symbol |
226 (record Event_KeyEvent_dwControlKeyState bitAnd: (symbol sendTo: {Windows})) isZero
229 bitCount isZero ifFalse: [c ; ','].
233 record Event_KeyEvent_wVirtualKeyCode = 27