PageRenderTime 142ms CodeModel.GetById 134ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/irb/ext/save-history.rb

http://github.com/agross/netopenspace
Ruby | 99 lines | 59 code | 13 blank | 27 comment | 5 complexity | 01fa2d6d7f86fe4b469a3a2ca67a1323 MD5 | raw file
 1#!/usr/local/bin/ruby
 2#
 3#   save-history.rb - 
 4#   	$Release Version: 0.9.5$
 5#   	$Revision: 24483 $
 6#   	$Date: 2009-08-09 17:44:15 +0900 (Sun, 09 Aug 2009) $
 7#   	by Keiju ISHITSUKAkeiju@ruby-lang.org)
 8#
 9# --
10#
11#   
12#
13
14require "readline"
15
16module IRB
17  module HistorySavingAbility
18    @RCS_ID='-$Id: save-history.rb 24483 2009-08-09 08:44:15Z shyouhei $-'
19  end
20
21  class Context
22    def init_save_history
23      unless (class<<@io;self;end).include?(HistorySavingAbility)
24	@io.extend(HistorySavingAbility)
25      end
26    end
27
28    def save_history
29      IRB.conf[:SAVE_HISTORY]
30    end
31
32    def save_history=(val)
33      IRB.conf[:SAVE_HISTORY] = val
34      if val
35	main_context = IRB.conf[:MAIN_CONTEXT]
36	main_context = self unless main_context
37	main_context.init_save_history
38      end
39    end
40
41    def history_file
42      IRB.conf[:HISTORY_FILE]
43    end
44
45    def history_file=(hist)
46      IRB.conf[:HISTORY_FILE] = hist
47    end
48  end
49
50  module HistorySavingAbility
51    include Readline
52
53#     def HistorySavingAbility.create_finalizer
54#       proc do
55# 	if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
56# 	  if hf = IRB.conf[:HISTORY_FILE]
57# 	    file = File.expand_path(hf)
58# 	  end
59# 	  file = IRB.rc_file("_history") unless file
60# 	  open(file, 'w' ) do |f|
61# 	    hist = HISTORY.to_a
62# 	    f.puts(hist[-num..-1] || hist)
63# 	  end
64# 	end
65#       end
66#     end
67
68    def HistorySavingAbility.extended(obj)
69#      ObjectSpace.define_finalizer(obj, HistorySavingAbility.create_finalizer)
70      IRB.conf[:AT_EXIT].push proc{obj.save_history}
71      obj.load_history
72      obj
73    end
74
75    def load_history
76      hist = IRB.conf[:HISTORY_FILE]
77      hist = IRB.rc_file("_history") unless hist
78      if File.exist?(hist)
79	open(hist) do |f|
80	  f.each {|l| HISTORY << l.chomp}
81	end
82      end
83    end
84
85    def save_history
86      if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
87	if history_file = IRB.conf[:HISTORY_FILE]
88	  history_file = File.expand_path(history_file)
89	end
90	history_file = IRB.rc_file("_history") unless history_file
91	open(history_file, 'w' ) do |f|
92	  hist = HISTORY.to_a
93	  f.puts(hist[-num..-1] || hist)
94	end
95      end
96    end
97  end
98end
99