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.



356
357
358
# File 'lib/readline.rb', line 356

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.



327
328
329
330
331
332
333
334
335
336
# File 'lib/readline.rb', line 327

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.



343
344
345
346
347
348
349
350
351
352
# File 'lib/readline.rb', line 343

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.



435
436
437
438
439
440
441
442
443
# File 'lib/readline.rb', line 435

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.



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

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)


429
430
431
# File 'lib/readline.rb', line 429

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

.lengthObject

Returns the length of the history buffer.



416
417
418
# File 'lib/readline.rb', line 416

def self.length()
   RbReadline.history_length
end

.popObject

Removes and returns the last element from the history buffer.



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

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.



362
363
364
365
366
# File 'lib/readline.rb', line 362

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?



373
374
375
376
377
378
379
380
381
# File 'lib/readline.rb', line 373

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.



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

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

.sizeObject

Synonym for Readline.length.



422
423
424
# File 'lib/readline.rb', line 422

def self.size()
   RbReadline.history_length
end

.to_sObject

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



318
319
320
# File 'lib/readline.rb', line 318

def self.to_s
   "HISTORY"
end