Class: Persistent::Shell::OldHistoryStore
Constant Summary
collapse
- OPTIONS =
{
:file => File.expand_path("~/.bash_history"),
:archive_file => File.expand_path("~/.bash_history.db"),
:time_format => "%F %T",
}
AbstractHistoryStore::SCHEMA_VERSION
Instance Method Summary
collapse
#shema_match?, #shema_version
Constructor Details
Returns a new instance of OldHistoryStore.
19
20
21
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 19
def initialize(opts = {})
@options = OPTIONS.merge(opts)
end
|
Instance Method Details
#[](key) ⇒ Object
49
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 49
def [](key); _yl(db[key]); end
|
#_md5(data) ⇒ Object
66
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 66
def _md5(data); Digest::MD5.hexdigest(data); end
|
#_yd(data) ⇒ Object
64
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 64
def _yd(data); YAML.dump(data); end
|
#_yl(data) ⇒ Object
65
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 65
def _yl(data); YAML.load(data); end
|
#archive ⇒ Object
23
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 23
def archive; @options[:archive_file]; end
|
#archive=(arg) ⇒ Object
24
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 24
def archive=(arg); @options[:archive_file] = arg; end
|
#commands ⇒ Object
63
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 63
def commands; values.map {|v| v[:cmd] }; end
|
#db ⇒ Object
46
47
48
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 46
def db
@db ||= GDBM.new(@options[:archive_file])
end
|
#find(pat) ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 71
def find(pat)
return values.select {|v|
v if v[:cmd] =~ /#{pat}/
}.map {|v|
v[:time].map {|t|
v.merge(:time => t)
}
}.flatten
end
|
#fmt(cmd) ⇒ Object
display a formatted time commend
69
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 69
def fmt(cmd); " %s %s" % [cmd[:time].strftime(@options[:time_format]), cmd[:cmd]]; end
|
#hist_file ⇒ Object
26
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 26
def hist_file; @options[:file]; end
|
#hist_file=(arg) ⇒ Object
27
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 27
def hist_file=(arg); @options[:file] = arg; end
|
check the archive, whether it’s the old format. If so, it needs to be converted to the BinaryHistoryStore
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 31
def is_oldformat?
db.keys[0..5].each {|key|
begin
YAML.load(db[key])
rescue Psych::SyntaxError => ex
return false
rescue => ex
return false
end
}
return true
end
|
#keys ⇒ Object
50
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 50
def keys; db.keys; end
|
#keys_to_i ⇒ Object
51
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 51
def keys_to_i; keys.map {|i| i.to_i }; end
|
#load(filename = ) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 81
def load(filename = @options[:file])
open(filename) do |f|
f.each_line do |line|
if line =~ /^#(.*)$/
l = f.readline.chomp
key = _md5(l)
if db.has_key?(key)
times = _yl(db[key])[:time]
if times.kind_of? Array
times.push(Time.at($1.to_i))
else
times = [times]
end
db[key] = _yd({:cmd => l, :time => times.uniq })
else
db[key] = _yd({:cmd => l, :time => [Time.at($1.to_i)] })
end
else
key = _md5(line.chomp)
if db.has_key?(key)
times = _yl(db[key])[:time]
if times.kind_of? Array
times.push(Time.at($1.to_i))
else
times = [times]
end
db[key] = _yd({:cmd => l, :time => times.uniq })
else
db[key] = _yd({:cmd => line.chomp, :time => [Time.at(0)] })
end
end
end
end
end
|
#render(file) ⇒ Object
create an output that looks like a regular ~/.bash_history file
129
130
131
132
133
134
135
136
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 129
def render(file)
File.open(file,'w+') do |f|
values.each do |v|
f.write("#" + v[:time].to_i.to_s + "\n") if v[:time] and not (v[:time].to_i == 0)
f.write(v[:cmd] + "\n")
end
end
end
|
44
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 44
def time_format; @options[:time_format]; end
|
45
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 45
def time_format=(tf); @options[:time_format] = tf; end
|
#to_history ⇒ Object
returns a Persistent::Shell::History object from the current GDBM database. intended for marshalling to other history-stores
118
119
120
121
122
123
124
125
126
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 118
def to_history
history = History.new
values.each do |value|
value[:time].each do |t|
history << Command.new(value[:cmd], t.to_i)
end
end
return history
end
|
#values ⇒ Object
52
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 52
def values; db.map {|k,v| _yl(v) }; end
|
#values_by_time ⇒ Object
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/persistent-shell-history/old-history-store.rb', line 53
def values_by_time
return db.map {|k,v|
data = _yl(v)
data[:time].map {|t|
data.merge(:time => t)
}
}.flatten.sort_by {|x|
x[:time]
}
end
|