1
0
Fork 0
mirror of https://github.com/vbatts/persistent-shell-history.git synced 2024-11-22 07:35:38 +00:00

adding flags and such, to be more useful

This commit is contained in:
Vincent Batts 2012-05-18 12:08:32 -04:00
parent f0c3b54a5f
commit f2043f30a9
2 changed files with 60 additions and 7 deletions

10
README
View file

@ -12,6 +12,16 @@ and set in ~/.bashrc:
export HISTCONTROL="ignoreboth" export HISTCONTROL="ignoreboth"
== USAGE
See the --help also,
Usage: bash_history [options]
--inspect inspect the data
-l, --list list history
--fix fix times
-f, --find PAT find a command with pattern PAT
== LICENSE == LICENSE
Copyright (c) 2012 Vincent Batts, Raleigh, NC, USA Copyright (c) 2012 Vincent Batts, Raleigh, NC, USA

View file

@ -9,6 +9,7 @@ class BashHistory
OPTIONS = { OPTIONS = {
:file => File.expand_path("~/.bash_history"), :file => File.expand_path("~/.bash_history"),
:archive_file => File.expand_path("~/.bash_history.db"), :archive_file => File.expand_path("~/.bash_history.db"),
:time_format => "%F %T",
} }
def initialize(opts = {}) def initialize(opts = {})
@options = OPTIONS.merge(opts) @options = OPTIONS.merge(opts)
@ -20,13 +21,15 @@ class BashHistory
def keys; db.keys; end def keys; db.keys; end
def keys_to_i; keys.map {|i| i.to_i }; end def keys_to_i; keys.map {|i| i.to_i }; end
def values; db.map {|k,v| _yl(v) }; end def values; db.map {|k,v| _yl(v) }; end
def values_by_time; db.map {|k,v| _yl(v) }.sort_by {|x| x[:time] } ; end
def commands; values.map {|v| v[:cmd] }; end def commands; values.map {|v| v[:cmd] }; end
def _yd(data); YAML.dump(data); end def _yd(data); YAML.dump(data); end
def _yl(data); YAML.load(data); end def _yl(data); YAML.load(data); end
def _md5(data); Digest::MD5.hexdigest(data); end def _md5(data); Digest::MD5.hexdigest(data); end
def _f(v); " %s %s" % [v[:time].strftime(@options[:time_format]), v[:cmd]]; end
def find(pat) def find(pat)
values.select {|v| v if v =~ /#{pat}/ } values.select {|v| v if v[:cmd] =~ /#{pat}/ }
end end
def _parse def _parse
@ -36,7 +39,7 @@ class BashHistory
l = f.readline.chomp l = f.readline.chomp
db[_md5(l)] = _yd({:cmd => l, :time => Time.at($1.to_i)}) db[_md5(l)] = _yd({:cmd => l, :time => Time.at($1.to_i)})
else else
db[_md5(line.chomp)] = _yd({:cmd => line.chomp}) db[_md5(line.chomp)] = _yd({:cmd => line.chomp, :time => Time.at(0) })
end end
end end
end end
@ -44,7 +47,7 @@ class BashHistory
def render(file) def render(file)
File.open(file,'w+') do |f| File.open(file,'w+') do |f|
values.each do |v| values.each do |v|
f.write("#" + v[:time].to_i.to_s + "\n") if v[:time] f.write("#" + v[:time].to_i.to_s + "\n") if v[:time] and not (v[:time].to_i == 0)
f.write(v[:cmd] + "\n") f.write(v[:cmd] + "\n")
end end
end end
@ -52,8 +55,48 @@ class BashHistory
end end
if $0 == __FILE__ if $0 == __FILE__
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.on('--inspect','inspect the data') do |o|
options[:inspect] = o
end
opts.on('-l','--list','list history') do |o|
options[:list] = o
end
opts.on('--fix','fix times') do |o|
options[:fix] = o
end
opts.on('-f','--find PAT','find a command with pattern PAT') do |o|
options[:find] = o
end
end.parse!(ARGV)
bh = BashHistory.new bh = BashHistory.new
if options[:inspect]
p bh p bh
p "storing #{bh.keys.count} commands" 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?
yv[:time] = Time.at(0)
bh.db[k] = bh._yd(yv)
count += 1
end
end
puts "fixed [#{count}] times values"
end
if options[:find]
bh.find(options[:find]).sort_by {|x| x[:time]}.each do |val|
puts bh._f(val)
end
elsif options[:list]
bh.values_by_time.each do |val|
puts bh._f(val)
end
end
end end