class Sortviz::Cursor
Cursor
represents a terminal cursor, can easily switch between different windows (curses) and can remember the last cursor position it was at. This is to wrap up the boilerplate code of moving a cursor around a terminal window
Attributes
Public Class Methods
Initialize a new Cursor
object, follows curses practice of passing the y before the x coords.
Params:
window
-
Curses::Window
The window that will receive input origin
-
Hash
Origins to start drawing at
# File lib/sortviz/cursor.rb, line 15 def initialize(window, origin) @window = window move(origin[:y], origin[:x]) end
Public Instance Methods
Cache the current cursor coordinates
# File lib/sortviz/cursor.rb, line 63 def cache @cached = { y: @y, x: @x } end
Decrement the x value by val (Default: 1)
# File lib/sortviz/cursor.rb, line 57 def decr_x(val = 1) update(y, (x - val).abs) apply_pos end
Decrement the y value by val (Default: 1)
# File lib/sortviz/cursor.rb, line 45 def decr_y(val = 1) update((y - val).abs, x) apply_pos end
Increment the x value by val (Default: 1)
# File lib/sortviz/cursor.rb, line 51 def incr_x(val = 1) update(y, x + val) apply_pos end
Increment the y value by val (Default: 1)
# File lib/sortviz/cursor.rb, line 39 def incr_y(val = 1) update(y + val, x) apply_pos end
Move to positions y, x
# File lib/sortviz/cursor.rb, line 21 def move(y, x) update(y, x) apply_pos end
Move along the x-axis
# File lib/sortviz/cursor.rb, line 33 def move_x(val) update(y, val) apply_pos end
Move along the y-axis
# File lib/sortviz/cursor.rb, line 27 def move_y(val) update(val, x) apply_pos end
Increment y to simulate the addition of a new line
# File lib/sortviz/cursor.rb, line 87 def newline incr_y end
Restore the previously cached cursor coordinates if any are cached
# File lib/sortviz/cursor.rb, line 68 def restore return unless @cached update(@cached[:y], @cached[:x]) @cached = nil end
Switch to a new window that will receive input and optionally move to new coordinates in the new window
# File lib/sortviz/cursor.rb, line 76 def switch_window(new_window, coords: {}) @window = new_window move(coords[:y], coords[:x]) unless coords.empty? end
Print to the current screen receiving input
# File lib/sortviz/cursor.rb, line 82 def tprint(string) @window.addstr(string) end
Private Instance Methods
# File lib/sortviz/cursor.rb, line 96 def apply_pos window.setpos(@y, @x) end
# File lib/sortviz/cursor.rb, line 92 def update(y, x) @y, @x = y, x end