Class: Thor::Interactive::TUI::OutputBuffer
- Inherits:
-
Object
- Object
- Thor::Interactive::TUI::OutputBuffer
- Defined in:
- lib/thor/interactive/tui/output_buffer.rb
Overview
Stores captured command output with scrollback support. Each entry is a hash with :text and optional :style keys.
Constant Summary collapse
- DEFAULT_MAX_LINES =
10_000
Instance Attribute Summary collapse
-
#scroll_offset ⇒ Object
readonly
Returns the value of attribute scroll_offset.
Instance Method Summary collapse
- #append(text, style: nil) ⇒ Object
- #at_bottom? ⇒ Boolean
- #clear ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(max_lines: DEFAULT_MAX_LINES) ⇒ OutputBuffer
constructor
A new instance of OutputBuffer.
- #line_count ⇒ Object
- #lines ⇒ Object
- #scroll_down(amount = 1) ⇒ Object
- #scroll_to_bottom ⇒ Object
- #scroll_to_top ⇒ Object
- #scroll_up(amount = 1) ⇒ Object
-
#visible_lines(viewport_height) ⇒ Object
Returns lines visible in a viewport of given height, accounting for scroll_offset (0 = bottom, positive = scrolled up).
Constructor Details
#initialize(max_lines: DEFAULT_MAX_LINES) ⇒ OutputBuffer
Returns a new instance of OutputBuffer.
13 14 15 16 17 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 13 def initialize(max_lines: DEFAULT_MAX_LINES) @lines = [] @max_lines = max_lines @scroll_offset = 0 end |
Instance Attribute Details
#scroll_offset ⇒ Object (readonly)
Returns the value of attribute scroll_offset.
11 12 13 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 11 def scroll_offset @scroll_offset end |
Instance Method Details
#append(text, style: nil) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 19 def append(text, style: nil) text.to_s.split("\n", -1).each do |line| @lines << {text: line, style: style} end trim_to_max # Auto-scroll to bottom when new content arrives @scroll_offset = 0 end |
#at_bottom? ⇒ Boolean
75 76 77 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 75 def at_bottom? @scroll_offset == 0 end |
#clear ⇒ Object
40 41 42 43 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 40 def clear @lines.clear @scroll_offset = 0 end |
#empty? ⇒ Boolean
36 37 38 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 36 def empty? @lines.empty? end |
#line_count ⇒ Object
32 33 34 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 32 def line_count @lines.length end |
#lines ⇒ Object
28 29 30 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 28 def lines @lines.dup end |
#scroll_down(amount = 1) ⇒ Object
63 64 65 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 63 def scroll_down(amount = 1) @scroll_offset = [@scroll_offset - amount, 0].max end |
#scroll_to_bottom ⇒ Object
67 68 69 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 67 def scroll_to_bottom @scroll_offset = 0 end |
#scroll_to_top ⇒ Object
71 72 73 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 71 def scroll_to_top @scroll_offset = [@lines.length - 1, 0].max end |
#scroll_up(amount = 1) ⇒ Object
58 59 60 61 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 58 def scroll_up(amount = 1) max_offset = [@lines.length - 1, 0].max @scroll_offset = [@scroll_offset + amount, max_offset].min end |
#visible_lines(viewport_height) ⇒ Object
Returns lines visible in a viewport of given height, accounting for scroll_offset (0 = bottom, positive = scrolled up).
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/thor/interactive/tui/output_buffer.rb', line 47 def visible_lines() return @lines.last() if @scroll_offset == 0 end_index = @lines.length - @scroll_offset end_index = @lines.length if end_index > @lines.length start_index = end_index - start_index = 0 if start_index < 0 @lines[start_index...end_index] || [] end |