Class: RubyRich::Panel

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/panel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = "", title: nil, border_style: :white, title_align: :center) ⇒ Panel

Returns a new instance of Panel.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ruby_rich/panel.rb', line 7

def initialize(content = "", title: nil, border_style: :white, title_align: :center)
  @content = content
  @title = title
  @border_style = border_style
  @width = 0
  @height = 0
  @line_pos = 0
  @title_align = title_align
  @wrapped_content_cache_key = nil
  @wrapped_content_cache = nil
end

Instance Attribute Details

#border_styleObject

Returns the value of attribute border_style.



3
4
5
# File 'lib/ruby_rich/panel.rb', line 3

def border_style
  @border_style
end

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/ruby_rich/panel.rb', line 3

def content
  @content
end

#content_changedObject

Returns the value of attribute content_changed.



4
5
6
# File 'lib/ruby_rich/panel.rb', line 4

def content_changed
  @content_changed
end

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/ruby_rich/panel.rb', line 3

def height
  @height
end

#line_posObject

Returns the value of attribute line_pos.



3
4
5
# File 'lib/ruby_rich/panel.rb', line 3

def line_pos
  @line_pos
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/ruby_rich/panel.rb', line 3

def title
  @title
end

#title_alignObject

Returns the value of attribute title_align.



4
5
6
# File 'lib/ruby_rich/panel.rb', line 4

def title_align
  @title_align
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/ruby_rich/panel.rb', line 5

def width
  @width
end

Instance Method Details

#endObject



53
54
55
56
57
58
59
# File 'lib/ruby_rich/panel.rb', line 53

def end
  unless @content.empty?
    content_lines = wrap_content(@content)
    @line_pos = content_lines.size - @height + 2
    @content_changed = false
  end
end

#homeObject



48
49
50
51
# File 'lib/ruby_rich/panel.rb', line 48

def home
  @line_pos = 0
  @content_changed = false
end

#inner_widthObject



25
26
27
# File 'lib/ruby_rich/panel.rb', line 25

def inner_width
  @width - 2  # Account for border characters
end

#page_downObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_rich/panel.rb', line 37

def page_down
  unless @content.empty?
    content_lines = wrap_content(@content)
    @line_pos += ( @height - 4 )
    if @line_pos + ( @height - 4 ) > content_lines.size
      @line_pos = content_lines.size - @height + 2
    end
    @content_changed = false
  end
end

#page_upObject



29
30
31
32
33
34
35
# File 'lib/ruby_rich/panel.rb', line 29

def page_up
  @line_pos -= ( @height - 4 )
  if @line_pos < 0
    @line_pos = 0
  end
  @content_changed = false
end

#renderObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby_rich/panel.rb', line 61

def render
  lines = []
  color_code = AnsiCode.color(@border_style) || AnsiCode.color(:white)
  reset_code = AnsiCode.reset

  # Top border
  top_border = color_code + ""
  if @title
    title_text = "[ #{@title} ]"
    bar_width = @width - @title.display_width-6
    case @title_align
    when :left
      top_border += title_text + '' * bar_width
    when :center
      top_border += '' * (bar_width/2)  + title_text + '' * (bar_width - bar_width/2)
    when :right
      top_border += '' * bar_width + title_text
    end
  else
    top_border += '' * (@width - 2)
  end
  top_border += "" + reset_code
  lines << top_border

  # Content area
  content_lines = wrap_content(@content)
  if @line_pos==0
    if @content_changed == false
      if content_lines.size > @height - 2
        content_lines=content_lines[0..@height - 3]
      end
    else
      if content_lines.size > @height - 2
        @line_pos = content_lines.size - @height + 2
        content_lines=content_lines[@line_pos..-1]
        @content_changed = false
      end
    end
  else
    if @line_pos+@height-2 >= content_lines.size
      content_lines=content_lines[@line_pos..-1]
    else
      content_lines=content_lines[@line_pos..@line_pos+@height-3]
    end
  end
  
  content_lines.each do |line|
    lines << color_code + "" + reset_code +
            line + " "*(@width - line.display_width - 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