Class: Readline::History

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/readline.rb

Overview

The History class encapsulates a history of all commands entered by users at the prompt, providing an interface for inspection and retrieval of all commands.

Class Method Summary collapse

Class Method Details

.<<(str) ⇒ Object

Synonym for Readline.add_history.



367
368
369
# File 'lib/readline.rb', line 367

def self.<<(str)
  RbReadline.add_history(str)
end

.[](index) ⇒ Object

Returns the command that was entered at the specified index in the history buffer.

Raises an IndexError if the entry is nil.



338
339
340
341
342
343
344
345
346
347
# File 'lib/readline.rb', line 338

def self.[](index)
  if index < 0
    index += RbReadline.history_length
  end
  entry = RbReadline.history_get(RbReadline.history_base+index)
  if entry.nil?
    raise IndexError,"invalid index"
  end
  entry.line.dup
end

.[]=(index, str) ⇒ Object

Sets the command str at the given index in the history buffer.

You can only replace an existing entry. Attempting to create a new entry will result in an IndexError.



354
355
356
357
358
359
360
361
362
363
# File 'lib/readline.rb', line 354

def self.[]=(index,str)
  if index<0
    index += RbReadline.history_length
  end
  entry = RbReadline.replace_history_entry(index,str,nil)
  if entry.nil?
    raise IndexError,"invalid index"
  end
  str
end

.delete_at(index) ⇒ Object

Deletes an entry from the histoyr buffer at the specified index.



446
447
448
449
450
451
452
453
454
# File 'lib/readline.rb', line 446

def self.delete_at(index)
  if index < 0
    i += RbReadline.history_length
  end
  if index < 0 || index > RbReadline.history_length - 1
    raise IndexError, "invalid index"
  end
  rb_remove_history(index)
end

.eachObject

Iterates over each entry in the history buffer.



416
417
418
419
420
421
422
423
# File 'lib/readline.rb', line 416

def self.each()
  for i in 0 ... RbReadline.history_length
    entry = RbReadline.history_get(RbReadline.history_base + i)
    break if entry.nil?
    yield entry.line.dup
  end
  self
end

.empty?Boolean

Returns a bolean value indicating whether or not the history buffer is empty.

Returns:

  • (Boolean)


440
441
442
# File 'lib/readline.rb', line 440

def self.empty?()
  RbReadline.history_length == 0
end

.lengthObject

Returns the length of the history buffer.



427
428
429
# File 'lib/readline.rb', line 427

def self.length()
  RbReadline.history_length
end

.popObject

Removes and returns the last element from the history buffer.



396
397
398
399
400
401
402
# File 'lib/readline.rb', line 396

def self.pop()
  if RbReadline.history_length>0
    rb_remove_history(RbReadline.history_length-1)
  else
    nil
  end
end

.push(*args) ⇒ Object

Pushes a list of args onto the history buffer.



373
374
375
376
377
# File 'lib/readline.rb', line 373

def self.push(*args)
  args.each do |str|
    RbReadline.add_history(str)
  end
end

.rb_remove_history(index) ⇒ Object

Internal function that removes the item at index from the history buffer, performing necessary duplication in the process. – TODO: mark private?



384
385
386
387
388
389
390
391
392
# File 'lib/readline.rb', line 384

def self.rb_remove_history(index)
  entry = RbReadline.remove_history(index)
  if (entry)
    val = entry.line.dup
    entry = nil
    return val
  end
  nil
end

.shiftObject

Removes and returns the first element from the history buffer.



406
407
408
409
410
411
412
# File 'lib/readline.rb', line 406

def self.shift()
  if RbReadline.history_length>0
    rb_remove_history(0)
  else
    nil
  end
end

.sizeObject

Synonym for Readline.length.



433
434
435
# File 'lib/readline.rb', line 433

def self.size()
  RbReadline.history_length
end

.to_sObject

The History class, stringified in all caps. – Why?



329
330
331
# File 'lib/readline.rb', line 329

def self.to_s
  "HISTORY"
end