Class: RubyRich::Panel
- Inherits:
-
Object
- Object
- RubyRich::Panel
- Defined in:
- lib/ruby_rich/panel.rb
Constant Summary collapse
- COLORS =
{ red: "\e[31m", green: "\e[32m", blue: "\e[34m", yellow: "\e[33m", cyan: "\e[36m", white: "\e[37m", reset: "\e[0m" }
Instance Attribute Summary collapse
-
#border_style ⇒ Object
Returns the value of attribute border_style.
-
#content ⇒ Object
Returns the value of attribute content.
-
#height ⇒ Object
Returns the value of attribute height.
-
#line_pos ⇒ Object
Returns the value of attribute line_pos.
-
#title ⇒ Object
Returns the value of attribute title.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #calculate_string_width(str) ⇒ Object
-
#initialize(content = "", title: nil, border_style: :white) ⇒ Panel
constructor
A new instance of Panel.
- #inner_width ⇒ Object
- #render ⇒ Object
- #update_content(new_content) ⇒ Object
Constructor Details
#initialize(content = "", title: nil, border_style: :white) ⇒ Panel
Returns a new instance of Panel.
14 15 16 17 18 19 20 21 |
# File 'lib/ruby_rich/panel.rb', line 14 def initialize(content = "", title: nil, border_style: :white) @content = content @title = title @border_style = border_style @width = 0 @height = 0 @line_pos = 0 end |
Instance Attribute Details
#border_style ⇒ Object
Returns the value of attribute border_style.
3 4 5 |
# File 'lib/ruby_rich/panel.rb', line 3 def border_style @border_style end |
#content ⇒ Object
Returns the value of attribute content.
3 4 5 |
# File 'lib/ruby_rich/panel.rb', line 3 def content @content end |
#height ⇒ Object
Returns the value of attribute height.
3 4 5 |
# File 'lib/ruby_rich/panel.rb', line 3 def height @height end |
#line_pos ⇒ Object
Returns the value of attribute line_pos.
3 4 5 |
# File 'lib/ruby_rich/panel.rb', line 3 def line_pos @line_pos end |
#title ⇒ Object
Returns the value of attribute title.
3 4 5 |
# File 'lib/ruby_rich/panel.rb', line 3 def title @title end |
#width ⇒ Object
Returns the value of attribute width.
3 4 5 |
# File 'lib/ruby_rich/panel.rb', line 3 def width @width end |
Instance Method Details
#calculate_string_width(str) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/ruby_rich/panel.rb', line 27 def calculate_string_width(str) width = 0 str.each_char do |char| width += Unicode::DisplayWidth.of(char) end width end |
#inner_width ⇒ Object
23 24 25 |
# File 'lib/ruby_rich/panel.rb', line 23 def inner_width @width - 2 # Account for border characters end |
#render ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 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 75 |
# File 'lib/ruby_rich/panel.rb', line 35 def render lines = [] color_code = COLORS[@border_style] || COLORS[:white] reset_code = COLORS[:reset] # Top border top_border = color_code + "┌" if @title title_text = "[ #{@title} ]" top_border += title_text + '─' * (@width - calculate_string_width(@title)-6) else top_border += '─' * (@width - 2) end top_border += "┐" + reset_code lines << top_border # Content area content_lines = wrap_content(@content) if content_lines.size > @height - 2 @line_pos = content_lines.size - @height + 2 content_lines=content_lines[@line_pos..-1] end content_lines.each do |line| lines << color_code + "│" + reset_code + line + " "*(@width - calculate_string_width(line) - 2) + color_code + "│" + reset_code end # Fill remaining vertical space remaining_lines = @height - 2 - content_lines.size remaining_lines.times do lines << color_code + "│" + reset_code + " " * (@width - 2) + color_code + "│" + reset_code end # Bottom border lines << color_code + "└" + "─" * (@width - 2) + "┘" + reset_code lines end |
#update_content(new_content) ⇒ Object
77 78 79 |
# File 'lib/ruby_rich/panel.rb', line 77 def update_content(new_content) @content = new_content end |