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

window[R]
x[R]
y[R]

Public Class Methods

new(window, origin) click to toggle source

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() click to toggle source

Cache the current cursor coordinates

# File lib/sortviz/cursor.rb, line 63
def cache
  @cached = { y: @y, x: @x }
end
decr_x(val = 1) click to toggle source

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
decr_y(val = 1) click to toggle source

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
incr_x(val = 1) click to toggle source

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
incr_y(val = 1) click to toggle source

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(y, x) click to toggle source

Move to positions y, x

# File lib/sortviz/cursor.rb, line 21
def move(y, x)
  update(y, x)
  apply_pos
end
move_x(val) click to toggle source

Move along the x-axis

# File lib/sortviz/cursor.rb, line 33
def move_x(val)
  update(y, val)
  apply_pos
end
move_y(val) click to toggle source

Move along the y-axis

# File lib/sortviz/cursor.rb, line 27
def move_y(val)
  update(val, x)
  apply_pos
end
newline() click to toggle source

Increment y to simulate the addition of a new line

# File lib/sortviz/cursor.rb, line 87
def newline
  incr_y
end
restore() click to toggle source

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_window(new_window, coords: {}) click to toggle source

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
tprint(string) click to toggle source

Print to the current screen receiving input

# File lib/sortviz/cursor.rb, line 82
def tprint(string)
  @window.addstr(string)
end

Private Instance Methods

apply_pos() click to toggle source
# File lib/sortviz/cursor.rb, line 96
def apply_pos
  window.setpos(@y, @x)
end
update(y, x) click to toggle source
# File lib/sortviz/cursor.rb, line 92
def update(y, x)
  @y, @x = y, x
end