2005-04-13 19:30:15 +04:00
|
|
|
module IRB
|
2012-12-21 09:45:50 +04:00
|
|
|
module HistorySavingAbility # :nodoc:
|
2023-08-09 17:57:47 +03:00
|
|
|
def support_history_saving?
|
|
|
|
true
|
2005-04-13 19:30:15 +04:00
|
|
|
end
|
|
|
|
|
2023-08-13 21:30:30 +03:00
|
|
|
def reset_history_counter
|
|
|
|
@loaded_history_lines = self.class::HISTORY.size if defined? @loaded_history_lines
|
|
|
|
end
|
|
|
|
|
2005-04-13 19:30:15 +04:00
|
|
|
def load_history
|
2019-05-27 00:23:47 +03:00
|
|
|
history = self.class::HISTORY
|
2023-09-16 15:48:26 +03:00
|
|
|
|
2008-10-04 07:26:16 +04:00
|
|
|
if history_file = IRB.conf[:HISTORY_FILE]
|
2014-08-09 05:36:49 +04:00
|
|
|
history_file = File.expand_path(history_file)
|
2008-10-04 07:26:16 +04:00
|
|
|
end
|
|
|
|
history_file = IRB.rc_file("_history") unless history_file
|
|
|
|
if File.exist?(history_file)
|
2022-11-30 13:11:42 +03:00
|
|
|
File.open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f|
|
2019-07-15 01:51:57 +03:00
|
|
|
f.each { |l|
|
|
|
|
l = l.chomp
|
2022-10-05 14:38:51 +03:00
|
|
|
if self.class == RelineInputMethod and history.last&.end_with?("\\")
|
2019-07-15 01:51:57 +03:00
|
|
|
history.last.delete_suffix!("\\")
|
|
|
|
history.last << "\n" << l
|
|
|
|
else
|
|
|
|
history << l
|
|
|
|
end
|
|
|
|
}
|
2014-08-09 05:36:49 +04:00
|
|
|
end
|
2021-03-02 23:17:23 +03:00
|
|
|
@loaded_history_lines = history.size
|
|
|
|
@loaded_history_mtime = File.mtime(history_file)
|
2005-04-13 19:30:15 +04:00
|
|
|
end
|
|
|
|
end
|
2009-07-21 19:39:51 +04:00
|
|
|
|
|
|
|
def save_history
|
2023-09-16 15:48:26 +03:00
|
|
|
history = self.class::HISTORY.to_a
|
|
|
|
|
2020-08-08 14:48:23 +03:00
|
|
|
if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) != 0
|
2014-08-09 05: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 18:17:18 +04:00
|
|
|
|
2014-08-09 05: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 21:13:09 +03:00
|
|
|
rescue Errno::EPERM
|
|
|
|
return
|
2014-08-09 05:36:49 +04:00
|
|
|
rescue
|
|
|
|
raise
|
|
|
|
end
|
2013-01-14 18:17:18 +04:00
|
|
|
|
2022-09-14 04:15:32 +03:00
|
|
|
if File.exist?(history_file) &&
|
2021-03-02 23:17:23 +03:00
|
|
|
File.mtime(history_file) != @loaded_history_mtime
|
2022-09-14 04:15:32 +03:00
|
|
|
history = history[@loaded_history_lines..-1] if @loaded_history_lines
|
2021-03-02 23:17:23 +03:00
|
|
|
append_history = true
|
|
|
|
end
|
|
|
|
|
2022-09-14 05:05:43 +03:00
|
|
|
File.open(history_file, (append_history ? 'a' : 'w'), 0o600, encoding: IRB.conf[:LC_MESSAGES]&.encoding) do |f|
|
2019-07-15 04:20:07 +03:00
|
|
|
hist = history.map{ |l| l.split("\n").join("\\\n") }
|
2021-03-02 23:17:23 +03: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 17:42:51 +03:00
|
|
|
end
|
|
|
|
f.puts(hist)
|
2014-08-09 05:36:49 +04:00
|
|
|
end
|
2009-07-21 19:39:51 +04:00
|
|
|
end
|
|
|
|
end
|
2005-04-13 19:30:15 +04:00
|
|
|
end
|
|
|
|
end
|