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.


377
378
379
# File 'lib/readline.rb', line 377

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.


348
349
350
351
352
353
354
355
356
357
# File 'lib/readline.rb', line 348

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.


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

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.


456
457
458
459
460
461
462
463
464
# File 'lib/readline.rb', line 456

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.


426
427
428
429
430
431
432
433
# File 'lib/readline.rb', line 426

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)

450
451
452
# File 'lib/readline.rb', line 450

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

.lengthObject

Returns the length of the history buffer.


437
438
439
# File 'lib/readline.rb', line 437

def self.length()
  RbReadline.history_length
end

.popObject

Removes and returns the last element from the history buffer.


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

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.


383
384
385
386
387
# File 'lib/readline.rb', line 383

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?


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

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.


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

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

.sizeObject

Synonym for Readline.length.


443
444
445
# File 'lib/readline.rb', line 443

def self.size()
  RbReadline.history_length
end

.to_sObject

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


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

def self.to_s
  "HISTORY"
end