class Pry::History

The History class is responsible for maintaining the user's input history, both internally and within Readline.

Attributes

clearer[RW]
loader[RW]
original_lines[R]

@return [Fixnum] Number of lines in history when Pry first loaded.

pusher[RW]
saver[RW]

Public Class Methods

new() click to toggle source
# File lib/pry/history.rb, line 10
def initialize
  @history = []
  @saved_lines = 0
  @original_lines = 0
  restore_default_behavior
end

Public Instance Methods

<<(line)
Alias for: push
clear() click to toggle source

Clear all history. Anything the user entered before this point won't be saved, but anything they put in afterwards will still be appended to the history file on exit.

# File lib/pry/history.rb, line 59
def clear
  @clearer.call
  @history = []
  @saved_lines = 0
end
history_line_count() click to toggle source

@return [Fixnum] The number of lines in history.

# File lib/pry/history.rb, line 66
def history_line_count
  @history.count
end
load() click to toggle source

Load the input history using `History.loader`. @return [Integer] The number of lines loaded

# File lib/pry/history.rb, line 27
def load
  @loader.call do |line|
    @pusher.call(line.chomp)
    @history << line.chomp
  end
  @saved_lines = @original_lines = @history.length
end
push(line) click to toggle source

Add a line to the input history, ignoring blank and duplicate lines. @param [String] line @return [String] The same line that was passed in

# File lib/pry/history.rb, line 47
def push(line)
  unless line.empty? || (@history.last && line == @history.last)
    @pusher.call(line)
    @history << line
  end
  line
end
Also aliased as: <<
restore_default_behavior() click to toggle source

Assign the default methods for loading, saving, pushing, and clearing.

# File lib/pry/history.rb, line 18
def restore_default_behavior
  @loader  = method(:read_from_file)
  @saver   = method(:write_to_file)
  @pusher  = method(:push_to_readline)
  @clearer = method(:clear_readline)
end
save() click to toggle source

Write this session's history using `History.saver`. @return [Integer] The number of lines saved

# File lib/pry/history.rb, line 37
def save
  history_to_save = @history[@saved_lines..-1]
  @saver.call(history_to_save)
  @saved_lines = @history.length
  history_to_save.length
end
session_line_count() click to toggle source
# File lib/pry/history.rb, line 70
def session_line_count
  @history.count - @original_lines
end
to_a() click to toggle source

Return an Array containing all stored history. @return [Array<String>] An Array containing all lines of history loaded

or entered by the user in the current session.
# File lib/pry/history.rb, line 77
def to_a
  @history.dup
end

Private Instance Methods

clear_readline() click to toggle source

The default clearer. Clears Readline::HISTORY.

# File lib/pry/history.rb, line 118
def clear_readline
  Readline::HISTORY.shift until Readline::HISTORY.empty?
end
push_to_readline(line) click to toggle source

The default pusher. Appends the given line to Readline::HISTORY. @param [String] line

# File lib/pry/history.rb, line 113
def push_to_readline(line)
  Readline::HISTORY << line
end
read_from_file() { |line| ... } click to toggle source

The default loader. Yields lines from `Pry.history.config.file`.

# File lib/pry/history.rb, line 83
def read_from_file
  begin
    history_file = File.expand_path(Pry.config.history.file)
    if File.exists?(history_file)
      File.foreach(history_file) { |line| yield(line) }
    end
  rescue => error
    unless error.message.empty?
      warn "History file not loaded, received an error: #{error.message}"
    end
  end
end
write_to_file(lines) click to toggle source

The default saver. Appends the given lines to `Pry.history.config.file`. @param [Array<String>] lines

# File lib/pry/history.rb, line 98
def write_to_file(lines)
  history_file = File.expand_path(Pry.config.history.file)

  begin
    File.open(history_file, 'a') do |f|
      lines.each { |ln| f.puts ln }
    end
  rescue Errno::EACCES
    # We should probably create an option Pry.show_warnings?!?!?!
    warn 'Unable to write to your history file, history not saved'
  end
end