Class: Termplot::Cursors::VirtualCursor

Inherits:
Object
  • Object
show all
Defined in:
lib/termplot/cursors/virtual_cursor.rb

Direct Known Subclasses

BufferedConsoleCursor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ VirtualCursor

Returns a new instance of VirtualCursor.



6
7
8
9
# File 'lib/termplot/cursors/virtual_cursor.rb', line 6

def initialize(window)
  @window = window
  @position = 0
end

Instance Attribute Details

#positionObject

Returns the value of attribute position.



4
5
6
# File 'lib/termplot/cursors/virtual_cursor.rb', line 4

def position
  @position
end

#windowObject (readonly)

Returns the value of attribute window.



4
5
6
# File 'lib/termplot/cursors/virtual_cursor.rb', line 4

def window
  @window
end

Instance Method Details

#back(n = 1) ⇒ Object



26
27
28
29
30
# File 'lib/termplot/cursors/virtual_cursor.rb', line 26

def back(n = 1)
  chars_to_move = [position, n].min
  @position -= chars_to_move
  chars_to_move
end

#beginning_of_lineObject



63
64
65
# File 'lib/termplot/cursors/virtual_cursor.rb', line 63

def beginning_of_line
  @position = position - (position % window.cols)
end

#beginning_of_line?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/termplot/cursors/virtual_cursor.rb', line 67

def beginning_of_line?
  @position % window.cols == 0
end

#colObject



43
44
45
# File 'lib/termplot/cursors/virtual_cursor.rb', line 43

def col
  position % window.cols
end

#col=(x) ⇒ Object



51
52
53
54
# File 'lib/termplot/cursors/virtual_cursor.rb', line 51

def col=(x)
  beginning_of_line
  forward(x)
end

#down(n = 1) ⇒ Object



56
57
58
59
60
61
# File 'lib/termplot/cursors/virtual_cursor.rb', line 56

def down(n=1)
  return 0 unless row < (window.rows - 1)
  rows_to_move = [n, window.rows - 1 - row].min
  @position += window.cols * rows_to_move
  rows_to_move
end

#forward(n = 1) ⇒ Object



19
20
21
22
23
24
# File 'lib/termplot/cursors/virtual_cursor.rb', line 19

def forward(n = 1)
  movable_chars = window.buffer.size - position
  chars_to_move = [movable_chars, n].min
  @position += chars_to_move
  chars_to_move
end

#reset_positionObject



75
76
77
78
79
# File 'lib/termplot/cursors/virtual_cursor.rb', line 75

def reset_position
  return if position == 0
  up(row) # Go up by row num times
  beginning_of_line
end

#rowObject



39
40
41
# File 'lib/termplot/cursors/virtual_cursor.rb', line 39

def row
  (position / window.cols).floor
end

#row=(y) ⇒ Object



47
48
49
# File 'lib/termplot/cursors/virtual_cursor.rb', line 47

def row=(y)
  @position = y * window.cols + col
end

#up(n = 1) ⇒ Object



32
33
34
35
36
37
# File 'lib/termplot/cursors/virtual_cursor.rb', line 32

def up(n=1)
  return unless row > 0
  rows_to_move = [n, row].min
  @position -= rows_to_move * window.cols
  rows_to_move
end

#write(char) ⇒ Object



11
12
13
# File 'lib/termplot/cursors/virtual_cursor.rb', line 11

def write(char)
  @position += 1 if writeable?
end

#writeable?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/termplot/cursors/virtual_cursor.rb', line 15

def writeable?
  position < window.buffer.size
end