Class: Kdep::Dashboard::Panel
- Inherits:
-
Object
- Object
- Kdep::Dashboard::Panel
- Defined in:
- lib/kdep/dashboard/panel.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#rect ⇒ Object
readonly
Returns the value of attribute rect.
-
#scroll_offset ⇒ Object
readonly
Returns the value of attribute scroll_offset.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_line(line) ⇒ Object
-
#initialize(title:, rect:) ⇒ Panel
constructor
A new instance of Panel.
- #render ⇒ Object
- #scroll_down ⇒ Object
- #scroll_up ⇒ Object
- #update(lines) ⇒ Object
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
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
4 5 6 |
# File 'lib/kdep/dashboard/panel.rb', line 4 def lines @lines end |
#rect ⇒ Object (readonly)
Returns the value of attribute rect.
4 5 6 |
# File 'lib/kdep/dashboard/panel.rb', line 4 def rect @rect end |
#scroll_offset ⇒ Object (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 |
#title ⇒ Object (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 |
#render ⇒ Object
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} " = [w - 2, 0].max if title_text.length > title_text = title_text[0, ] end dashes = - 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 << "+" + ("-" * ) + "+" output end |
#scroll_down ⇒ Object
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_up ⇒ Object
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 |