2011-09-05 04:45:58 -04:00
|
|
|
class Pry
|
2011-09-05 16:52:49 -04:00
|
|
|
# The History class is responsible for maintaining the user's input history, both
|
2011-12-02 00:03:36 -05:00
|
|
|
# internally and within Readline.
|
2011-09-05 04:46:16 -04:00
|
|
|
class History
|
2011-12-02 00:03:36 -05:00
|
|
|
attr_accessor :loader, :saver, :pusher, :clearer
|
|
|
|
|
2012-07-12 07:13:13 -04:00
|
|
|
# @return [Fixnum] Number of lines in history when Pry first loaded.
|
|
|
|
attr_reader :original_lines
|
|
|
|
|
2011-09-05 04:45:58 -04:00
|
|
|
def initialize
|
|
|
|
@history = []
|
2011-09-05 16:52:49 -04:00
|
|
|
@saved_lines = 0
|
2012-07-12 07:13:13 -04:00
|
|
|
@original_lines = 0
|
2011-12-02 00:26:22 -05:00
|
|
|
restore_default_behavior
|
|
|
|
end
|
2011-12-02 00:03:36 -05:00
|
|
|
|
2011-12-02 00:26:22 -05:00
|
|
|
# Assign the default methods for loading, saving, pushing, and clearing.
|
|
|
|
def restore_default_behavior
|
2011-12-02 00:03:36 -05:00
|
|
|
@loader = method(:read_from_file)
|
|
|
|
@saver = method(:write_to_file)
|
|
|
|
@pusher = method(:push_to_readline)
|
|
|
|
@clearer = method(:clear_readline)
|
2011-09-05 04:45:58 -04:00
|
|
|
end
|
|
|
|
|
2011-12-02 00:03:36 -05:00
|
|
|
# Load the input history using `History.loader`.
|
2011-09-05 16:52:49 -04:00
|
|
|
# @return [Integer] The number of lines loaded
|
2011-12-02 00:03:36 -05:00
|
|
|
def load
|
|
|
|
@loader.call do |line|
|
|
|
|
@pusher.call(line.chomp)
|
2011-09-05 04:45:58 -04:00
|
|
|
@history << line.chomp
|
|
|
|
end
|
2012-07-12 07:13:13 -04:00
|
|
|
@saved_lines = @original_lines = @history.length
|
2011-09-05 04:45:58 -04:00
|
|
|
end
|
|
|
|
|
2011-12-02 00:26:22 -05:00
|
|
|
# Write this session's history using `History.saver`.
|
2011-09-05 16:52:49 -04:00
|
|
|
# @return [Integer] The number of lines saved
|
2011-12-02 00:03:36 -05:00
|
|
|
def save
|
2011-09-05 16:52:49 -04:00
|
|
|
history_to_save = @history[@saved_lines..-1]
|
2011-12-02 00:03:36 -05:00
|
|
|
@saver.call(history_to_save)
|
2011-09-05 16:52:49 -04:00
|
|
|
@saved_lines = @history.length
|
|
|
|
history_to_save.length
|
2011-09-05 04:45:58 -04:00
|
|
|
end
|
|
|
|
|
2011-12-02 00:26:22 -05:00
|
|
|
# Add a line to the input history, ignoring blank and duplicate lines.
|
2011-09-05 16:52:49 -04:00
|
|
|
# @param [String] line
|
|
|
|
# @return [String] The same line that was passed in
|
2011-09-05 04:45:58 -04:00
|
|
|
def push(line)
|
2011-10-09 04:41:42 -04:00
|
|
|
unless line.empty? || (@history.last && line == @history.last)
|
2011-12-02 00:03:36 -05:00
|
|
|
@pusher.call(line)
|
2011-09-05 04:45:58 -04:00
|
|
|
@history << line
|
|
|
|
end
|
|
|
|
line
|
|
|
|
end
|
|
|
|
alias << push
|
|
|
|
|
2011-12-02 00:26:22 -05:00
|
|
|
# Clear all history. Anything the user entered before this point won't be
|
2011-09-05 16:52:49 -04:00
|
|
|
# saved, but anything they put in afterwards will still be appended to the
|
|
|
|
# history file on exit.
|
2011-09-05 04:45:58 -04:00
|
|
|
def clear
|
2011-12-02 00:03:36 -05:00
|
|
|
@clearer.call
|
2011-09-05 04:45:58 -04:00
|
|
|
@history = []
|
2011-09-05 16:52:49 -04:00
|
|
|
@saved_lines = 0
|
2011-09-05 04:45:58 -04:00
|
|
|
end
|
|
|
|
|
2012-07-12 07:13:13 -04:00
|
|
|
# @return [Fixnum] The number of lines in history.
|
|
|
|
def history_line_count
|
|
|
|
@history.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def session_line_count
|
|
|
|
@history.count - @original_lines
|
|
|
|
end
|
|
|
|
|
2011-12-02 00:26:22 -05:00
|
|
|
# Return an Array containing all stored history.
|
2011-09-05 16:52:49 -04:00
|
|
|
# @return [Array<String>] An Array containing all lines of history loaded
|
|
|
|
# or entered by the user in the current session.
|
2011-09-05 04:45:58 -04:00
|
|
|
def to_a
|
|
|
|
@history.dup
|
|
|
|
end
|
2011-12-02 00:03:36 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
# The default loader. Yields lines from `Pry.history.config.file`.
|
|
|
|
def read_from_file
|
2012-02-15 06:33:15 -05:00
|
|
|
begin
|
2012-03-05 06:21:54 -05:00
|
|
|
history_file = File.expand_path(Pry.config.history.file)
|
2012-02-15 06:33:15 -05:00
|
|
|
if File.exists?(history_file)
|
|
|
|
File.foreach(history_file) { |line| yield(line) }
|
|
|
|
end
|
2012-02-21 22:46:33 -05:00
|
|
|
rescue => error
|
|
|
|
unless error.message.empty?
|
2012-02-22 12:49:35 -05:00
|
|
|
warn "History file not loaded, received an error: #{error.message}"
|
2012-02-21 22:46:33 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-02 00:03:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# The default saver. Appends the given lines to `Pry.history.config.file`.
|
|
|
|
# @param [Array<String>] lines
|
|
|
|
def write_to_file(lines)
|
|
|
|
history_file = File.expand_path(Pry.config.history.file)
|
|
|
|
|
2012-03-05 06:17:01 -05:00
|
|
|
begin
|
2012-02-15 06:33:15 -05:00
|
|
|
File.open(history_file, 'a') do |f|
|
|
|
|
lines.each { |ln| f.puts ln }
|
|
|
|
end
|
2012-05-28 19:37:02 -04:00
|
|
|
rescue Errno::EACCES
|
2012-03-05 06:17:01 -05:00
|
|
|
# We should probably create an option Pry.show_warnings?!?!?!
|
|
|
|
warn 'Unable to write to your history file, history not saved'
|
2011-12-02 00:03:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# The default pusher. Appends the given line to Readline::HISTORY.
|
|
|
|
# @param [String] line
|
|
|
|
def push_to_readline(line)
|
|
|
|
Readline::HISTORY << line
|
|
|
|
end
|
|
|
|
|
|
|
|
# The default clearer. Clears Readline::HISTORY.
|
|
|
|
def clear_readline
|
|
|
|
Readline::HISTORY.shift until Readline::HISTORY.empty?
|
|
|
|
end
|
2011-09-05 04:45:58 -04:00
|
|
|
end
|
|
|
|
end
|