2011-09-05 04:45:58 -04:00
|
|
|
class Pry
|
2012-12-28 20:50:06 -05:00
|
|
|
# The History class is responsible for maintaining the user's input history,
|
|
|
|
# both 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
|
|
|
|
|
2012-12-28 19:57:30 -05:00
|
|
|
def initialize(options={})
|
2011-09-05 04:45:58 -04:00
|
|
|
@history = []
|
2012-07-12 07:13:13 -04:00
|
|
|
@original_lines = 0
|
2012-12-28 19:57:30 -05:00
|
|
|
@file_path = options[:file_path]
|
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)
|
2012-12-28 20:35:04 -05:00
|
|
|
@saver = method(:save_to_file)
|
2011-12-02 00:03:36 -05:00
|
|
|
@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|
|
2012-12-28 20:28:08 -05:00
|
|
|
@pusher.call(line.chomp)
|
2011-09-05 04:45:58 -04:00
|
|
|
@history << line.chomp
|
2013-03-17 04:18:52 -04:00
|
|
|
@original_lines += 1
|
2011-09-05 04:45:58 -04:00
|
|
|
end
|
|
|
|
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
|
2012-12-28 20:35:04 -05:00
|
|
|
@saver.call(line) if Pry.config.history.should_save
|
2011-09-05 04:45:58 -04:00
|
|
|
end
|
|
|
|
line
|
|
|
|
end
|
|
|
|
alias << push
|
|
|
|
|
2012-12-28 20:50:06 -05:00
|
|
|
# Clear this session's history. This won't affect the contents of the
|
|
|
|
# history file.
|
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 = []
|
|
|
|
end
|
|
|
|
|
2012-07-12 07:13:13 -04:00
|
|
|
# @return [Fixnum] The number of lines in history.
|
|
|
|
def history_line_count
|
|
|
|
@history.count
|
|
|
|
end
|
|
|
|
|
2012-12-28 20:50:06 -05:00
|
|
|
# @return [Fixnum] The number of lines in history from just this session.
|
2012-07-12 07:13:13 -04:00
|
|
|
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
|
2012-12-28 20:50:06 -05:00
|
|
|
|
2011-12-02 00:03:36 -05:00
|
|
|
# The default loader. Yields lines from `Pry.history.config.file`.
|
|
|
|
def read_from_file
|
2012-12-28 20:50:06 -05:00
|
|
|
filename = File.expand_path(Pry.config.history.file)
|
|
|
|
|
|
|
|
if File.exists?(filename)
|
|
|
|
File.foreach(filename) { |line| yield(line) }
|
2012-02-21 22:46:33 -05:00
|
|
|
end
|
2012-12-28 20:50:06 -05:00
|
|
|
rescue => error
|
|
|
|
warn "History file not loaded: #{error.message}"
|
2011-12-02 00:03:36 -05:00
|
|
|
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
|
2012-12-28 19:57:30 -05:00
|
|
|
|
2012-12-28 20:50:06 -05:00
|
|
|
# The default saver. Appends the given line to `Pry.history.config.file`.
|
2012-12-28 20:35:04 -05:00
|
|
|
def save_to_file(line)
|
|
|
|
history_file.puts line if history_file
|
|
|
|
end
|
|
|
|
|
2012-12-28 20:50:06 -05:00
|
|
|
# The history file, opened for appending.
|
2012-12-28 19:57:30 -05:00
|
|
|
def history_file
|
2012-12-28 20:50:06 -05:00
|
|
|
if defined?(@history_file)
|
|
|
|
@history_file
|
|
|
|
else
|
2013-05-26 05:53:32 -04:00
|
|
|
@history_file = File.open(file_path, 'a', 0600).tap { |f| f.sync = true }
|
2012-12-28 19:57:30 -05:00
|
|
|
end
|
2012-12-28 20:50:06 -05:00
|
|
|
rescue Errno::EACCES
|
|
|
|
warn 'History not saved; unable to open your history file for writing.'
|
|
|
|
@history_file = false
|
2012-12-28 19:57:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def file_path
|
|
|
|
@file_path || Pry.config.history.file
|
|
|
|
end
|
2011-09-05 04:45:58 -04:00
|
|
|
end
|
|
|
|
end
|