class Chamomile::Layout::Panel
Attributes
Public Class Methods
Source
# File lib/chamomile/layout/panel.rb, line 8 def self.border_map @border_map ||= { rounded: Chamomile::Border::ROUNDED, normal: Chamomile::Border::NORMAL, thick: Chamomile::Border::THICK, double: Chamomile::Border::DOUBLE, ascii: Chamomile::Border::ASCII, none: nil, }.freeze end
Source
# File lib/chamomile/layout/panel.rb, line 19 def initialize(title: nil, width: nil, border: :rounded, color: nil, focused: false) @title = title @width = width @border = border @color = color || (focused ? "#7d56f4" : "#444444") @focused = focused @children = [] end
Public Instance Methods
Source
# File lib/chamomile/layout/panel.rb, line 28 def add(child) @children << child self end
Source
# File lib/chamomile/layout/panel.rb, line 33 def explicit_width? !@width.nil? end
Source
# File lib/chamomile/layout/panel.rb, line 47 def render(width:, height:) my_width = resolved_width(width) inner_width = [my_width - 2, 0].max inner_height = [height - 2, 0].max inner_content = @children.map { |c| c.render(width: inner_width, height: inner_height) }.join("\n") style = Chamomile::Style.new .width(my_width) .height(height) .border_foreground(@color) border_preset = self.class.border_map[@border] || Chamomile::Border::ROUNDED style = style.border(border_preset) if border_preset if @title && border_preset rendered = style.render(inner_content) inject_title(rendered, @title, @color) else style.render(inner_content) end end
Source
# File lib/chamomile/layout/panel.rb, line 37 def resolved_width(available_width) return available_width unless @width if @width.is_a?(String) && @width.end_with?("%") pct = @width.to_f / 100.0 (available_width * pct).to_i else @width.to_i end end