mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
44 lines
944 B
Ruby
44 lines
944 B
Ruby
class Pry
|
|
class History
|
|
def initialize
|
|
@history = []
|
|
@first_new_line = 0 # TODO: rename this
|
|
end
|
|
|
|
def load(filename)
|
|
File.foreach(filename) do |line|
|
|
Readline::HISTORY << line.chomp
|
|
@history << line.chomp
|
|
end
|
|
@first_new_line = @history.length
|
|
end
|
|
|
|
def save(filename)
|
|
history_to_save = @history[@first_new_line..-1]
|
|
File.open(filename, 'a') do |f|
|
|
history_to_save.each { |ln| f.puts ln }
|
|
end
|
|
@first_new_line = @history.length
|
|
end
|
|
|
|
def push(line)
|
|
line = line.to_s
|
|
unless line.empty? || (@history.last && line.strip == @history.last.strip)
|
|
Readline::HISTORY << line
|
|
@history << line
|
|
end
|
|
line
|
|
end
|
|
alias << push
|
|
|
|
def clear
|
|
Readline::HISTORY.shift until Readline::HISTORY.empty?
|
|
@history = []
|
|
@first_new_line = 0
|
|
end
|
|
|
|
def to_a
|
|
@history.dup
|
|
end
|
|
end
|
|
end
|