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.



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

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
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.



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

def width
  @width
end

Instance Method Details

#endObject



44
45
46
47
48
49
50
# File 'lib/ruby_rich/panel.rb', line 44

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

#homeObject



39
40
41
42
# File 'lib/ruby_rich/panel.rb', line 39

def home
  @line_pos = 0
  @content_changed = false
end

#inner_widthObject



16
17
18
# File 'lib/ruby_rich/panel.rb', line 16

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

#page_downObject



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

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



20
21
22
23
24
25
26
# File 'lib/ruby_rich/panel.rb', line 20

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

#renderObject



52
53
54
55
56
57
58
59
60
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
# File 'lib/ruby_rich/panel.rb', line 52

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