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
- #clear ⇒ Object
- #console_cursor ⇒ Object
- #cursor ⇒ Object
- #flush ⇒ Object
- #flush_debug(str = "Window") ⇒ Object
-
#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
#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 |
#flush ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/termplot/window.rb', line 38 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(str = "Window") ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/termplot/window.rb', line 50 def flush_debug(str = "Window") padding = "-" * 10 puts "\n#{padding} #{str} #{padding}\n" 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 print render_line puts end end |
#print_errors(errors) ⇒ Object
TODO: Refine later and include errors properly in the window
63 64 65 66 67 |
# File 'lib/termplot/window.rb', line 63 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 |