2012-05-17 21:00:33 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'digest/md5'
|
|
|
|
require 'gdbm'
|
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
class BashHistory
|
|
|
|
attr_reader :db
|
|
|
|
OPTIONS = {
|
2012-05-18 16:08:32 +00:00
|
|
|
:file => File.expand_path("~/.bash_history"),
|
|
|
|
:archive_file => File.expand_path("~/.bash_history.db"),
|
|
|
|
:time_format => "%F %T",
|
2012-05-17 21:00:33 +00:00
|
|
|
}
|
|
|
|
def initialize(opts = {})
|
|
|
|
@options = OPTIONS.merge(opts)
|
|
|
|
_parse
|
|
|
|
end
|
2012-05-18 16:18:02 +00:00
|
|
|
def time_format; @options[:time_format]; end
|
|
|
|
def time_format=(tf); @options[:time_format] = tf; end
|
2012-05-17 21:00:33 +00:00
|
|
|
def db
|
|
|
|
@db ||= GDBM.new(@options[:archive_file])
|
|
|
|
end
|
|
|
|
def keys; db.keys; end
|
|
|
|
def keys_to_i; keys.map {|i| i.to_i }; end
|
|
|
|
def values; db.map {|k,v| _yl(v) }; end
|
2012-06-01 04:22:57 +00:00
|
|
|
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
|
2012-05-17 21:00:33 +00:00
|
|
|
def commands; values.map {|v| v[:cmd] }; end
|
|
|
|
def _yd(data); YAML.dump(data); end
|
|
|
|
def _yl(data); YAML.load(data); end
|
|
|
|
def _md5(data); Digest::MD5.hexdigest(data); end
|
2012-05-18 16:08:32 +00:00
|
|
|
def _f(v); " %s %s" % [v[:time].strftime(@options[:time_format]), v[:cmd]]; end
|
2012-05-17 21:00:33 +00:00
|
|
|
|
|
|
|
def find(pat)
|
2012-06-01 04:22:57 +00:00
|
|
|
return values.select {|v|
|
|
|
|
v if v[:cmd] =~ /#{pat}/
|
|
|
|
}.map {|v|
|
|
|
|
v[:time].map {|t|
|
|
|
|
v.merge(:time => t)
|
|
|
|
}
|
|
|
|
}.flatten
|
2012-05-17 21:00:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def _parse
|
|
|
|
open(@options[:file]) do |f|
|
|
|
|
f.each_line do |line|
|
|
|
|
if line =~ /^#(.*)$/
|
|
|
|
l = f.readline.chomp
|
2012-06-01 04:22:57 +00:00
|
|
|
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
|
2012-05-17 21:00:33 +00:00
|
|
|
else
|
2012-06-01 04:22:57 +00:00
|
|
|
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
|
2012-05-17 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def render(file)
|
|
|
|
File.open(file,'w+') do |f|
|
|
|
|
values.each do |v|
|
2012-05-18 16:08:32 +00:00
|
|
|
f.write("#" + v[:time].to_i.to_s + "\n") if v[:time] and not (v[:time].to_i == 0)
|
2012-05-17 21:00:33 +00:00
|
|
|
f.write(v[:cmd] + "\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
2012-05-18 16:08:32 +00:00
|
|
|
require 'optparse'
|
|
|
|
options = {}
|
2012-05-18 16:28:58 +00:00
|
|
|
bh_options = {}
|
2012-05-18 16:08:32 +00:00
|
|
|
OptionParser.new do |opts|
|
|
|
|
opts.on('--inspect','inspect the data') do |o|
|
|
|
|
options[:inspect] = o
|
|
|
|
end
|
2012-05-18 16:41:09 +00:00
|
|
|
opts.on('-h','--history FILE','use bash_history FILE instead of the default (~/.bash_history)') do |o|
|
2012-05-18 16:28:58 +00:00
|
|
|
bh_options[:file] = o
|
|
|
|
end
|
2012-05-18 16:41:09 +00:00
|
|
|
opts.on('-d','--db FILE','use database FILE instead of the default (~/.bash_history.db)') do |o|
|
2012-05-18 16:28:58 +00:00
|
|
|
bh_options[:archive_file] = o
|
|
|
|
end
|
2012-05-18 16:08:32 +00:00
|
|
|
opts.on('-l','--list','list history') do |o|
|
|
|
|
options[:list] = o
|
|
|
|
end
|
|
|
|
opts.on('--fix','fix times') do |o|
|
|
|
|
options[:fix] = o
|
|
|
|
end
|
2012-05-18 16:18:02 +00:00
|
|
|
opts.on('--format FORMAT','specify a different strftime format. (default is "%F %T")') do |o|
|
2012-05-18 16:28:58 +00:00
|
|
|
bh_options[:time_format] = o
|
2012-05-18 16:18:02 +00:00
|
|
|
end
|
2012-05-18 16:08:32 +00:00
|
|
|
opts.on('-f','--find PAT','find a command with pattern PAT') do |o|
|
|
|
|
options[:find] = o
|
|
|
|
end
|
|
|
|
end.parse!(ARGV)
|
|
|
|
|
2012-05-18 16:28:58 +00:00
|
|
|
bh = BashHistory.new(bh_options)
|
|
|
|
|
2012-05-18 16:08:32 +00:00
|
|
|
if options[:inspect]
|
|
|
|
p bh
|
|
|
|
p "storing #{bh.keys.count} commands"
|
|
|
|
end
|
|
|
|
if options[:fix]
|
|
|
|
count = 0
|
|
|
|
bh.db.each_pair do |k,v|
|
|
|
|
yv = bh._yl(v)
|
|
|
|
if yv[:time].nil?
|
2012-06-01 04:22:57 +00:00
|
|
|
yv[:time] = [Time.at(0)]
|
|
|
|
bh.db[k] = bh._yd(yv)
|
|
|
|
count += 1
|
|
|
|
elsif not yv[:time].kind_of? Array
|
|
|
|
yv[:time] = [yv[:time]]
|
2012-05-18 16:08:32 +00:00
|
|
|
bh.db[k] = bh._yd(yv)
|
|
|
|
count += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
puts "fixed [#{count}] times values"
|
|
|
|
end
|
|
|
|
if options[:find]
|
2012-06-01 04:22:57 +00:00
|
|
|
bh.find(options[:find]).sort_by {|x| x[:time] }.each do |val|
|
2012-05-18 16:08:32 +00:00
|
|
|
puts bh._f(val)
|
|
|
|
end
|
|
|
|
elsif options[:list]
|
|
|
|
bh.values_by_time.each do |val|
|
|
|
|
puts bh._f(val)
|
|
|
|
end
|
|
|
|
end
|
2012-05-17 21:00:33 +00:00
|
|
|
end
|
|
|
|
|