Class: RubyRich::Layout
- Inherits:
-
Object
- Object
- RubyRich::Layout
- Defined in:
- lib/ruby_rich/layout.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#content ⇒ Object
Returns the value of attribute content.
-
#dialog ⇒ Object
Returns the value of attribute dialog.
-
#height ⇒ Object
Returns the value of attribute height.
-
#live ⇒ Object
Returns the value of attribute live.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#ratio ⇒ Object
Returns the value of attribute ratio.
-
#show ⇒ Object
Returns the value of attribute show.
-
#size ⇒ Object
Returns the value of attribute size.
-
#split_direction ⇒ Object
readonly
Returns the value of attribute split_direction.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x_offset ⇒ Object
Returns the value of attribute x_offset.
-
#y_offset ⇒ Object
Returns the value of attribute y_offset.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add_child(layout) ⇒ Object
- #calculate_dimensions(terminal_width, terminal_height) ⇒ Object
- #calculate_node_dimensions(available_width, available_height) ⇒ Object
- #contains?(x, y) ⇒ Boolean
- #draw ⇒ Object
- #find_by_name(name) ⇒ Object
- #hide_dialog ⇒ Object
- #hit_test(x, y) ⇒ Object
-
#initialize(name: nil, ratio: 1, size: nil, width: nil, height: nil) ⇒ Layout
constructor
A new instance of Layout.
- #key(event_name, priority = 0, &block) ⇒ Object
- #notify_listeners(event_data) ⇒ Object
- #render ⇒ Object
- #render_dialog_into(buffer) ⇒ Object
- #render_into(buffer) ⇒ Object
- #render_to_buffer ⇒ Object
- #root ⇒ Object
- #show_dialog(dialog) ⇒ Object
- #split_column(*layouts) ⇒ Object
- #split_row(*layouts) ⇒ Object
- #update_content(content) ⇒ Object
Constructor Details
#initialize(name: nil, ratio: 1, size: nil, width: nil, height: nil) ⇒ Layout
Returns a new instance of Layout.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ruby_rich/layout.rb', line 7 def initialize(name: nil, ratio: 1, size: nil, width: nil, height: nil) @name = name @ratio = ratio @size = size @children = [] @content = nil @parent = nil @x_offset = 0 @y_offset = 0 @width = width if width @height = height if height @split_direction = nil @show = true @event_listeners = {} @event_intercepted = false @mouse_capture = nil end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
3 4 5 |
# File 'lib/ruby_rich/layout.rb', line 3 def children @children end |
#content ⇒ Object
Returns the value of attribute content.
3 4 5 |
# File 'lib/ruby_rich/layout.rb', line 3 def content @content end |
#dialog ⇒ Object
Returns the value of attribute dialog.
3 4 5 |
# File 'lib/ruby_rich/layout.rb', line 3 def dialog @dialog end |
#height ⇒ Object
Returns the value of attribute height.
4 5 6 |
# File 'lib/ruby_rich/layout.rb', line 4 def height @height end |
#live ⇒ Object
Returns the value of attribute live.
3 4 5 |
# File 'lib/ruby_rich/layout.rb', line 3 def live @live end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/ruby_rich/layout.rb', line 3 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
3 4 5 |
# File 'lib/ruby_rich/layout.rb', line 3 def parent @parent end |
#ratio ⇒ Object
Returns the value of attribute ratio.
3 4 5 |
# File 'lib/ruby_rich/layout.rb', line 3 def ratio @ratio end |
#show ⇒ Object
Returns the value of attribute show.
4 5 6 |
# File 'lib/ruby_rich/layout.rb', line 4 def show @show end |
#size ⇒ Object
Returns the value of attribute size.
3 4 5 |
# File 'lib/ruby_rich/layout.rb', line 3 def size @size end |
#split_direction ⇒ Object (readonly)
Returns the value of attribute split_direction.
5 6 7 |
# File 'lib/ruby_rich/layout.rb', line 5 def split_direction @split_direction end |
#width ⇒ Object
Returns the value of attribute width.
4 5 6 |
# File 'lib/ruby_rich/layout.rb', line 4 def width @width end |
#x_offset ⇒ Object
Returns the value of attribute x_offset.
4 5 6 |
# File 'lib/ruby_rich/layout.rb', line 4 def x_offset @x_offset end |
#y_offset ⇒ Object
Returns the value of attribute y_offset.
4 5 6 |
# File 'lib/ruby_rich/layout.rb', line 4 def y_offset @y_offset end |
Instance Method Details
#[](name) ⇒ Object
116 117 118 |
# File 'lib/ruby_rich/layout.rb', line 116 def [](name) find_by_name(name) end |
#add_child(layout) ⇒ Object
101 102 103 104 |
# File 'lib/ruby_rich/layout.rb', line 101 def add_child(layout) layout.parent = self @children << layout end |
#calculate_dimensions(terminal_width, terminal_height) ⇒ Object
110 111 112 113 114 |
# File 'lib/ruby_rich/layout.rb', line 110 def calculate_dimensions(terminal_width, terminal_height) @x_offset = 0 @y_offset = 0 calculate_node_dimensions(terminal_width, terminal_height) end |
#calculate_node_dimensions(available_width, available_height) ⇒ Object
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 |
# File 'lib/ruby_rich/layout.rb', line 230 def calculate_node_dimensions(available_width, available_height) @width = if @size && @parent&.split_direction == :row [@size, available_width].min else available_width end @height = if @size && @parent&.split_direction == :column [@size, available_height].min else available_height end if @content.class == RubyRich::Panel @content.width = @width @content.height = @height else @content.width = @width if @content.respond_to?(:width=) @content.height = @height if @content.respond_to?(:height=) end return if @children.empty? @children.each do |child| next unless child.content.respond_to?(:desired_height) next unless @split_direction == :column child.size = [[child.content.desired_height, 1].max, available_height - 2].min end case @split_direction when :row remaining_width = @width fixed_children, flexible_children = @children.partition { |c| c.size } fixed_children.each do |child| child_width = [child.size, remaining_width].min child.width = child_width remaining_width -= child_width end total_ratio = flexible_children.sum(&:ratio) if total_ratio > 0 ratio_width = remaining_width.to_f / total_ratio flexible_children.each do |child| child_width = (child.ratio * ratio_width).floor child.width = child_width remaining_width -= child_width end flexible_children.last.width += remaining_width if remaining_width > 0 end @children.each { |child| child.height = @height } current_x = @x_offset @children.each do |child| child.x_offset = current_x child.y_offset = @y_offset current_x += child.width child.calculate_node_dimensions(child.width, child.height) end when :column remaining_height = @height fixed_children, flexible_children = @children.partition { |c| c.size } fixed_children.each do |child| child_height = [child.size, remaining_height].min child.height = child_height remaining_height -= child_height end total_ratio = flexible_children.sum(&:ratio) if total_ratio > 0 ratio_height = remaining_height.to_f / total_ratio flexible_children.each do |child| child_height = (child.ratio * ratio_height).floor child.height = child_height remaining_height -= child_height end flexible_children.last.height += remaining_height if remaining_height > 0 end @children.each { |child| child.width = @width } current_y = @y_offset @children.each do |child| child.x_offset = @x_offset child.y_offset = current_y current_y += child.height child.calculate_node_dimensions(child.width, child.height) end end end |
#contains?(x, y) ⇒ Boolean
68 69 70 71 72 73 74 75 76 |
# File 'lib/ruby_rich/layout.rb', line 68 def contains?(x, y) return false unless @show return false unless @width && @height x >= @x_offset && x < @x_offset + @width && y >= @y_offset && y < @y_offset + @height end |
#draw ⇒ Object
153 154 155 |
# File 'lib/ruby_rich/layout.rb', line 153 def draw puts render end |
#find_by_name(name) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/ruby_rich/layout.rb', line 120 def find_by_name(name) return self if @name == name @children.each do |child| result = child.find_by_name(name) return result if result end nil end |
#hide_dialog ⇒ Object
133 134 135 136 |
# File 'lib/ruby_rich/layout.rb', line 133 def hide_dialog @dialog.notify_listeners({:name=>:close}) @dialog = nil end |
#hit_test(x, y) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ruby_rich/layout.rb', line 78 def hit_test(x, y) return nil unless contains?(x, y) @children.reverse_each do |child| hit = child.hit_test(x, y) return hit if hit end self end |
#key(event_name, priority = 0, &block) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/ruby_rich/layout.rb', line 25 def key(event_name, priority = 0, &block) unless @event_listeners[event_name] @event_listeners[event_name] = [] end @event_listeners[event_name] << { priority: priority, block: block } @event_listeners[event_name].sort_by! { |l| -l[:priority] } # Higher priority first end |
#notify_listeners(event_data) ⇒ Object
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 |
# File 'lib/ruby_rich/layout.rb', line 38 def notify_listeners(event_data) return route_mouse_event(event_data) if event_data[:type] == :mouse return if @event_intercepted if @dialog @dialog.notify_listeners(event_data) else event_name = event_data[:name] if @event_listeners[event_name] @event_listeners[event_name].each do |listener| next if @event_intercepted result = listener[:block].call(event_data, self.root.live) if result == true @event_intercepted = true end end end unless @event_intercepted @children.each do |child| child.notify_listeners(event_data) end end end end |
#render ⇒ Object
138 139 140 141 142 |
# File 'lib/ruby_rich/layout.rb', line 138 def render # Convert buffer to string (join lines with newlines) buffer = render_to_buffer buffer.map { |line| line.compact.join("") }.join("\n") end |
#render_dialog_into(buffer) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/ruby_rich/layout.rb', line 157 def render_dialog_into(buffer) start_x = (@width - 2 - @dialog.width) / 2 + 1 start_y = (@height - 2 - @dialog.height) / 2 + 1 dialog_buffer = @dialog.render_to_buffer buffer.each_with_index do |arr, y| arr.each_with_index do |char, x| if x >= start_x && y >= start_y if y-start_y <= dialog_buffer.size-1 && x-start_x <= dialog_buffer[y-start_y].size-1 buffer[y][x] = dialog_buffer[y-start_y][x-start_x] end end end end end |
#render_into(buffer) ⇒ Object
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 |
# File 'lib/ruby_rich/layout.rb', line 172 def render_into(buffer) children.each { |child| child.render_into(buffer) } if children return unless content content_lines = if content.is_a?(String) content.split("\n")[0...height] else normalize_rendered_lines(content.render)[0...height] end content_lines.each_with_index do |line, line_index| y_pos = y_offset + line_index next if y_pos >= buffer.size in_escape = false escape_char = "" char_width = 0 # Initialize width to 0 for position calculation line.each_char do |char| # Handle ANSI escape codes if in_escape escape_char += char in_escape = false if char == 'm' if escape_char=="\e[0m" escape_char = "" end next elsif char.ord == 27 # Detect escape sequence start \e in_escape = true escape_char += char next end # Calculate character width char_w = case char.ord when 0x0000..0x007F then 1 # English characters when 0x4E00..0x9FFF then 2 # Chinese characters else Unicode::DisplayWidth.of(char) end # Calculate character start position x_start = x_offset + char_width # Skip if beyond right boundary next if x_start >= buffer[y_pos].size # Handle character rendering (Chinese characters may occupy multiple positions) char_w.times do |i| x_pos = x_start + i break if x_pos >= buffer[y_pos].size # Stop at right boundary unless escape_char.empty? char = escape_char + char + "\e[0m" # Record character's actual color each time end buffer[y_pos][x_pos] = char unless i > 0 # Write Chinese character only at first position to avoid overwriting buffer[y_pos][x_pos+1] = nil if char_w == 2 end char_width += char_w # Update cumulative width end end end |
#render_to_buffer ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/ruby_rich/layout.rb', line 144 def render_to_buffer # Initialize buffer (2D array, each element represents a character) buffer = Array.new(@height) { Array.new(@width, " ") } # Recursively fill content into buffer render_into(buffer) render_dialog_into(buffer) if @dialog return buffer end |
#root ⇒ Object
64 65 66 |
# File 'lib/ruby_rich/layout.rb', line 64 def root @parent ? @parent.root : self end |
#show_dialog(dialog) ⇒ Object
129 130 131 |
# File 'lib/ruby_rich/layout.rb', line 129 def show_dialog(dialog) @dialog = dialog end |
#split_column(*layouts) ⇒ Object
95 96 97 98 99 |
# File 'lib/ruby_rich/layout.rb', line 95 def split_column(*layouts) @split_direction = :column layouts.each { |l| l.parent = self } @children.concat(layouts) end |
#split_row(*layouts) ⇒ Object
89 90 91 92 93 |
# File 'lib/ruby_rich/layout.rb', line 89 def split_row(*layouts) @split_direction = :row layouts.each { |l| l.parent = self } @children.concat(layouts) end |
#update_content(content) ⇒ Object
106 107 108 |
# File 'lib/ruby_rich/layout.rb', line 106 def update_content(content) @content = content end |