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
|
|
|
#
|
|
|
|
|
|
|
|
require "readline"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-04-15 07:27:38 -04:00
|
|
|
remove_method :save_history= if respond_to?(: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
|
|
|
include Readline
|
|
|
|
|
|
|
|
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
|
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)
|
2014-08-08 21:36:49 -04:00
|
|
|
open(history_file) do |f|
|
|
|
|
f.each {|l| HISTORY << l.chomp}
|
|
|
|
end
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
end
|
2009-07-21 11:39:51 -04:00
|
|
|
|
|
|
|
def save_history
|
|
|
|
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
|
|
|
|
rescue
|
|
|
|
raise
|
|
|
|
end
|
2013-01-14 09:17:18 -05:00
|
|
|
|
2014-08-08 21:36:49 -04:00
|
|
|
open(history_file, 'w', 0600 ) do |f|
|
|
|
|
hist = HISTORY.to_a
|
|
|
|
f.puts(hist[-num..-1] || hist)
|
|
|
|
end
|
2009-07-21 11:39:51 -04:00
|
|
|
end
|
|
|
|
end
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
end
|