Class: Kdep::Dashboard::Panel

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

Direct Known Subclasses

HealthPanel, LogPanel, ResourcesPanel, RolloutPanel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, rect:) ⇒ Panel

Returns a new instance of Panel.



6
7
8
9
10
11
# File 'lib/kdep/dashboard/panel.rb', line 6

def initialize(title:, rect:)
  @title = title
  @rect = rect
  @lines = []
  @scroll_offset = 0
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



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

def lines
  @lines
end

#rectObject (readonly)

Returns the value of attribute rect.



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

def rect
  @rect
end

#scroll_offsetObject (readonly)

Returns the value of attribute scroll_offset.



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

def scroll_offset
  @scroll_offset
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_line(line) ⇒ Object



18
19
20
# File 'lib/kdep/dashboard/panel.rb', line 18

def add_line(line)
  @lines << line
end

#renderObject



31
32
33
34
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
# File 'lib/kdep/dashboard/panel.rb', line 31

def render
  output = []
  w = @rect.width

  # Title bar
  title_text = " #{@title} "
  bar_width = [w - 2, 0].max
  if title_text.length > bar_width
    title_text = title_text[0, bar_width]
  end
  dashes = bar_width - title_text.length
  left_dashes = dashes / 2
  right_dashes = dashes - left_dashes
  output << "+" + ("-" * left_dashes) + title_text + ("-" * right_dashes) + "+"

  # Content area
  content_height = @rect.height - 2  # minus title and bottom border
  content_width = [w - 4, 0].max  # minus "| " and " |"

  visible = content_lines
  content_height.times do |i|
    line = visible[i] || ""
    # Strip ANSI for length calculation, but keep ANSI in output
    display_len = strip_ansi(line).length
    if display_len > content_width
      line = truncate_with_ansi(line, content_width)
    elsif display_len < content_width
      line = line + (" " * (content_width - display_len))
    end
    output << "| " + line + " |"
  end

  # Bottom border
  output << "+" + ("-" * bar_width) + "+"

  output
end

#scroll_downObject



26
27
28
29
# File 'lib/kdep/dashboard/panel.rb', line 26

def scroll_down
  max = [visible_line_count, 0].max
  @scroll_offset = [@scroll_offset + 1, max].min
end

#scroll_upObject



22
23
24
# File 'lib/kdep/dashboard/panel.rb', line 22

def scroll_up
  @scroll_offset = [@scroll_offset - 1, 0].max
end

#update(lines) ⇒ Object



13
14
15
16
# File 'lib/kdep/dashboard/panel.rb', line 13

def update(lines)
  @lines = lines.dup
  clamp_scroll
end