Class: Yatte::Cursor
- Inherits:
-
Object
- Object
- Yatte::Cursor
- Defined in:
- lib/yatte/cursor.rb
Instance Attribute Summary collapse
-
#col ⇒ Object
readonly
Returns the value of attribute col.
-
#col_offset ⇒ Object
readonly
Returns the value of attribute col_offset.
-
#row ⇒ Object
readonly
Returns the value of attribute row.
-
#row_offset ⇒ Object
readonly
Returns the value of attribute row_offset.
Instance Method Summary collapse
- #clamp_col ⇒ Object
-
#initialize(buffer) ⇒ Cursor
constructor
A new instance of Cursor.
- #move_down ⇒ Object
- #move_left ⇒ Object
- #move_right ⇒ Object
- #move_to_buffer_end ⇒ Object
- #move_to_buffer_start ⇒ Object
- #move_to_line_end ⇒ Object
- #move_to_line_start ⇒ Object
- #move_up ⇒ Object
- #move_word_left ⇒ Object
- #move_word_right ⇒ Object
- #screen_col ⇒ Object
- #screen_row ⇒ Object
- #scroll(screen_rows, screen_cols) ⇒ Object
- #set(row, col) ⇒ Object
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
#col ⇒ Object (readonly)
Returns the value of attribute col.
5 6 7 |
# File 'lib/yatte/cursor.rb', line 5 def col @col end |
#col_offset ⇒ Object (readonly)
Returns the value of attribute col_offset.
5 6 7 |
# File 'lib/yatte/cursor.rb', line 5 def col_offset @col_offset end |
#row ⇒ Object (readonly)
Returns the value of attribute row.
5 6 7 |
# File 'lib/yatte/cursor.rb', line 5 def row @row end |
#row_offset ⇒ Object (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_col ⇒ Object
47 48 49 |
# File 'lib/yatte/cursor.rb', line 47 def clamp_col @col = [@col, @buffer.line_length(@row)].min end |
#move_down ⇒ Object
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_left ⇒ Object
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_right ⇒ Object
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_end ⇒ Object
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_start ⇒ Object
101 102 103 104 |
# File 'lib/yatte/cursor.rb', line 101 def move_to_buffer_start @row = 0 @col = 0 end |
#move_to_line_end ⇒ Object
76 77 78 |
# File 'lib/yatte/cursor.rb', line 76 def move_to_line_end @col = @buffer.line_length(@row) end |
#move_to_line_start ⇒ Object
72 73 74 |
# File 'lib/yatte/cursor.rb', line 72 def move_to_line_start @col = 0 end |
#move_up ⇒ Object
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_left ⇒ Object
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_right ⇒ Object
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_col ⇒ Object
63 64 65 |
# File 'lib/yatte/cursor.rb', line 63 def screen_col @col - @col_offset end |
#screen_row ⇒ Object
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 |