1
0
Fork 0
mirror of https://github.com/vbatts/persistent-shell-history.git synced 2024-11-25 17:05:39 +00:00
persistent-shell-history/bin/bash_history.rb

90 lines
2.5 KiB
Bash
Raw Normal View History

2012-05-17 21:00:33 +00:00
#!/usr/bin/env ruby
require 'persistent-shell-history/binary-history-store'
require 'persistent-shell-history/old-history-store'
2012-08-24 00:17:36 +00:00
require 'optparse'
2012-05-17 21:00:33 +00:00
2012-08-24 00:17:36 +00:00
options = {}
bh_options = {:archive_file => File.expand_path("~/.bash_history.db")}
opts = OptionParser.new do |opts|
2012-08-24 00:17:36 +00:00
opts.on('--inspect','inspect the data') do |o|
options[:inspect] = o
2012-05-17 21:00:33 +00:00
end
2012-08-24 00:17:36 +00:00
opts.on('-h','--history FILE','use bash_history FILE instead of the default (~/.bash_history)') do |o|
bh_options[:file] = o
2012-05-17 21:00:33 +00:00
end
2012-08-24 00:17:36 +00:00
opts.on('-d','--db FILE','use database FILE instead of the default (~/.bash_history.db)') do |o|
bh_options[:archive_file] = o
end
opts.on('--migrate','check-for and migrate, only') do |o|
options[:migrate] = o
end
2012-08-24 00:17:36 +00:00
opts.on('-l','--list','list history') do |o|
options[:list] = o
2012-05-17 21:00:33 +00:00
end
2012-08-24 00:17:36 +00:00
opts.on('--format FORMAT','specify a different strftime format. (default is "%F %T")') do |o|
bh_options[:time_format] = o
2012-05-17 21:00:33 +00:00
end
2012-08-24 00:17:36 +00:00
opts.on('-f','--find PAT','find a command with pattern PAT') do |o|
options[:find] = o
end
end
begin
opts.parse!(ARGV)
rescue => ex
puts ex
puts opts
end
def migrate(old_bashhistorystore)
require 'fileutils'
# migrate
temp_db = GDBM.new("#{old_bashhistorystore.archive}.#{$$}")
old_bashhistorystore.keys.each {|key|
h = old_bashhistorystore[key]
h[:time] = h[:time].map {|t| t.to_i }
temp_db[key] = Marshal.dump(h)
}
old_bashhistorystore.db.close()
temp_db.close()
ts = "#{Time.now.year}#{Time.now.month}#{Time.now.day}#{Time.now.hour}#{Time.now.min}"
old_filename = "#{old_bashhistorystore.archive}.old.#{ts}"
FileUtils.mv(old_bashhistorystore.archive, old_filename)
puts "archived [#{old_bashhistorystore.archive}] to [#{old_filename}] ..."
FileUtils.mv("#{old_bashhistorystore.archive}.#{$$}", old_bashhistorystore.archive)
end
# First check the database, for whether it is the old format,
# if so, convert it, and reopen it.
2012-08-24 01:20:08 +00:00
bh = Persistent::Shell::OldHistoryStore.new(bh_options)
migrate(bh) if bh.is_oldformat?
bh.db.close unless bh.db.closed?
exit(0) if options[:migrate]
# re-open the history storage
bh = Persistent::Shell::BinaryHistoryStore.new(bh_options)
# load the new bash_history into the database
unless (options[:inspect] or options[:find] or options[:list])
bh.load()
bh.db.reorganize()
end
2012-08-24 00:17:36 +00:00
if options[:inspect]
p bh
#p "storing #{bh.keys.count} commands"
2012-08-24 00:17:36 +00:00
end
if options[:find]
bh.find(options[:find]).sort_by {|x| x[:time] }.each do |val|
puts bh.fmt(val)
end
2012-08-24 00:17:36 +00:00
elsif options[:list]
bh.values_by_time.each do |val|
puts bh.fmt(val)
end
2012-05-17 21:00:33 +00:00
end
# vim: set sts=2 sw=2 et ai: