class Chamomile::Frame
Declarative view DSL. Build a widget tree from view, the renderer calls frame.render(width:, height:) to produce the final string.
def view Chamomile::Frame.build do |f| f.horizontal do |left, right| left.panel("Routes") { left.text @routes } right.panel("Detail") { right.text @detail } end f.status_bar @status end end
Attributes
Public Class Methods
Source
# File lib/chamomile/frame.rb, line 19 def self.build(&block) frame = new block.call(frame) if block frame end
Public Instance Methods
Source
# File lib/chamomile/frame.rb, line 38 def horizontal(&block) layout = Widgets::HorizontalLayout.new block.call(layout) if block @children << layout layout end
Add a horizontal layout container.
Source
# File lib/chamomile/frame.rb, line 46 def panel(title = nil, border: :rounded, width: nil, &block) p = Widgets::Panel.new(title: title, border: border, width: width) block.call(p) if block @children << p p end
Add a bordered panel with a title.
Source
# File lib/chamomile/frame.rb, line 68 def render(width:, height:) return "" if @children.empty? if @children.length == 1 @children[0].render(width: width, height: height) else # Default: stack children vertically lines = [] remaining_height = height @children.each_with_index do |child, i| child_height = if i == @children.length - 1 remaining_height else [remaining_height / (@children.length - i), 1].max end lines << child.render(width: width, height: child_height) remaining_height -= child_height end lines.join("\n") end end
Render the frame to a string.
Source
# File lib/chamomile/frame.rb, line 61 def status_bar(content) sb = Widgets::StatusBar.new(content: content.to_s) @children << sb sb end
Add a status bar (fixed bottom line).
Source
# File lib/chamomile/frame.rb, line 54 def text(content) t = Widgets::Text.new(content: content.to_s) @children << t t end
Add a plain text block.
Source
# File lib/chamomile/frame.rb, line 30 def vertical(&block) layout = Widgets::VerticalLayout.new block.call(layout) if block @children << layout layout end
Add a vertical layout container.