history: delete #pusher & #clearer, make the class Readline-agnostic
1. The `options` parameter may contain the `:history` key, which contains a history array (typically `Readline::HISTORY`) 2. The `history` key is expected to have a history object as a value. Usually, it's `Readline::HISTORY`. This object *must* be Enumerable and implement `#clear` & `#to_a`. If nothing is provided, it defaults to an empty array. As result, no hard dependency on `Readline` anymore. 3. Removed duplicated history objects. It looks like the original code assumed that pushing to `Readline::HISTORY` also pushes to the file but it's not the case. There doesn't seem to be a real need to keep two arrays of history
This commit is contained in:
parent
4b425ed2a6
commit
76e31977af
|
@ -2,22 +2,17 @@ class Pry
|
|||
# The History class is responsible for maintaining the user's input history,
|
||||
# both internally and within Readline.
|
||||
class History
|
||||
attr_accessor :loader, :saver, :pusher, :clearer
|
||||
attr_accessor :loader, :saver
|
||||
|
||||
# @return [Fixnum] Number of lines in history when Pry first loaded.
|
||||
attr_reader :original_lines
|
||||
|
||||
def initialize(options = {})
|
||||
@history = []
|
||||
@original_lines = 0
|
||||
@history = options[:history] || []
|
||||
@file_path = options[:file_path]
|
||||
|
||||
@original_lines = 0
|
||||
@loader = method(:read_from_file)
|
||||
@saver = method(:save_to_file)
|
||||
|
||||
Pry.config.input # force Readline to load if applicable
|
||||
@pusher = defined?(Readline) ? method(:push_to_readline) : proc {}
|
||||
@clearer = defined?(Readline) ? method(:clear_readline) : proc {}
|
||||
end
|
||||
|
||||
# Load the input history using `History.loader`.
|
||||
|
@ -26,7 +21,6 @@ class Pry
|
|||
@loader.call do |line|
|
||||
next if invalid_readline_line?(line)
|
||||
|
||||
@pusher.call(line.chomp)
|
||||
@history << line.chomp
|
||||
@original_lines += 1
|
||||
end
|
||||
|
@ -36,15 +30,21 @@ class Pry
|
|||
# @param [String] line
|
||||
# @return [String] The same line that was passed in
|
||||
def push(line)
|
||||
empty_or_invalid_line = line.empty? || invalid_readline_line?(line)
|
||||
return line if line.empty? || invalid_readline_line?(line)
|
||||
|
||||
unless empty_or_invalid_line || (@history.last && line == @history.last)
|
||||
@pusher.call(line)
|
||||
@history << line
|
||||
if !should_ignore?(line) && Pry.config.history.should_save
|
||||
@saver.call(line)
|
||||
end
|
||||
begin
|
||||
last_line = @history[-1]
|
||||
rescue IndexError
|
||||
last_line = nil
|
||||
end
|
||||
|
||||
return line if line == last_line
|
||||
|
||||
@history << line
|
||||
if !should_ignore?(line) && Pry.config.history.should_save
|
||||
@saver.call(line)
|
||||
end
|
||||
|
||||
line
|
||||
end
|
||||
alias << push
|
||||
|
@ -52,9 +52,8 @@ class Pry
|
|||
# Clear this session's history. This won't affect the contents of the
|
||||
# history file.
|
||||
def clear
|
||||
@clearer.call
|
||||
@history.clear
|
||||
@original_lines = 0
|
||||
@history = []
|
||||
end
|
||||
|
||||
# @return [Fixnum] The number of lines in history.
|
||||
|
@ -71,7 +70,7 @@ class Pry
|
|||
# @return [Array<String>] An Array containing all lines of history loaded
|
||||
# or entered by the user in the current session.
|
||||
def to_a
|
||||
@history.dup
|
||||
@history.to_a
|
||||
end
|
||||
|
||||
# Filter the history with the histignore options
|
||||
|
@ -105,17 +104,6 @@ class Pry
|
|||
warn "Unable to read history file: #{error.message}"
|
||||
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
|
||||
|
||||
# The default saver. Appends the given line to `Pry.history.config.file`.
|
||||
def save_to_file(line)
|
||||
history_file.puts line if history_file
|
||||
|
|
|
@ -39,7 +39,14 @@ class Pry
|
|||
end
|
||||
|
||||
def history
|
||||
@history ||= History.new
|
||||
return @history if @history
|
||||
|
||||
@history =
|
||||
if defined?(Pry.config.input::HISTORY)
|
||||
History.new(history: Pry.config.input::HISTORY)
|
||||
else
|
||||
History.new
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
|
|
|
@ -104,7 +104,6 @@ describe Pry do
|
|||
@histfile = Tempfile.new(["pryhistory", "txt"])
|
||||
@history = Pry::History.new(file_path: @histfile.path)
|
||||
Pry.config.history.should_save = true
|
||||
@history.pusher = proc {}
|
||||
end
|
||||
|
||||
after do
|
||||
|
|
Loading…
Reference in New Issue