2015-12-16 05:07:31 +00:00
|
|
|
# frozen_string_literal: false
|
2009-03-06 03:56:38 +00:00
|
|
|
# save-history.rb -
|
2009-07-07 11:36:20 +00:00
|
|
|
# $Release Version: 0.9.6$
|
2005-04-13 15:30:15 +00:00
|
|
|
# $Revision$
|
2008-10-04 03:28:19 +00:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2005-04-13 15:30:15 +00:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-06 03:56:38 +00:00
|
|
|
#
|
2005-04-13 15:30:15 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
module IRB
|
2012-12-21 05:45:50 +00:00
|
|
|
module HistorySavingAbility # :nodoc:
|
2005-04-14 09:58:18 +00:00
|
|
|
end
|
|
|
|
|
2005-04-13 15:30:15 +00:00
|
|
|
class Context
|
2012-12-21 05:45:50 +00:00
|
|
|
def init_save_history# :nodoc:
|
2005-04-13 15:30:15 +00:00
|
|
|
unless (class<<@io;self;end).include?(HistorySavingAbility)
|
2014-08-09 01:36:49 +00:00
|
|
|
@io.extend(HistorySavingAbility)
|
2005-04-13 15:30:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 05:45:50 +00:00
|
|
|
# A copy of the default <code>IRB.conf[:SAVE_HISTORY]</code>
|
2005-04-13 15:30:15 +00:00
|
|
|
def save_history
|
|
|
|
IRB.conf[:SAVE_HISTORY]
|
|
|
|
end
|
|
|
|
|
2020-08-12 13:15:29 +09:00
|
|
|
remove_method(:save_history=) if method_defined?(:save_history=)
|
2012-12-21 05:45:50 +00:00
|
|
|
# Sets <code>IRB.conf[:SAVE_HISTORY]</code> to the given +val+ and calls
|
|
|
|
# #init_save_history with this context.
|
2013-02-04 23:04:09 +00:00
|
|
|
#
|
|
|
|
# Will store the number of +val+ entries of history in the #history_file
|
|
|
|
#
|
|
|
|
# Add the following to your +.irbrc+ to change the number of history
|
|
|
|
# entries stored to 1000:
|
|
|
|
#
|
|
|
|
# IRB.conf[:SAVE_HISTORY] = 1000
|
2005-04-13 15:30:15 +00:00
|
|
|
def save_history=(val)
|
|
|
|
IRB.conf[:SAVE_HISTORY] = val
|
|
|
|
if val
|
2014-08-09 01:36:49 +00:00
|
|
|
main_context = IRB.conf[:MAIN_CONTEXT]
|
|
|
|
main_context = self unless main_context
|
|
|
|
main_context.init_save_history
|
2005-04-13 15:30:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 05:45:50 +00:00
|
|
|
# A copy of the default <code>IRB.conf[:HISTORY_FILE]</code>
|
2005-04-13 15:30:15 +00:00
|
|
|
def history_file
|
|
|
|
IRB.conf[:HISTORY_FILE]
|
|
|
|
end
|
|
|
|
|
2012-12-21 05:45:50 +00:00
|
|
|
# Set <code>IRB.conf[:HISTORY_FILE]</code> to the given +hist+.
|
2005-04-13 15:30:15 +00:00
|
|
|
def history_file=(hist)
|
|
|
|
IRB.conf[:HISTORY_FILE] = hist
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 05:45:50 +00:00
|
|
|
module HistorySavingAbility # :nodoc:
|
2005-04-13 15:30:15 +00:00
|
|
|
def HistorySavingAbility.extended(obj)
|
2009-07-21 15:39:51 +00:00
|
|
|
IRB.conf[:AT_EXIT].push proc{obj.save_history}
|
2005-04-13 15:30:15 +00:00
|
|
|
obj.load_history
|
|
|
|
obj
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_history
|
2019-05-27 06:23:47 +09:00
|
|
|
return unless self.class.const_defined?(:HISTORY)
|
|
|
|
history = self.class::HISTORY
|
2008-10-04 03:26:16 +00:00
|
|
|
if history_file = IRB.conf[:HISTORY_FILE]
|
2014-08-09 01:36:49 +00:00
|
|
|
history_file = File.expand_path(history_file)
|
2008-10-04 03:26:16 +00:00
|
|
|
end
|
|
|
|
history_file = IRB.rc_file("_history") unless history_file
|
|
|
|
if File.exist?(history_file)
|
2020-01-14 15:40:03 +09:00
|
|
|
open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f|
|
2019-07-15 07:51:57 +09:00
|
|
|
f.each { |l|
|
|
|
|
l = l.chomp
|
2019-08-22 05:07:59 +09:00
|
|
|
if self.class == ReidlineInputMethod and history.last&.end_with?("\\")
|
2019-07-15 07:51:57 +09:00
|
|
|
history.last.delete_suffix!("\\")
|
|
|
|
history.last << "\n" << l
|
|
|
|
else
|
|
|
|
history << l
|
|
|
|
end
|
|
|
|
}
|
2014-08-09 01:36:49 +00:00
|
|
|
end
|
2021-03-02 12:17:23 -08:00
|
|
|
@loaded_history_lines = history.size
|
|
|
|
@loaded_history_mtime = File.mtime(history_file)
|
2005-04-13 15:30:15 +00:00
|
|
|
end
|
|
|
|
end
|
2009-07-21 15:39:51 +00:00
|
|
|
|
|
|
|
def save_history
|
2019-05-27 06:23:47 +09:00
|
|
|
return unless self.class.const_defined?(:HISTORY)
|
|
|
|
history = self.class::HISTORY
|
2020-08-08 20:48:23 +09:00
|
|
|
if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) != 0
|
2014-08-09 01:36:49 +00:00
|
|
|
if history_file = IRB.conf[:HISTORY_FILE]
|
|
|
|
history_file = File.expand_path(history_file)
|
|
|
|
end
|
|
|
|
history_file = IRB.rc_file("_history") unless history_file
|
2013-01-14 14:17:18 +00:00
|
|
|
|
2014-08-09 01:36:49 +00:00
|
|
|
# Change the permission of a file that already exists[BUG #7694]
|
|
|
|
begin
|
|
|
|
if File.stat(history_file).mode & 066 != 0
|
|
|
|
File.chmod(0600, history_file)
|
|
|
|
end
|
|
|
|
rescue Errno::ENOENT
|
2019-06-24 11:13:09 -07:00
|
|
|
rescue Errno::EPERM
|
|
|
|
return
|
2014-08-09 01:36:49 +00:00
|
|
|
rescue
|
|
|
|
raise
|
|
|
|
end
|
2013-01-14 14:17:18 +00:00
|
|
|
|
2021-03-02 12:17:23 -08:00
|
|
|
if File.exist?(history_file) && @loaded_history_mtime &&
|
|
|
|
File.mtime(history_file) != @loaded_history_mtime
|
|
|
|
history = history[@loaded_history_lines..-1]
|
|
|
|
append_history = true
|
|
|
|
end
|
|
|
|
|
|
|
|
open(history_file, "#{append_history ? 'a' : 'w'}:#{IRB.conf[:LC_MESSAGES].encoding}", 0600) do |f|
|
2019-07-15 10:20:07 +09:00
|
|
|
hist = history.map{ |l| l.split("\n").join("\\\n") }
|
2021-03-02 12:17:23 -08:00
|
|
|
unless append_history
|
|
|
|
begin
|
|
|
|
hist = hist.last(num) if hist.size > num and num > 0
|
|
|
|
rescue RangeError # bignum too big to convert into `long'
|
|
|
|
# Do nothing because the bignum should be treated as inifinity
|
|
|
|
end
|
2020-08-07 23:42:51 +09:00
|
|
|
end
|
|
|
|
f.puts(hist)
|
2014-08-09 01:36:49 +00:00
|
|
|
end
|
2009-07-21 15:39:51 +00:00
|
|
|
end
|
|
|
|
end
|
2005-04-13 15:30:15 +00:00
|
|
|
end
|
|
|
|
end
|