Class: Termplot::Window
- Inherits:
-
Object
- Object
- Termplot::Window
- Defined in:
- lib/termplot/window.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #blit(other, start_row, start_col) ⇒ Object
- #clear ⇒ Object
- #console_cursor ⇒ Object
- #cursor ⇒ Object
- #each_row ⇒ Object
-
#flush ⇒ Object
Flush rendered window to a string.
-
#flush_debug ⇒ Object
Flush to 2d array rather than string.
-
#initialize(cols:, rows:) ⇒ Window
constructor
A new instance of Window.
-
#print_errors(errors) ⇒ Object
TODO: Refine later and include errors properly in the window.
- #size ⇒ Object
- #write(char) ⇒ Object
Constructor Details
#initialize(cols:, rows:) ⇒ Window
Returns a new instance of Window.
7 8 9 10 11 |
# File 'lib/termplot/window.rb', line 7 def initialize(cols:, rows:) @rows = rows @cols = cols @buffer = Array.new(cols * rows) { CharacterMap::DEFAULT[:empty] } end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
6 7 8 |
# File 'lib/termplot/window.rb', line 6 def buffer @buffer end |
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
6 7 8 |
# File 'lib/termplot/window.rb', line 6 def cols @cols end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
6 7 8 |
# File 'lib/termplot/window.rb', line 6 def rows @rows end |
Instance Method Details
#blit(other, start_row, start_col) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/termplot/window.rb', line 71 def blit(other, start_row, start_col) cursor.position = start_row * cols + start_col other.each_row do |row| row.each do |char| write(char) end cursor.down unless cursor.beginning_of_line? cursor.col = start_col end end |
#clear ⇒ Object
33 34 35 36 |
# File 'lib/termplot/window.rb', line 33 def clear cursor.reset_position size.times { write CharacterMap::DEFAULT[:empty] } end |
#console_cursor ⇒ Object
17 18 19 20 21 22 |
# File 'lib/termplot/window.rb', line 17 def console_cursor # Console buffer has an extra rows - 1 to account for new line characters # between rows @console_cursor ||= Termplot::Cursors::BufferedConsoleCursor.new(self, Array.new(cols * rows + rows - 1)) end |
#cursor ⇒ Object
13 14 15 |
# File 'lib/termplot/window.rb', line 13 def cursor @cursor ||= Termplot::Cursors::VirtualCursor.new(self) end |
#each_row ⇒ Object
83 84 85 86 87 |
# File 'lib/termplot/window.rb', line 83 def each_row buffer.each_slice(cols) do |row| yield row end end |
#flush ⇒ Object
Flush rendered window to a string
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/termplot/window.rb', line 39 def flush console_cursor.clear_buffer console_cursor.reset_position buffer.each_slice(cols).with_index do |line, i| line.each do |v| console_cursor.write(v) end console_cursor.new_line end console_cursor.flush end |
#flush_debug ⇒ Object
Flush to 2d array rather than string
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/termplot/window.rb', line 52 def flush_debug debug_arr = [] buffer.each_slice(cols).with_index do |line, y| render_line = line.each_with_index.map do |c, x| y * cols + x == cursor.position ? "𝥺" : c end debug_arr << render_line debug_arr << "\n" end debug_arr end |
#print_errors(errors) ⇒ Object
TODO: Refine later and include errors properly in the window
65 66 67 68 69 |
# File 'lib/termplot/window.rb', line 65 def print_errors(errors) print errors.join(Termplot::ControlChars::NEWLINE) print Termplot::ControlChars::NEWLINE errors.length.times { print Termplot::ControlChars::UP } end |
#size ⇒ Object
24 25 26 |
# File 'lib/termplot/window.rb', line 24 def size rows * cols end |
#write(char) ⇒ Object
28 29 30 31 |
# File 'lib/termplot/window.rb', line 28 def write(char) buffer[cursor.position] = char cursor.write(char) end |