Class: Yatte::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/yatte/cursor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ Cursor

Returns a new instance of Cursor.



7
8
9
10
11
12
13
# File 'lib/yatte/cursor.rb', line 7

def initialize(buffer)
  @buffer = buffer
  @row = 0
  @col = 0
  @row_offset = 0
  @col_offset = 0
end

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col.



5
6
7
# File 'lib/yatte/cursor.rb', line 5

def col
  @col
end

#col_offsetObject (readonly)

Returns the value of attribute col_offset.



5
6
7
# File 'lib/yatte/cursor.rb', line 5

def col_offset
  @col_offset
end

#rowObject (readonly)

Returns the value of attribute row.



5
6
7
# File 'lib/yatte/cursor.rb', line 5

def row
  @row
end

#row_offsetObject (readonly)

Returns the value of attribute row_offset.



5
6
7
# File 'lib/yatte/cursor.rb', line 5

def row_offset
  @row_offset
end

Instance Method Details

#clamp_colObject



47
48
49
# File 'lib/yatte/cursor.rb', line 47

def clamp_col
  @col = [@col, @buffer.line_length(@row)].min
end

#move_downObject



40
41
42
43
44
45
# File 'lib/yatte/cursor.rb', line 40

def move_down
  return if @row >= @buffer.line_count - 1

  @row += 1
  clamp_col
end

#move_leftObject



15
16
17
18
19
20
21
22
# File 'lib/yatte/cursor.rb', line 15

def move_left
  if @col > 0
    @col -= 1
  elsif @row > 0
    @row -= 1
    @col = @buffer.line_length(@row)
  end
end

#move_rightObject



24
25
26
27
28
29
30
31
# File 'lib/yatte/cursor.rb', line 24

def move_right
  if @col < @buffer.line_length(@row)
    @col += 1
  elsif @row < @buffer.line_count - 1
    @row += 1
    @col = 0
  end
end

#move_to_buffer_endObject



106
107
108
109
# File 'lib/yatte/cursor.rb', line 106

def move_to_buffer_end
  @row = [@buffer.line_count - 1, 0].max
  @col = @buffer.line_length(@row)
end

#move_to_buffer_startObject



101
102
103
104
# File 'lib/yatte/cursor.rb', line 101

def move_to_buffer_start
  @row = 0
  @col = 0
end

#move_to_line_endObject



76
77
78
# File 'lib/yatte/cursor.rb', line 76

def move_to_line_end
  @col = @buffer.line_length(@row)
end

#move_to_line_startObject



72
73
74
# File 'lib/yatte/cursor.rb', line 72

def move_to_line_start
  @col = 0
end

#move_upObject



33
34
35
36
37
38
# File 'lib/yatte/cursor.rb', line 33

def move_up
  return if @row == 0

  @row -= 1
  clamp_col
end

#move_word_leftObject



80
81
82
83
84
85
86
87
88
# File 'lib/yatte/cursor.rb', line 80

def move_word_left
  line = @buffer.line_at(@row)
  pos = @col

  pos -= 1 while pos > 0 && !word_char?(line[pos - 1])
  pos -= 1 while pos > 0 && word_char?(line[pos - 1])

  @col = pos
end

#move_word_rightObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/yatte/cursor.rb', line 90

def move_word_right
  line = @buffer.line_at(@row)
  len = line.length
  pos = @col

  pos += 1 while pos < len && word_char?(line[pos])
  pos += 1 while pos < len && !word_char?(line[pos])

  @col = pos
end

#screen_colObject



63
64
65
# File 'lib/yatte/cursor.rb', line 63

def screen_col
  @col - @col_offset
end

#screen_rowObject



59
60
61
# File 'lib/yatte/cursor.rb', line 59

def screen_row
  @row - @row_offset
end

#scroll(screen_rows, screen_cols) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/yatte/cursor.rb', line 51

def scroll(screen_rows, screen_cols)
  @row_offset = @row if @row < @row_offset
  @row_offset = @row - screen_rows + 1 if @row >= @row_offset + screen_rows

  @col_offset = @col if @col < @col_offset
  @col_offset = @col - screen_cols + 1 if @col >= @col_offset + screen_cols
end

#set(row, col) ⇒ Object



67
68
69
70
# File 'lib/yatte/cursor.rb', line 67

def set(row, col)
  @row = row
  @col = col
end