Class: Chamomile::Layout::Panel
- Defined in:
- lib/chamomile/layout/panel.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
Class Method Summary collapse
Instance Method Summary collapse
- #add(child) ⇒ Object
- #explicit_width? ⇒ Boolean
-
#initialize(title: nil, width: nil, border: :rounded, color: nil, focused: false) ⇒ Panel
constructor
A new instance of Panel.
- #render(width:, height:) ⇒ Object
- #resolved_width(available_width) ⇒ Object
Constructor Details
#initialize(title: nil, width: nil, border: :rounded, color: nil, focused: false) ⇒ Panel
Returns a new instance of Panel.
19 20 21 22 23 24 25 26 |
# 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 |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
6 7 8 |
# File 'lib/chamomile/layout/panel.rb', line 6 def children @children end |
Class Method Details
.border_map ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# 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 |
Instance Method Details
#add(child) ⇒ Object
28 29 30 31 |
# File 'lib/chamomile/layout/panel.rb', line 28 def add(child) @children << child self end |
#explicit_width? ⇒ Boolean
33 34 35 |
# File 'lib/chamomile/layout/panel.rb', line 33 def explicit_width? !@width.nil? end |
#render(width:, height:) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# 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 |
#resolved_width(available_width) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# 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 |