11import Base. print
22
3+ """
4+ Terminal
5+ """
36struct Terminal
47 buffers:: Vector{Buffer}
58 current:: Ref{UInt8}
@@ -41,6 +44,9 @@ struct Terminal
4144 end
4245end
4346
47+ """
48+ Get key press
49+ """
4450function get_event (t)
4551 return if isready (t. stdin_channel)
4652 take! (t. stdin_channel)
@@ -59,6 +65,9 @@ update_channel(t, arg::String) = put!(t.stdout_channel, arg)
5965
6066const TERMINAL = Ref {Terminal} ()
6167
68+ """
69+ Flush terminal contents
70+ """
6271function flush (t:: Terminal , diff = true )
6372 previous_buffer = t. buffers[END - t. current[]]
6473 current_buffer = t. buffers[t. current[]]
@@ -73,44 +82,128 @@ function flush(t::Terminal, diff = true)
7382 update (t)
7483end
7584
85+ """
86+ Move cursor
87+ """
7688move_cursor (t:: Terminal , row, col) = update_channel (t, Terminals. CSI, INDICES[row], ' ;' , INDICES[col], ' H' )
89+ """
90+ Move cursor up
91+ """
7792move_cursor_up (t:: Terminal , row = 1 ) = update_channel (t, Terminals. CSI, INDICES[row], ' A' )
93+ """
94+ Move cursor down
95+ """
7896move_cursor_down (t:: Terminal , row = 1 ) = update_channel (t, Terminals. CSI, INDICES[row], ' B' )
97+ """
98+ Move cursor right
99+ """
79100move_cursor_right (t:: Terminal , col = 1 ) = update_channel (t, Terminals. CSI, INDICES[col], ' C' )
101+ """
102+ Move cursor left
103+ """
80104move_cursor_left (t:: Terminal , col = 1 ) = update_channel (t, Terminals. CSI, INDICES[col], ' D' )
105+ """
106+ Move cursor home
107+ """
81108move_cursor_home (t:: Terminal ) = update_channel (t, HOMEPOSITION)
82109
110+ """
111+ Clear screen
112+ """
83113clear_screen (t:: Terminal ) = update_channel (t, CLEARSCREEN)
114+ """
115+ Clear screen from cursor up onwards
116+ """
84117clear_screen_from_cursor_up (t:: Terminal ) = update_channel (t, CLEARBELOWCURSOR)
118+ """
119+ Clear screen from cursor down onwards
120+ """
85121clear_screen_from_cursor_down (t:: Terminal ) = update_channel (t, CLEARABOVECURSOR)
86122
123+ """
124+ Clear line
125+ """
87126clear_line (t:: Terminal ) = update_channel (t, CLEARLINE)
127+ """
128+ Clear line from cursor right onwards
129+ """
88130clear_line_from_cursor_right (t:: Terminal ) = update_channel (t, CLEARAFTERCURSOR)
131+ """
132+ Clear line from cursor left onwards
133+ """
89134clear_line_from_cursor_left (t:: Terminal ) = update_channel (t, CLEARBEFORECURSOR)
90135
136+ """
137+ Hide cursor
138+ """
91139hide_cursor (t:: Terminal ) = update_channel (t, HIDECURSOR)
140+ """
141+ Show cursor
142+ """
92143show_cursor (t:: Terminal ) = update_channel (t, SHOWCURSOR)
93144
145+ """
146+ Save cursor
147+ """
94148save_cursor (t:: Terminal ) = update_channel (t, SAVECURSOR)
149+ """
150+ Restore cursor
151+ """
95152restore_cursor (t:: Terminal ) = update_channel (t, RESTORECURSOR)
96153
154+ """
155+ Change cursor to blinking block
156+ """
97157change_cursor_to_blinking_block (t:: Terminal ) = update_channel (t, CURSORBLINKINGBLOCK)
158+ """
159+ Change cursor to steady block
160+ """
98161change_cursor_to_steady_block (t:: Terminal ) = update_channel (t, CURSORSTEADYBLOCK)
162+ """
163+ Change cursor to blinking underline
164+ """
99165change_cursor_to_blinking_underline (t:: Terminal ) = update_channel (t, CURSORBLINKINGUNDERLINE)
166+ """
167+ Change cursor to steady underline
168+ """
100169change_cursor_to_steady_underline (t:: Terminal ) = update_channel (t, CURSORSTEADYUNDERLINE)
170+ """
171+ Change cursor to blinking ibeam
172+ """
101173change_cursor_to_blinking_ibeam (t:: Terminal ) = update_channel (t, CURSORBLINKINGIBEAM)
174+ """
175+ Change cursor to steady ibeam
176+ """
102177change_cursor_to_steady_ibeam (t:: Terminal ) = update_channel (t, CURSORSTEADYIBEAM)
103178
179+ """
180+ Reset terminal settings
181+ """
104182reset (t:: Terminal ) = update_channel (t, RESET)
105183
184+ """
185+ Alternate screen TUI mode
186+ """
106187tui_mode (t:: Terminal ) = update_channel (t, TUIMODE)
188+ """
189+ Default mode
190+ """
107191default_mode (t:: Terminal ) = update_channel (t, DEFAULTMODE)
108192
193+ """
194+ Get current buffer
195+ """
109196current_buffer (t:: Terminal ):: Buffer = t. buffers[t. current[]]
110197current_buffer ():: Buffer = current_buffer (TERMINAL[])
111198
199+ """
200+ Reset terminal
201+ """
112202reset (t:: Terminal , buffer:: Int ) = reset (t. buffers[buffer])
113203
204+ """
205+ Update terminal
206+ """
114207function update (t:: Terminal )
115208 reset (t. buffers[END - t. current[]])
116209 t. current[] = END - t. current[]
@@ -123,6 +216,9 @@ function update(t::Terminal)
123216 end
124217end
125218
219+ """
220+ Draw terminal
221+ """
126222function draw (t:: Terminal , buffer:: Buffer )
127223 save_cursor (t)
128224 move_cursor_home (t)
@@ -138,6 +234,9 @@ function draw(t::Terminal, buffer::Buffer)
138234 update (t)
139235end
140236
237+ """
238+ Draw terminal
239+ """
141240function draw (t:: Terminal , buffer1:: Buffer , buffer2:: Buffer )
142241 save_cursor (t)
143242 b1 = buffer1. content[:]
@@ -154,13 +253,19 @@ function draw(t::Terminal, buffer1::Buffer, buffer2::Buffer)
154253 restore_cursor (t)
155254end
156255
256+ """
257+ Resize terminal
258+ """
157259function resize (t:: Terminal , w:: Int , h:: Int )
158260 rect = Rect (1 , 1 , w, h)
159261 t. terminal_size[] = rect
160262 t. buffers[1 ] = Buffer (rect)
161263 t. buffers[2 ] = Buffer (rect)
162264end
163265
266+ """
267+ Locate cursor
268+ """
164269function locate_cursor (t:: Terminal )
165270 ucs = Char[]
166271 while length (t. keyboard_buffer) > 0
0 commit comments