Class: Vice::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/vice/buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bufferObject (readonly)

Returns the value of attribute buffer.



2
3
4
# File 'lib/vice/buffer.rb', line 2

def buffer
  @buffer
end

#cursorObject

Returns the value of attribute cursor.



5
6
7
# File 'lib/vice/buffer.rb', line 5

def cursor
  @cursor
end

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/vice/buffer.rb', line 3

def filename
  @filename
end

#modifiedObject (readonly)

Returns the value of attribute modified.



4
5
6
# File 'lib/vice/buffer.rb', line 4

def modified
  @modified
end

#v_scrollObject

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

#colsObject



141
142
143
# File 'lib/vice/buffer.rb', line 141

def cols
  @buffer[@cursor.line].length
end

#currentlineObject



133
134
135
# File 'lib/vice/buffer.rb', line 133

def currentline
  getline @cursor.line
end

#cursor_downObject



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_lineObject



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_leftObject



54
55
56
# File 'lib/vice/buffer.rb', line 54

def cursor_left
  @cursor.col -= 1 if @cursor.col.positive?
end

#cursor_rightObject



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_upObject



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

#linesObject



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

#rmcharObject



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

#rmlineObject



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

#writeObject



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