Class: Rich::Panel
- Inherits:
-
Object
- Object
- Rich::Panel
- Defined in:
- lib/rich/panel.rb
Overview
A bordered panel container for content
Instance Attribute Summary collapse
-
#border_style ⇒ Style?
readonly
Border style.
-
#box ⇒ Box
readonly
Box style.
-
#content ⇒ Object
readonly
Content to display.
-
#expand ⇒ Boolean
readonly
Expand to fill width.
-
#padding ⇒ Integer
readonly
Padding inside panel.
-
#subtitle ⇒ String?
readonly
Panel subtitle.
-
#subtitle_align ⇒ Symbol
readonly
Subtitle alignment.
-
#subtitle_style ⇒ Style?
readonly
Subtitle style.
-
#title ⇒ String?
readonly
Panel title.
-
#title_align ⇒ Symbol
readonly
Title alignment.
-
#title_style ⇒ Style?
readonly
Title style.
-
#width ⇒ Integer?
readonly
Fixed width.
Class Method Summary collapse
-
.fit(content, title: nil) ⇒ Panel
Create a simple panel.
Instance Method Summary collapse
-
#initialize(content, title: nil, subtitle: nil, box: Box::ROUNDED, border_style: nil, title_style: nil, subtitle_style: nil, expand: true, padding: 1, width: nil, title_align: :center, subtitle_align: :center) ⇒ Panel
constructor
A new instance of Panel.
-
#print_to(console) ⇒ Object
Print panel to console.
-
#render(max_width: 80, color_system: ColorSystem::TRUECOLOR) ⇒ String
Render panel to string with ANSI codes.
-
#to_segments(max_width: 80) ⇒ Array<Segment>
Render panel to segments.
Constructor Details
#initialize(content, title: nil, subtitle: nil, box: Box::ROUNDED, border_style: nil, title_style: nil, subtitle_style: nil, expand: true, padding: 1, width: nil, title_align: :center, subtitle_align: :center) ⇒ Panel
Returns a new instance of Panel.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rich/panel.rb', line 48 def initialize( content, title: nil, subtitle: nil, box: Box::ROUNDED, border_style: nil, title_style: nil, subtitle_style: nil, expand: true, padding: 1, width: nil, title_align: :center, subtitle_align: :center ) @content = content @title = title @subtitle = subtitle @box = box @border_style = border_style.is_a?(String) ? Style.parse(border_style) : border_style @title_style = title_style.is_a?(String) ? Style.parse(title_style) : title_style @subtitle_style = subtitle_style.is_a?(String) ? Style.parse(subtitle_style) : subtitle_style @expand = @padding = padding @width = width @title_align = title_align @subtitle_align = subtitle_align end |
Instance Attribute Details
#border_style ⇒ Style? (readonly)
Returns Border style.
25 26 27 |
# File 'lib/rich/panel.rb', line 25 def border_style @border_style end |
#box ⇒ Box (readonly)
Returns Box style.
22 23 24 |
# File 'lib/rich/panel.rb', line 22 def box @box end |
#content ⇒ Object (readonly)
Returns Content to display.
13 14 15 |
# File 'lib/rich/panel.rb', line 13 def content @content end |
#expand ⇒ Boolean (readonly)
Returns Expand to fill width.
34 35 36 |
# File 'lib/rich/panel.rb', line 34 def @expand end |
#padding ⇒ Integer (readonly)
Returns Padding inside panel.
37 38 39 |
# File 'lib/rich/panel.rb', line 37 def padding @padding end |
#subtitle ⇒ String? (readonly)
Returns Panel subtitle.
19 20 21 |
# File 'lib/rich/panel.rb', line 19 def subtitle @subtitle end |
#subtitle_align ⇒ Symbol (readonly)
Returns Subtitle alignment.
46 47 48 |
# File 'lib/rich/panel.rb', line 46 def subtitle_align @subtitle_align end |
#subtitle_style ⇒ Style? (readonly)
Returns Subtitle style.
31 32 33 |
# File 'lib/rich/panel.rb', line 31 def subtitle_style @subtitle_style end |
#title ⇒ String? (readonly)
Returns Panel title.
16 17 18 |
# File 'lib/rich/panel.rb', line 16 def title @title end |
#title_align ⇒ Symbol (readonly)
Returns Title alignment.
43 44 45 |
# File 'lib/rich/panel.rb', line 43 def title_align @title_align end |
#title_style ⇒ Style? (readonly)
Returns Title style.
28 29 30 |
# File 'lib/rich/panel.rb', line 28 def title_style @title_style end |
#width ⇒ Integer? (readonly)
Returns Fixed width.
40 41 42 |
# File 'lib/rich/panel.rb', line 40 def width @width end |
Class Method Details
.fit(content, title: nil) ⇒ Panel
Create a simple panel
136 137 138 |
# File 'lib/rich/panel.rb', line 136 def fit(content, title: nil) new(content, title: title, expand: false) end |
Instance Method Details
#print_to(console) ⇒ Object
Print panel to console
125 126 127 128 129 |
# File 'lib/rich/panel.rb', line 125 def print_to(console) rendered = render(max_width: console.width, color_system: console.color_system) console.write(rendered) console.write("\n") end |
#render(max_width: 80, color_system: ColorSystem::TRUECOLOR) ⇒ String
Render panel to string with ANSI codes
119 120 121 |
# File 'lib/rich/panel.rb', line 119 def render(max_width: 80, color_system: ColorSystem::TRUECOLOR) Segment.render(to_segments(max_width: max_width), color_system: color_system) end |
#to_segments(max_width: 80) ⇒ Array<Segment>
Render panel to segments
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rich/panel.rb', line 79 def to_segments(max_width: 80) segments = [] inner_width = calculate_inner_width(max_width) content_width = [inner_width - @padding * 2, 0].max # Render content to lines content_lines = render_content(content_width) # Top border with title segments.concat(render_top_border(inner_width)) segments << Segment.new("\n") # Padding top @padding.times do segments.concat(render_empty_row(inner_width)) segments << Segment.new("\n") end # Content rows content_lines.each do |line| segments.concat(render_content_row(line, inner_width)) segments << Segment.new("\n") end # Padding bottom @padding.times do segments.concat(render_empty_row(inner_width)) segments << Segment.new("\n") end # Bottom border with subtitle segments.concat(render_bottom_border(inner_width)) segments end |