Class: TTYString::Screen
- Inherits:
-
Object
- Object
- TTYString::Screen
- Defined in:
- lib/tty_string/screen.rb
Overview
a grid to draw on
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
Instance Method Summary collapse
- #[]=(row, col, value) ⇒ Object
- #clear ⇒ Object
- #clear_at_cursor ⇒ Object
- #clear_backward ⇒ Object
- #clear_forward ⇒ Object
- #clear_line ⇒ Object
- #clear_line_backward ⇒ Object
- #clear_line_forward ⇒ Object
- #clear_lines_after ⇒ Object
- #clear_lines_before ⇒ Object
- #ensure_row ⇒ Object
-
#initialize(initial_style:) ⇒ Screen
constructor
A new instance of Screen.
- #scroll_down ⇒ Object
- #scroll_up ⇒ Object
- #style(style_codes) ⇒ Object
-
#to_s ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
- #write(string) ⇒ Object
Constructor Details
Instance Attribute Details
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
11 12 13 |
# File 'lib/tty_string/screen.rb', line 11 def cursor @cursor end |
Instance Method Details
#[]=(row, col, value) ⇒ Object
43 44 45 46 |
# File 'lib/tty_string/screen.rb', line 43 def []=((row, col), value) screen[row] ||= Row.new(newline_style: current_style) screen[row][col] = value end |
#clear ⇒ Object
64 65 66 |
# File 'lib/tty_string/screen.rb', line 64 def clear @screen = [] end |
#clear_at_cursor ⇒ Object
48 49 50 |
# File 'lib/tty_string/screen.rb', line 48 def clear_at_cursor self[cursor] = Cell.new(' ', style: current_style) end |
#clear_backward ⇒ Object
86 87 88 89 |
# File 'lib/tty_string/screen.rb', line 86 def clear_backward clear_line_backward clear_lines_before end |
#clear_forward ⇒ Object
91 92 93 94 |
# File 'lib/tty_string/screen.rb', line 91 def clear_forward clear_lines_after clear_line_forward end |
#clear_line ⇒ Object
60 61 62 |
# File 'lib/tty_string/screen.rb', line 60 def clear_line screen[row] = Row.new(newline_style: current_style) end |
#clear_line_backward ⇒ Object
56 57 58 |
# File 'lib/tty_string/screen.rb', line 56 def clear_line_backward screen[row].fill(Cell.new(' ', style: current_style), 0..col) end |
#clear_line_forward ⇒ Object
52 53 54 |
# File 'lib/tty_string/screen.rb', line 52 def clear_line_forward screen[row].slice!(col..-1) end |
#clear_lines_after ⇒ Object
82 83 84 |
# File 'lib/tty_string/screen.rb', line 82 def clear_lines_after screen.slice!((row + 1)..-1) end |
#clear_lines_before ⇒ Object
78 79 80 |
# File 'lib/tty_string/screen.rb', line 78 def clear_lines_before screen.fill(Row.new(newline_style: current_style), 0...row) end |
#ensure_row ⇒ Object
96 97 98 |
# File 'lib/tty_string/screen.rb', line 96 def ensure_row screen[row] ||= Row.new(newline_style: current_style) end |
#scroll_down ⇒ Object
73 74 75 76 |
# File 'lib/tty_string/screen.rb', line 73 def scroll_down screen.unshift(Row.new(newline_style: current_style)) screen.pop end |
#scroll_up ⇒ Object
68 69 70 71 |
# File 'lib/tty_string/screen.rb', line 68 def scroll_up screen.push(Row.new(newline_style: current_style)) screen.shift end |
#style(style_codes) ⇒ Object
107 108 109 |
# File 'lib/tty_string/screen.rb', line 107 def style(style_codes) self.current_style = current_style.new(style_codes) end |
#to_s ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tty_string/screen.rb', line 19 def to_s # rubocop:disable Metrics/MethodLength, Metrics/AbcSize style_context = initial_style str = +'' screen.each_with_index do |row, index| unless index.zero? str << row.newline_style.to_s(context: style_context) if row str << "\n" style_context = row.newline_style if row end Array(row).each do |cell| if cell str << cell.style.to_s(context: style_context) str << cell.value style_context = cell.style else str << ' ' end end end str << current_style.to_s(context: style_context) str end |