Class: RubyCurses::BufferManager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rbcurse/rmultitextview.rb,
lib/rbcurse/rmulticontainer.rb

Overview

Handles multiple buffers, navigation, maintenance etc Instantiated at startup of MultiTextView

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ BufferManager

Returns a new instance of BufferManager.



205
206
207
208
209
210
# File 'lib/rbcurse/rmultitextview.rb', line 205

def initialize source
  @source = source
  @buffers = [] # contains RBuffer
  @counter = 0
  # for each buffer i need to store data, current_index (row), curpos (col offset) and title (filename).
end

Instance Method Details

#add(component, title = nil) ⇒ Object



270
271
272
# File 'lib/rbcurse/rmultitextview.rb', line 270

def add filename, title=nil
  insert filename, @buffers.size, title
end

#add_content(str, title = "UNtitled") ⇒ Object

add content (array or str) to buffer list as a new buffer



289
290
291
292
# File 'lib/rbcurse/rmultitextview.rb', line 289

def add_content str, title="UNtitled"
  $log.debug " inside BUFFER MANAGER "
  insert_content str,  @buffers.size, title
end

#currentRBuffer

Returns current buffer/file.

Returns:

  • (RBuffer)

    current buffer/file



220
221
222
# File 'lib/rbcurse/rmultitextview.rb', line 220

def current
  @buffers[@counter]
end

#delete_at(index = @counter) ⇒ Object



252
253
254
# File 'lib/rbcurse/rmultitextview.rb', line 252

def delete_at index=@counter
  @buffers.delete_at index
end

#delete_by_name(name) ⇒ Object



255
256
257
# File 'lib/rbcurse/rmultitextview.rb', line 255

def delete_by_name name
  @buffers.delete_if {|b| b.filename == name }
end

#eachObject



214
215
216
# File 'lib/rbcurse/rmultitextview.rb', line 214

def each
  @buffers.each {|k| yield(k)}
end

#element_at(index) ⇒ Object

for each buffer i need to store data, current_index (row), curpos (col offset) and title (filename).



211
212
213
# File 'lib/rbcurse/rmultitextview.rb', line 211

def element_at index
  @buffers[index]
end

#firstObject



243
244
245
246
# File 'lib/rbcurse/rmultitextview.rb', line 243

def first
  @counter = 0
  @buffers[@counter]
end

#index(buff) ⇒ Object



297
298
299
# File 'lib/rbcurse/rmultitextview.rb', line 297

def index buff
  return @buffers.index
end

#insert(component, position, title = nil) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rbcurse/rmultitextview.rb', line 258

def insert filename, position, title=nil
  # read up file
  list = @source.set_content File.open(filename,"r").readlines
  # set new RBuffer
  title = filename unless title
  raise "invalid value for list, Should be an array #{list.class} " unless list.is_a? Array
  anew = RBuffer.new(list, 0, 0, filename, title)
  #@buffers << anew
  @buffers.insert position, anew
  @counter = position
  return anew
end

#insert_content(str, position, title = "UNTitled") ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rbcurse/rmultitextview.rb', line 273

def insert_content str, position,  title="UNTitled"
  case str
  when String
    # put str into list
    @source.set_content str
    list = @list
  when Array
    list = str
  end
  anew = RBuffer.new(list, 0, 0, "", title)
  #@buffers << anew
  @buffers.insert position, anew
  @counter = position
  return anew
end

#lastObject



247
248
249
250
# File 'lib/rbcurse/rmultitextview.rb', line 247

def last
  @counter = @buffers.size - 1
  @buffers[@counter]
end

#nextRBuffer

Would have liked to just return next buffer and not get lost in details of caller

Returns:



228
229
230
231
232
# File 'lib/rbcurse/rmultitextview.rb', line 228

def next
  @counter += 1
  @counter = 0 if @counter >= @buffers.size
  @buffers[@counter]
end

#previousRBuffer

Returns previous buffer/file.

Returns:

  • (RBuffer)

    previous buffer/file



236
237
238
239
240
241
242
# File 'lib/rbcurse/rmultitextview.rb', line 236

def previous
  $log.debug " previous bs: #{@buffers.size}, #{@counter}  "
  @counter -= 1
  return last() if @counter < 0
  $log.debug " previous ctr  #{@counter} "
  @buffers[@counter]
end

#sizeObject Also known as: count



293
294
295
# File 'lib/rbcurse/rmultitextview.rb', line 293

def size
  @buffers.size
end