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
|
|
|
@RCS_ID='-$Id$-'
|
|
|
|
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)
|
|
|
|
@io.extend(HistorySavingAbility)
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
main_context = IRB.conf[:MAIN_CONTEXT]
|
|
|
|
main_context = self unless main_context
|
|
|
|
main_context.init_save_history
|
|
|
|
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
|
|
|
|
|
2009-07-21 11:39:51 -04:00
|
|
|
# def HistorySavingAbility.create_finalizer
|
|
|
|
# proc do
|
|
|
|
# if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
|
|
|
|
# if hf = IRB.conf[:HISTORY_FILE]
|
|
|
|
# file = File.expand_path(hf)
|
|
|
|
# end
|
|
|
|
# file = IRB.rc_file("_history") unless file
|
|
|
|
# open(file, 'w' ) do |f|
|
|
|
|
# hist = HISTORY.to_a
|
|
|
|
# f.puts(hist[-num..-1] || hist)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2005-04-13 11:30:15 -04:00
|
|
|
|
|
|
|
def HistorySavingAbility.extended(obj)
|
2009-07-21 11:39:51 -04:00
|
|
|
# ObjectSpace.define_finalizer(obj, HistorySavingAbility.create_finalizer)
|
|
|
|
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]
|
|
|
|
history_file = File.expand_path(history_file)
|
|
|
|
end
|
|
|
|
history_file = IRB.rc_file("_history") unless history_file
|
|
|
|
if File.exist?(history_file)
|
|
|
|
open(history_file) do |f|
|
2005-04-13 11:30:15 -04:00
|
|
|
f.each {|l| HISTORY << l.chomp}
|
|
|
|
end
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
# Change the permission of a file that already exists[BUG #7694]
|
|
|
|
begin
|
2013-01-15 08:42:09 -05:00
|
|
|
if File.stat(history_file).mode & 066 != 0
|
2013-01-14 09:17:18 -05:00
|
|
|
File.chmod(0600, history_file)
|
|
|
|
end
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
rescue
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
open(history_file, 'w', 0600 ) do |f|
|
2009-07-21 11:39:51 -04:00
|
|
|
hist = HISTORY.to_a
|
|
|
|
f.puts(hist[-num..-1] || hist)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-04-13 11:30:15 -04:00
|
|
|
end
|
|
|
|
end
|