Class: TerminalLayout::Renderer
- Inherits:
-
Object
- Object
- TerminalLayout::Renderer
show all
- Includes:
- HighLine::SystemExtensions, EventEmitter
- Defined in:
- lib/terminal_layout/renderer.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#_callbacks, #emit, #on, #unsubscribe
Constructor Details
#initialize(output: $stdout) ⇒ Renderer
Returns a new instance of Renderer.
12
13
14
15
16
17
|
# File 'lib/terminal_layout/renderer.rb', line 12
def initialize(output: $stdout)
@output = output
@term_info = TermInfo.new ENV["TERM"], @output
@previously_printed_lines = []
@x, @y = 0, 0
end
|
Instance Attribute Details
#term_info ⇒ Object
Returns the value of attribute term_info.
10
11
12
|
# File 'lib/terminal_layout/renderer.rb', line 10
def term_info
@term_info
end
|
Instance Method Details
#clear_screen ⇒ Object
158
|
# File 'lib/terminal_layout/renderer.rb', line 158
def clear_screen ; term_info.control "clear" ; end
|
#clear_screen_down ⇒ Object
159
|
# File 'lib/terminal_layout/renderer.rb', line 159
def clear_screen_down ; term_info.control "ed" ; end
|
#clear_to_beginning_of_line ⇒ Object
157
|
# File 'lib/terminal_layout/renderer.rb', line 157
def clear_to_beginning_of_line ; term_info.control "el1" ; end
|
#dumb_render(object, reset: false) ⇒ Object
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
|
# File 'lib/terminal_layout/renderer.rb', line 99
def dumb_render(object, reset: false)
Treefell['render'].puts %|\nDUMB RENDER: #{self.class}##{__callee__} reset=#{reset} caller=#{caller[0..5].join("\n")}}|
if reset
@y = 0
@previously_printed_lines.clear
else
move_up_n_rows @y
move_to_beginning_of_row
@y = 0
end
@output.print @term_info.control_string "civis"
object = find_top_of_tree(object)
object_width = object.width
rendered_content = object.render
printable_content = rendered_content.sub(/\s*\Z/m, '')
printable_lines = printable_content.split(/\n/).each_with_object([]) do |line, results|
if line.empty?
results << line
else
results.concat line.scan(/.{1,#{terminal_width}}/)
end
end
i = 0
fullzip(printable_lines, @previously_printed_lines) do |new_line, previous_line|
i += 1
if new_line && new_line != previous_line
line2print = "#{new_line}\e[0m"
term_info.control "el"
move_to_beginning_of_row
term_info.control "el"
@output.puts line2print
move_to_beginning_of_row
elsif i <= printable_lines.length
move_down_n_rows 1
end
end
move_to_beginning_of_row
clear_screen_down
lines_drawn = (printable_content.length / object_width.to_f).ceil
@y = lines_drawn
input_box = object.box.find_child_of_type(InputBox) do |box|
box.focused?
end
render_cursor(input_box)
@previously_printed_lines = printable_lines
end
|
#find_top_of_tree(object) ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/terminal_layout/renderer.rb', line 78
def find_top_of_tree(object)
loop do
break unless object.parent
object = object.parent
end
object
end
|
#fullzip(a, b, &blk) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/terminal_layout/renderer.rb', line 86
def fullzip(a, b, &blk)
results = if a.length >= b.length
a.zip(b)
else
b.zip(a).map(&:reverse)
end
if block_given?
results.each { |*args| blk.call(*args) }
else
results
end
end
|
#move_down_n_rows(n) ⇒ Object
167
|
# File 'lib/terminal_layout/renderer.rb', line 167
def move_down_n_rows(n) ; n.times { term_info.control "cud1" } ; end
|
#move_left ⇒ Object
161
|
# File 'lib/terminal_layout/renderer.rb', line 161
def move_left ; move_left_n_characters 1 ; end
|
#move_left_n_characters(n) ⇒ Object
162
|
# File 'lib/terminal_layout/renderer.rb', line 162
def move_left_n_characters(n) ; n.times { term_info.control "cub1" } ; end
|
#move_right_n_characters(n) ⇒ Object
163
|
# File 'lib/terminal_layout/renderer.rb', line 163
def move_right_n_characters(n) ; n.times { term_info.control "cuf1" } ; end
|
#move_to_beginning_of_row ⇒ Object
160
|
# File 'lib/terminal_layout/renderer.rb', line 160
def move_to_beginning_of_row ; move_to_column 0 ; end
|
#move_to_column(n) ⇒ Object
165
|
# File 'lib/terminal_layout/renderer.rb', line 165
def move_to_column(n) ; term_info.control "hpa", n ; end
|
#move_to_column_and_row(column, row) ⇒ Object
164
|
# File 'lib/terminal_layout/renderer.rb', line 164
def move_to_column_and_row(column, row) ; term_info.control "cup", column, row ; end
|
#move_up_n_rows(n) ⇒ Object
166
|
# File 'lib/terminal_layout/renderer.rb', line 166
def move_up_n_rows(n) ; n.times { term_info.control "cuu1" } ; end
|
#render(object, reset: false) ⇒ Object
74
75
76
|
# File 'lib/terminal_layout/renderer.rb', line 74
def render(object, reset: false)
dumb_render(object, reset: reset)
end
|
#render_cursor(input_box) ⇒ Object
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
|
# File 'lib/terminal_layout/renderer.rb', line 19
def render_cursor(input_box)
Treefell['render'].puts %|\nCURSOR RENDER: #{self.class}##{__callee__} caller=#{caller[0..5].join("\n")}}|
move_up_n_rows @y
move_to_beginning_of_row
position = input_box.position
cursor_position = input_box.cursor_position
cursor_x = cursor_position.x
cursor_y = cursor_position.y
relative_position_on_row = position
initial_offset_x = input_box.computed[:x] + (input_box.computed[:y] * terminal_width)
cursor_x = 0
cursor_y = 0
absolute_position_on_row = relative_position_on_row + initial_offset_x
loop do
if absolute_position_on_row >= terminal_width
initial_offset_x = 0
absolute_position_on_row -= terminal_width
cursor_y += 1
else
cursor_x = absolute_position_on_row
break
end
end
if @y < cursor_y
move_up_n_rows(@y - cursor_y)
elsif @y > cursor_y
move_down_n_rows(cursor_y - @y)
end
move_down_n_rows cursor_y
move_to_beginning_of_row
move_right_n_characters cursor_x
@x = cursor_x
@y = cursor_y
if input_box.style[:cursor] == 'none'
@output.print @term_info.control_string "civis"
else
@output.print @term_info.control_string "cnorm"
end
end
|
#terminal_height ⇒ Object
173
174
175
|
# File 'lib/terminal_layout/renderer.rb', line 173
def terminal_height
terminal_size[1]
end
|
#terminal_width ⇒ Object
169
170
171
|
# File 'lib/terminal_layout/renderer.rb', line 169
def terminal_width
terminal_size[0]
end
|