Class: Vice::Buffer
- Inherits:
-
Object
- Object
- Vice::Buffer
- Defined in:
- lib/vice/buffer.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#cursor ⇒ Object
Returns the value of attribute cursor.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#modified ⇒ Object
readonly
Returns the value of attribute modified.
-
#v_scroll ⇒ Object
Returns the value of attribute v_scroll.
Instance Method Summary collapse
- #addmark(mark) ⇒ Object
- #cols ⇒ Object
- #currentline ⇒ Object
- #cursor_down ⇒ Object
- #cursor_end_of_line ⇒ Object
- #cursor_left ⇒ Object
- #cursor_right ⇒ Object
- #cursor_up ⇒ Object
- #getline(index) ⇒ Object
- #gotomark(mark) ⇒ Object
-
#initialize(filename) ⇒ Buffer
constructor
A new instance of Buffer.
- #insert(text) ⇒ Object
- #insertf(index, column, text) ⇒ Object
- #lines ⇒ Object
- #newline(index) ⇒ Object
- #rmchar ⇒ Object
- #rmcharf(index, column) ⇒ Object
- #rmline ⇒ Object
- #rmlinef(index) ⇒ Object
- #setline(index, text) ⇒ Object
- #write ⇒ Object
- #writef(filename) ⇒ Object
Constructor Details
#initialize(filename) ⇒ Buffer
Returns a new instance of Buffer.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/vice/buffer.rb', line 8 def initialize(filename) @buffer = [] @marks = {} if filename @filename = filename File.open(filename, 'r') do |f| # TODO: don't assume file exists f.each_line { |line| @buffer.push line.chomp } end else @buffer.push '' end @cursor = Vice::Cursor.new @modified = false @v_scroll = 0 end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
2 3 4 |
# File 'lib/vice/buffer.rb', line 2 def buffer @buffer end |
#cursor ⇒ Object
Returns the value of attribute cursor.
5 6 7 |
# File 'lib/vice/buffer.rb', line 5 def cursor @cursor end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
3 4 5 |
# File 'lib/vice/buffer.rb', line 3 def filename @filename end |
#modified ⇒ Object (readonly)
Returns the value of attribute modified.
4 5 6 |
# File 'lib/vice/buffer.rb', line 4 def modified @modified end |
#v_scroll ⇒ Object
Returns the value of attribute v_scroll.
6 7 8 |
# File 'lib/vice/buffer.rb', line 6 def v_scroll @v_scroll end |
Instance Method Details
#addmark(mark) ⇒ Object
145 146 147 |
# File 'lib/vice/buffer.rb', line 145 def addmark(mark) @marks[mark] = @cursor.clone end |
#cols ⇒ Object
141 142 143 |
# File 'lib/vice/buffer.rb', line 141 def cols @buffer[@cursor.line].length end |
#currentline ⇒ Object
133 134 135 |
# File 'lib/vice/buffer.rb', line 133 def currentline getline @cursor.line end |
#cursor_down ⇒ Object
49 50 51 52 |
# File 'lib/vice/buffer.rb', line 49 def cursor_down @cursor.line += 1 if @cursor.line < @buffer.length - 1 cursor_end_of_line end |
#cursor_end_of_line ⇒ Object
38 39 40 41 42 |
# File 'lib/vice/buffer.rb', line 38 def cursor_end_of_line # if we're out of bounds, move the cursor to the end of the line @cursor.col = @buffer[@cursor.line].length - 1 if @cursor.col >= @buffer[@cursor.line].length @cursor.col = 0 if @cursor.col.negative? end |
#cursor_left ⇒ Object
54 55 56 |
# File 'lib/vice/buffer.rb', line 54 def cursor_left @cursor.col -= 1 if @cursor.col.positive? end |
#cursor_right ⇒ Object
58 59 60 |
# File 'lib/vice/buffer.rb', line 58 def cursor_right @cursor.col += 1 if @cursor.col < @buffer[@cursor.line].length - 1 end |
#cursor_up ⇒ Object
44 45 46 47 |
# File 'lib/vice/buffer.rb', line 44 def cursor_up @cursor.line -= 1 if @cursor.line.positive? cursor_end_of_line end |
#getline(index) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/vice/buffer.rb', line 126 def getline(index) raise 'negative line index' unless index >= 0 raise 'line index out of bounds' unless index < @buffer.length @buffer[index] end |
#gotomark(mark) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/vice/buffer.rb', line 149 def gotomark(mark) return false unless @marks[mark] @cursor = @marks[mark].clone @cursor.line = @buffer.length - 1 if @cursor.line >= @buffer.length cursor_end_of_line true end |
#insert(text) ⇒ Object
97 98 99 |
# File 'lib/vice/buffer.rb', line 97 def insert(text) insertf @cursor.line, @cursor.col, text end |
#insertf(index, column, text) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/vice/buffer.rb', line 86 def insertf(index, column, text) raise 'negative line index' unless index >= 0 raise 'line index out of bounds' unless index < @buffer.length raise 'negative column index' unless column >= 0 raise 'column index out of bounds' unless column <= @buffer[index].length @modified = true @buffer[index].insert column, text end |
#lines ⇒ Object
137 138 139 |
# File 'lib/vice/buffer.rb', line 137 def lines @buffer.length end |
#newline(index) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/vice/buffer.rb', line 62 def newline(index) raise 'negative line index' unless index >= 0 @modified = true # silently append to the end when index out of bounds index = @buffer.length if index > @buffer.length @buffer.insert index, '' end |
#rmchar ⇒ Object
112 113 114 |
# File 'lib/vice/buffer.rb', line 112 def rmchar rmcharf @cursor.line, @cursor.col end |
#rmcharf(index, column) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/vice/buffer.rb', line 101 def rmcharf(index, column) raise 'negative line index' unless index >= 0 raise 'line index out of bounds' unless index < @buffer.length raise 'negative column index' unless column >= 0 raise 'column index out of bounds' unless column <= @buffer[index].length @modified = true @buffer[index].slice! column end |
#rmline ⇒ Object
82 83 84 |
# File 'lib/vice/buffer.rb', line 82 def rmline rmlinef @cursor.line end |
#rmlinef(index) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/vice/buffer.rb', line 73 def rmlinef(index) raise 'negative line index' unless index >= 0 raise 'line index out of bounds' unless index < @buffer.length @modified = true @buffer.delete_at index end |
#setline(index, text) ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/vice/buffer.rb', line 116 def setline(index, text) raise 'negative line index' unless index >= 0 raise 'line index out of bounds' unless index < @buffer.length @modified = true @buffer[index] = text cursor_end_of_line end |
#write ⇒ Object
34 35 36 |
# File 'lib/vice/buffer.rb', line 34 def write writef @filename end |
#writef(filename) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/vice/buffer.rb', line 24 def writef(filename) @modified = false File.open(filename, 'w') do |f| f.write @buffer.join "\n" end @filename = filename end |