Line | |
---|
1 | #!/usr/local/bin/ruby
|
---|
2 | #
|
---|
3 | # save-history.rb -
|
---|
4 | # $Release Version: 0.9.5$
|
---|
5 | # $Revision: 11708 $
|
---|
6 | # $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
|
---|
7 | # by Keiju [email protected])
|
---|
8 | #
|
---|
9 | # --
|
---|
10 | #
|
---|
11 | #
|
---|
12 | #
|
---|
13 |
|
---|
14 | require "readline"
|
---|
15 |
|
---|
16 | module IRB
|
---|
17 | module HistorySavingAbility
|
---|
18 | @RCS_ID='-$Id: save-history.rb 11708 2007-02-12 23:01:19Z 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 | obj.load_history
|
---|
71 | obj
|
---|
72 | end
|
---|
73 |
|
---|
74 | def load_history
|
---|
75 | hist = IRB.conf[:HISTORY_FILE]
|
---|
76 | hist = IRB.rc_file("_history") unless hist
|
---|
77 | if File.exist?(hist)
|
---|
78 | open(hist) do |f|
|
---|
79 | f.each {|l| HISTORY << l.chomp}
|
---|
80 | end
|
---|
81 | end
|
---|
82 | end
|
---|
83 | end
|
---|
84 | end
|
---|
85 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.