Class: Terminal::Ansi::ScreenViewer
- Inherits:
-
Object
- Object
- Terminal::Ansi::ScreenViewer
- Defined in:
- lib/terminal/ansi/screen_viewer.rb
Overview
A scrollable text viewer for the terminal.
Renders word-wrapped content and supports paging, line-by-line scrolling, and terminal resize. Intended for use with the alternate screen buffer.
Instance Attribute Summary collapse
-
#columns ⇒ Integer
Total number of terminal columns.
-
#line_bottom ⇒ Integer
readonly
Index of the line just below the last visible line.
-
#line_count ⇒ Integer?
readonly
Total number of lines.
-
#line_top ⇒ Integer
readonly
Index of the topmost visible line.
-
#rows ⇒ Integer
Total number of terminal rows.
-
#rows_visible ⇒ Integer
readonly
Number of rows available for content (rows minus footer).
Instance Method Summary collapse
-
#begin ⇒ self?
Scroll to the beginning of the content.
-
#down(count = 1) ⇒ self?
Scroll down by the given number of lines.
-
#draw ⇒ self
Render the current viewport to the terminal.
-
#end ⇒ self?
Scroll to the end of the content.
-
#half_down ⇒ self?
Scroll down by half a page.
-
#half_up ⇒ self?
Scroll up by half a page.
-
#initialize(lines, footer_rows: 0, bbcode: true) ⇒ ScreenViewer
constructor
Create a new screen viewer.
-
#page_down ⇒ self?
Scroll down by one full page.
-
#page_up ⇒ self?
Scroll up by one full page.
-
#resize(rows, columns) ⇒ self?
Resize to specific dimensions (clamped to terminal bounds) and redraw.
-
#resize_to_screen ⇒ self?
Resize to the current terminal dimensions and redraw.
-
#up(count = 1) ⇒ self?
Scroll up by the given number of lines.
Constructor Details
#initialize(lines, footer_rows: 0, bbcode: true) ⇒ ScreenViewer
Create a new screen viewer.
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 179 def initialize(lines, footer_rows: 0, bbcode: true) @footer_rows = .to_i @rows, @columns = Terminal.size @rows_visible = @rows - @footer_rows @fmt = Text::Formatter.new( *lines, bbcode: bbcode, ansi: true, eol: true, spaces: true ) end |
Instance Attribute Details
#columns ⇒ Integer
Total number of terminal columns.
40 41 42 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 40 def columns @columns end |
#line_bottom ⇒ Integer (readonly)
Index of the line just below the last visible line.
67 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 67 def line_bottom = (@line_top + @rows_visible if @line_top) |
#line_count ⇒ Integer? (readonly)
Total number of lines.
56 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 56 def line_count = @buf&.size |
#line_top ⇒ Integer (readonly)
Index of the topmost visible line.
61 62 63 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 61 def line_top @line_top end |
#rows ⇒ Integer
Total number of terminal rows.
30 31 32 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 30 def rows @rows end |
#rows_visible ⇒ Integer (readonly)
Number of rows available for content (rows minus footer).
50 51 52 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 50 def rows_visible @rows_visible end |
Instance Method Details
#begin ⇒ self?
Scroll to the beginning of the content.
103 104 105 106 107 108 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 103 def begin return draw unless @buf return if @line_top == 0 @line_top = 0 draw end |
#down(count = 1) ⇒ self?
Scroll down by the given number of lines.
143 144 145 146 147 148 149 150 151 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 143 def down(count = 1) return draw unless @buf lidx = @line_top + @rows_visible return if (count = [count, @buf.size - lidx].min) <= 0 @line_top += count Terminal.raw_write(Ansi.screen_scroll_up(count)) Terminal.raw_write(Ansi.cursor_pos(@rows_visible - count + 1)) _draw(lidx, count) end |
#draw ⇒ self
Render the current viewport to the terminal.
90 91 92 93 94 95 96 97 98 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 90 def draw recalculate unless @buf if @buf.empty? Terminal.raw_write(SCREEN_ERASE) return self end Terminal.raw_write(CURSOR_HOME) _draw(@line_top, @rows_visible) end |
#end ⇒ self?
Scroll to the end of the content.
113 114 115 116 117 118 119 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 113 def end return draw unless @buf idx = @buf.size - @rows_visible return if idx < 0 || @line_top == idx @line_top = idx draw end |
#half_down ⇒ self?
Scroll down by half a page.
171 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 171 def half_down = down(@rows_visible / 2) |
#half_up ⇒ self?
Scroll up by half a page.
161 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 161 def half_up = up(@rows_visible / 2) |
#page_down ⇒ self?
Scroll down by one full page.
166 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 166 def page_down = down(@rows_visible) |
#page_up ⇒ self?
Scroll up by one full page.
156 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 156 def page_up = up(@rows_visible) |
#resize(rows, columns) ⇒ self?
Resize to specific dimensions (clamped to terminal bounds) and redraw.
80 81 82 83 84 85 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 80 def resize(rows, columns) _resize( rows.clamp(1, Terminal.rows), columns.clamp(1, Terminal.columns) ) end |
#resize_to_screen ⇒ self?
Resize to the current terminal dimensions and redraw.
72 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 72 def resize_to_screen = _resize(*Terminal.size) |
#up(count = 1) ⇒ self?
Scroll up by the given number of lines.
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/terminal/ansi/screen_viewer.rb', line 125 def up(count = 1) return draw unless @buf return if @line_top == 0 if (nidx = @line_top - count) < 0 count = @line_top @line_top = 0 else @line_top = nidx end Terminal.raw_write(Ansi.screen_scroll_down(count)) Terminal.raw_write(CURSOR_HOME) _draw(@line_top, count) end |