Skip to content

Commit c87c918

Browse files
committed
Fix modes and add diff
1 parent c1fd4db commit c87c918

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

src/TerminalUserInterfaces.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ include("widgets/list.jl")
3030
include("widgets/barchart.jl")
3131
include("widgets/grid.jl")
3232

33+
function __init__()
34+
35+
backup_termios()
36+
37+
end
38+
3339
end # module

src/terminal.jl

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5060
const 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
110124
end
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)
123155
end
124156

125157
function resize(t::Terminal, w::Int, h::Int)

src/utils.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ tui_mode() = print(stdout, TUIMODE)
9999
default_mode() = print(stdout, DEFAULTMODE)
100100

101101
function with_raw_mode(f::Function)
102+
# if already in in raw mode don't enter raw mode again.
103+
# entering raw mode
102104
entered_raw_mode = false
103105
if MODE[] != :raw
104106
enable_raw_mode()
@@ -179,22 +181,21 @@ function enable_raw_mode()
179181
termios.c_cc[TERMIOS.VMIN] = 0
180182
termios.c_cc[TERMIOS.VTIME] = 1
181183
TERMIOS.tcsetattr(stdin, TERMIOS.TCSANOW, termios)
182-
MODE[] = :raw
183184
end
184185

185186
function disable_raw_mode()
186187
TERMIOS.tcsetattr(stdin, TERMIOS.TCSANOW, BACKUP_STDIN_TERMIOS[])
188+
MODE[] = :default
187189
end
188190

189-
function initialize_backup_termios()
191+
function backup_termios()
190192
BACKUP_STDIN_TERMIOS[] = TERMIOS.termios()
191193
BACKUP_STDOUT_TERMIOS[] = TERMIOS.termios()
192194
TERMIOS.tcgetattr(stdin, BACKUP_STDIN_TERMIOS[])
193195
TERMIOS.tcgetattr(stdout, BACKUP_STDOUT_TERMIOS[])
194196
end
195197

196198
function initialize()
197-
initialize_backup_termios()
198199
tui_mode()
199200
hide_cursor()
200201
enable_raw_mode()

0 commit comments

Comments
 (0)