Class: Persistent::Shell::BinaryHistoryStore
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 BinaryHistoryStore.
23
24
25
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 23
def initialize(opts = {})
@options = OPTIONS.merge(opts)
end
|
Instance Method Details
#[](key) ⇒ Object
38
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 38
def [](key); _ml(db[key]); end
|
#_md(data) ⇒ Object
52
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 52
def _md(data); Marshal.dump(data); end
|
#_md5(data) ⇒ Object
54
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 54
def _md5(data); Digest::MD5.hexdigest(data); end
|
#_ml(data) ⇒ Object
53
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 53
def _ml(data); Marshal.load(data); end
|
#archive ⇒ Object
27
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 27
def archive; @options[:archive_file]; end
|
#archive=(arg) ⇒ Object
28
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 28
def archive=(arg); @options[:archive_file] = arg; end
|
#commands ⇒ Object
51
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 51
def commands; values.map {|v| v[:cmd] }; end
|
#db ⇒ Object
35
36
37
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 35
def db
@db ||= GDBM.new(@options[:archive_file])
end
|
#find(pat) ⇒ Object
59
60
61
62
63
64
65
66
67
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 59
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
57
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 57
def fmt(cmd); " %s %s" % [Time.at(cmd[:time]).strftime(@options[:time_format]), cmd[:cmd]]; end
|
#hist_file ⇒ Object
30
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 30
def hist_file; @options[:file]; end
|
#hist_file=(arg) ⇒ Object
31
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 31
def hist_file=(arg); @options[:file] = arg; end
|
#keys ⇒ Object
39
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 39
def keys; db.keys; end
|
#load(filename = ) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 69
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 = _ml(db[key])[:time]
if times.kind_of? Array
times.push($1.to_i)
else
times = [times]
end
db[key] = _md({:cmd => l, :time => times.uniq })
else
db[key] = _md({:cmd => l, :time => [$1.to_i] })
end
else
key = _md5(line.chomp)
if db.has_key?(key)
times = _ml(db[key])[:time]
if times.kind_of? Array
times.push($1.to_i)
else
times = [times]
end
db[key] = _md({:cmd => l, :time => times.uniq })
else
db[key] = _md({:cmd => line.chomp, :time => [0] })
end
end
end
end
end
|
#render(file) ⇒ Object
create an output that looks like a regular ~/.bash_history file
117
118
119
120
121
122
123
124
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 117
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
|
33
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 33
def time_format; @options[:time_format]; end
|
34
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 34
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
106
107
108
109
110
111
112
113
114
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 106
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
40
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 40
def values; db.map {|k,v| _ml(v) }; end
|
#values_by_time ⇒ Object
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/persistent-shell-history/binary-history-store.rb', line 41
def values_by_time
return db.map {|k,v|
data = _ml(v)
data[:time].map {|t|
data.merge(:time => t)
}
}.flatten.sort_by {|x|
x[:time]
}
end
|