class Chamomile::Widgets::Panel
Bordered panel with optional title.
Attributes
Public Class Methods
Source
# File lib/chamomile/frame.rb, line 115 def initialize(title: nil, border: :rounded, width: nil) @title = title @border = border @width = width @children = [] end
Public Instance Methods
Source
# 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
Source
# File lib/chamomile/frame.rb, line 122 def text(content) t = Text.new(content: content.to_s) @children << t t end