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