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.



338
339
340
# File 'lib/readline.rb', line 338

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.



309
310
311
312
313
314
315
316
317
318
# File 'lib/readline.rb', line 309

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.



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

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.



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

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.



387
388
389
390
391
392
393
394
# File 'lib/readline.rb', line 387

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)


411
412
413
# File 'lib/readline.rb', line 411

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

.lengthObject

Returns the length of the history buffer.



398
399
400
# File 'lib/readline.rb', line 398

def self.length()
   RbReadline.history_length
end

.popObject

Removes and returns the last element from the history buffer.



367
368
369
370
371
372
373
# File 'lib/readline.rb', line 367

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.



344
345
346
347
348
# File 'lib/readline.rb', line 344

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?



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

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.



377
378
379
380
381
382
383
# File 'lib/readline.rb', line 377

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

.sizeObject

Synonym for Readline.length.



404
405
406
# File 'lib/readline.rb', line 404

def self.size()
   RbReadline.history_length
end

.to_sObject

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



300
301
302
# File 'lib/readline.rb', line 300

def self.to_s
   "HISTORY"
end