@@ -10,6 +10,7 @@ struct Terminal
1010 stdin_channel:: Channel{Char}
1111 kind:: String
1212 wait:: Float64
13+ ispaused:: Ref{Bool}
1314 function Terminal (stdout_io = stdout , stdin_io = stdin )
1415 width, height = terminal_size ()
1516 rect = Rect (1 , 1 , width, height)
@@ -18,14 +19,23 @@ struct Terminal
1819 cursor_hidden = false
1920 stdout_channel = Channel {String} (Inf )
2021 stdin_channel = Channel {Char} (Inf )
22+ ispaused = Ref {Bool} (false )
2123 stdout_task = @async while true
24+ if ispaused[] == true
25+ sleep (1 )
26+ continue
27+ end
2228 print (stdout_io, take! (stdout_channel))
2329 end
2430 stdin_task = @async while true
31+ if ispaused[] == true
32+ sleep (1 )
33+ continue
34+ end
2535 c = Char (read (stdin_io, 1 )[])
2636 put! (stdin_channel, c)
2737 end
28- t = new (buffers, current, cursor_hidden, rect, Char[], stdout_channel, stdin_channel, get (ENV , " TERM" , " " ), 1 / 1000 )
38+ t = new (buffers, current, cursor_hidden, rect, Char[], stdout_channel, stdin_channel, get (ENV , " TERM" , " " ), 1 / 1000 , ispaused )
2939 TERMINAL[] = t
3040 return t
3141 end
@@ -49,10 +59,14 @@ update_channel(t, arg::String) = put!(t.stdout_channel, arg)
4959
5060const TERMINAL = Ref {Terminal} ()
5161
52- function flush (t:: Terminal )
62+ function flush (t:: Terminal , diff = true )
5363 previous_buffer = t. buffers[END - t. current[]]
5464 current_buffer = t. buffers[t. current[]]
55- draw (t, previous_buffer, current_buffer)
65+ if diff
66+ draw (t, previous_buffer, current_buffer)
67+ else
68+ draw (t, current_buffer)
69+ end
5670 while isready (t. stdout_channel)
5771 sleep (1e-6 )
5872 end
@@ -109,17 +123,35 @@ function update(t::Terminal)
109123 end
110124end
111125
112- function draw (t:: Terminal , buffer1 :: Buffer , buffer2 :: Buffer )
126+ function draw (t:: Terminal , buffer :: Buffer )
113127 save_cursor (t)
114- b1 = buffer1. content[:]
115- b2 = buffer2. content[:]
116128 move_cursor_home (t)
117129 iob = IOBuffer ()
118- for cell in permutedims (buffer2 . content)[:]
130+ for cell in permutedims (buffer . content)[:]
119131 print (iob, cell. style, cell. char, inv (cell. style))
120132 end
121133 update_channel (t, String (take! (iob)))
122134 restore_cursor (t)
135+ while isready (t. stdout_channel)
136+ sleep (1e-6 )
137+ end
138+ update (t)
139+ end
140+
141+ function draw (t:: Terminal , buffer1:: Buffer , buffer2:: Buffer )
142+ save_cursor (t)
143+ b1 = buffer1. content[:]
144+ b2 = buffer2. content[:]
145+ move_cursor_home (t)
146+ R, C = size (buffer2. content)
147+ for r = 1 : R, c = 1 : C
148+ if buffer1. content[r, c] != buffer2. content[r, c]
149+ move_cursor (t, r, c)
150+ cell = buffer2. content[r, c]
151+ update_channel (t, cell. style, cell. char, inv (cell. style))
152+ end
153+ end
154+ restore_cursor (t)
123155end
124156
125157function resize (t:: Terminal , w:: Int , h:: Int )
0 commit comments