Class: Chamomile::Widgets::Panel
- Inherits:
-
Object
- Object
- Chamomile::Widgets::Panel
- Defined in:
- lib/chamomile/frame.rb
Overview
Bordered panel with optional title.
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#initialize(title: nil, border: :rounded, width: nil) ⇒ Panel
constructor
A new instance of Panel.
- #render(width: 80, height: 24) ⇒ Object
- #text(content) ⇒ Object
Constructor Details
#initialize(title: nil, border: :rounded, width: nil) ⇒ Panel
Returns a new instance of Panel.
115 116 117 118 119 120 |
# File 'lib/chamomile/frame.rb', line 115 def initialize(title: nil, border: :rounded, width: nil) @title = title @border = border @width = width @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
113 114 115 |
# File 'lib/chamomile/frame.rb', line 113 def children @children end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
113 114 115 |
# File 'lib/chamomile/frame.rb', line 113 def title @title end |
Instance Method Details
#render(width: 80, height: 24) ⇒ Object
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/chamomile/frame.rb', line 128 def render(width: 80, height: 24) w = @width || width inner_w = [w - 2, 0].max inner_h = [height - 2, 0].max # Render children content_lines = [] @children.each do |child| content_lines.concat(child.render(width: inner_w, height: inner_h).split("\n", -1)) end # Pad or truncate to inner height content_lines = content_lines[0, inner_h] if content_lines.length > inner_h while content_lines.length < inner_h content_lines << "" end border_chars = resolve_border top_line = build_top(border_chars, inner_w) bottom_line = "#{border_chars[:bl]}#{"#{border_chars[:h]}" * inner_w}#{border_chars[:br]}" body = content_lines.map do |line| padded = line.length < inner_w ? "#{line}#{" " * (inner_w - line.length)}" : line[0, inner_w] "#{border_chars[:v]}#{padded}#{border_chars[:v]}" end [top_line, *body, bottom_line].join("\n") end |