-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecent_tui.rb
435 lines (339 loc) · 10.6 KB
/
decent_tui.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
require_relative "decent"
require "io/console"
require "async"
require "async/io/stream"
module Decent
class TerminalNode < TreeNode
def initialize(*args)
super(*args)
@dirty = true
@calculated_size = [0, 0]
@constraints = reactive({ width: @calculated_size[0], height: @calculated_size[1] })
@cache = ""
end
def render
return @cache unless dirty
@dirty = false
layout_children
@cache = render_children
end
def width
attributes[:width] || 1.0
end
def height
attributes[:height] || 1.0
end
def is_stack?
self.class == StackNode
end
# This accounts for layouting.
def layout_children
absolute_widths, fraction_widths = children.partition { _1.width.is_a? Integer }
absolute_heights, fraction_heights = children.partition { _1.height.is_a? Integer }
available_width = is_stack? ? constraints.width : (@constraints.width - absolute_widths.map { _1.width }.sum)
available_height = is_stack? ? (@constraints.height - absolute_heights.map { _1.height }.sum) : constraints.height
widths = fraction_widths.map { _1.width }
heights = fraction_heights.map { _1.height }
resolved_width = is_stack? ? (widths.max || 0) : widths.sum
resolved_height = is_stack? ? heights.sum : (heights.max || 0)
remaining_width = available_width
fraction_widths.each_with_index do |node, idx|
width = node.width
size = (available_width * width / resolved_width).floor
unless is_stack?
remaining_width -= size
if idx == fraction_widths.length - 1
size += remaining_width
end
end
if node.calculated_size[0] != size
node.dirty = true
node.calculated_size[0] = size
node.constraints.width = size
end
end
remaining_height = available_height
fraction_heights.each_with_index do |node, idx|
height = node.height
size = (available_height * height / resolved_height).floor
if is_stack?
remaining_height -= size
if idx == fraction_heights.length - 1
size += remaining_height
end
end
if node.calculated_size[1] != size
node.dirty = true
node.calculated_size[1] = size
node.constraints.height = size
end
end
absolute_widths.each_with_index do |node, idx|
width = node.width
if node.calculated_size[0] != width
node.dirty = true
node.calculated_size[0] = width
node.constraints.width = width
end
end
absolute_heights.each do |node|
height = node.height
if node.calculated_size[1] != height
node.dirty = true
node.calculated_size[1] = height
node.constraints.height = height
end
end
end
def render_children
# This is implicitly a stack, I guess!
rendered = children.map(&:render)
return "" if rendered.length == 0
if is_stack?
rendered.join "\n"
else
child_lines = rendered.map { _1.lines(chomp: true) }
max_height = child_lines.map(&:length).max
# This adds missing vertical height to the rendered child to prevent collisions between child nodes.
child_lines.each do |lines|
(max_height - lines.length).times do
if lines.length > 0
lines.push " " * lines.last.length
end
end
end
child_lines.transpose.map(&:join).join("\n")
end
end
def update
node = self
until node.is_root?
node.dirty = true
node = node.parent
end
node.dirty = true
end
attr_accessor :calculated_size, :dirty, :constraints
end
class TerminalRoot < TerminalNode
def initialize(renderer)
super
@calculated_size = renderer.size.reverse
@constraints = reactive({ width: @calculated_size[0], height: @calculated_size[1] })
@renderer = renderer
end
def draw
@renderer.draw(render)
@renderer.render
end
def is_root?
true
end
end
class StackNode < TerminalNode
end
class FlowNode < TerminalNode
end
class BoxNode < TerminalNode
def render
return @cache unless @dirty
# Width
@constraints.width = @calculated_size[0] - 2
# Height
@constraints.height = @calculated_size[1] - 2
layout_children
width = @constraints.width
height = @constraints.height
child_lines = render_children.lines(chomp: true)
(height - child_lines.length).to_i.times do
child_lines.push("")
end
box = child_lines.map { |line| "│" + line + (" " * (width - line.length)) + "│" }.join("\n")
box = "┌#{'─' * width}┐\n" + box + "\n└#{'─' * width}┘"
@dirty = false
@cache = box
end
end
class LabelNode < TerminalNode
def render
attributes[:content]
end
def height
attributes[:content].lines(chomp: true).length
end
def width
attributes[:content].lines(chomp: true).map(&:length).max
end
end
class SpacerNode < TerminalNode
def render
width = @constraints.width
height = @constraints.height
((" " * width + "\n") * height).chomp
end
end
class DecentTUI < DecentInternal
def fr(num)
num * 0.01
end
def box(width: 1.0, height: 1.0, &ui)
create_node(BoxNode, { width:, height: }, &ui)
end
def stack(&ui)
create_node(StackNode, {}, &ui)
end
def flow(&ui)
create_node(FlowNode, {}, &ui)
end
def label(content)
create_node(LabelNode, { content: }) {}
end
def spacer
create_node(SpacerNode, {}) {}
end
def center(&ui)
spacer
ui.call
spacer
end
def stop_queue
@stop_queue = true
end
def queue_task(&cb)
@queue.push(cb)
end
# Create a keyboard handler (rework later)
def key(names = [], &callback)
if names.is_a? String
names = [names]
end
cleanups = names.map do |name|
@keyboard_handlers[name] = [] unless @keyboard_handlers[name]
handlers = @keyboard_handlers[name]
handlers.push(callback)
-> {
handlers.delete_at(handlers.index(callback))
}
end
Unloadable.new(-> { cleanups.each(&:call) })
end
def initialize(stdout, stdin, &ui)
@renderer = TerminalRenderer.new(stdout, stdin)
@renderer.setup
@stop_queue = false
@queue = []
# This keymap is actually extremely primitive.
@keymap = { "\r" => "Enter",
"\b" => "Backspace",
"\x7F" => "Backspace",
"^Q" => "\u0011",
"^W" => "\u0017",
"^E" => "\u0005",
"^R" => "\u0012",
"^T" => "\u0014",
"^Y" => "\u0019",
"^U" => "\u0015",
"^I" => "\t",
"^O" => "\u000F",
"^P" => "\u0010",
"^A" => "\u0001",
"^S" => "\u0013",
"^D" => "\u0004",
"^F" => "\u0006",
"^G" => "\a",
"^H" => "\b",
"^J" => "k",
"^\v" => "l",
"^\f" => "x",
"^\u0018" => "v",
"^B" => "\u0002",
"^N" => "\u000E",
"^[" => "\e",
"^]" => "\u001D" }
@keyboard_handlers = { "*" => [] }
root = TerminalRoot.new(@renderer)
super root, TerminalNode, &ui
begin
root.draw
# We can remove this after TruffleRuby gets support for Fiber schedulers.
# This is why I wanted 0 deps. Sigh :(
readable_stdin = Async::IO::Stream.new(
Async::IO::Generic.new(stdin)
)
until @stop_queue
Async do |task|
task.async do
stdin.raw!
char = readable_stdin.read(1)
stdin.cooked! # This method name is fucking dumb lmao.
if char == "\x03"
@stop_queue = true
end
mapped = @keymap[char] || char
((@keyboard_handlers[mapped] || []) + @keyboard_handlers["*"]).each do |handler|
handler.call(mapped)
end
end
task.async do
@queue.each do
task.async(&_1)
end
@queue = []
end
end
end
ensure
@renderer.cleanup
end
end
end
def self.tui(stdout = STDOUT, stdin = $stdin, &ui)
DecentTUI.new(stdout, stdin, &ui)
end
class TerminalRenderer
def initialize(stdout = STDOUT, stdin = $stdin)
@stdout = stdout
@stdin = stdin
@buffer = []
@previous_buffer = @buffer
end
def setup
@stdout.print "\033[?1049h" # Save screen
@stdout.print "\033[2J" # Clear screen
@stdout.print "\033[?25l" # Disable cursor
@stdin.echo = false
@stdout.sync = false
clear
end
def cleanup
@stdin.echo = true
@stdout.sync = true
@stdout.print "\033[2J" # Clear screen
@stdout.print "\033[?25h" # Re-enable cursor
@stdout.print "\033[?1049l" # Restore screen
end
# Draw takes starting coordinates and draws text to the current screen buffer.
# Draw does *not* render the current buffer to the terminal.
def draw(text = "", x = 0, y = 0)
# TODO: This sucks, we *need* diffing
text.each_line(chomp: true).each_with_index do |line, y_offset|
line.each_char.each_with_index do |char, x_offset|
@buffer[y + y_offset][x + x_offset] = char
end
end
end
def clear
@buffer = Array.new(size[0]) { Array.new(size[1]) { " " } }
end
# Render takes the current screen buffer and renders it to the terminal.
def render
@stdout.print "\033[0;0f" + @buffer.map(&:join).join("\n")
clear
@stdout.flush
end
# Gets the terminal size, index 0 is rows, index 1 is columns.
def size
@stdout.winsize
end
end
end