From 789ea644acada751674354bd37753a7f77310078 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 20 Sep 2012 22:26:27 -0400 Subject: [PATCH] -- splitting th loading out to a different step -- making an abstract history class -- adding a History marshaller to the oldHistory. --- bin/bash_history.rb | 1 + lib/persistent-shell-history.rb | 2 +- lib/persistent-shell-history/history.rb | 13 +++++++++--- .../old-history-store.rb | 20 ++++++++++++++++--- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/bin/bash_history.rb b/bin/bash_history.rb index 2f81789..3cc1559 100755 --- a/bin/bash_history.rb +++ b/bin/bash_history.rb @@ -30,6 +30,7 @@ OptionParser.new do |opts| end.parse!(ARGV) bh = Persistent::Shell::OldHistoryStore.new(bh_options) +bh.load() if options[:inspect] p bh diff --git a/lib/persistent-shell-history.rb b/lib/persistent-shell-history.rb index e768a8b..e9b4499 100644 --- a/lib/persistent-shell-history.rb +++ b/lib/persistent-shell-history.rb @@ -1,5 +1,5 @@ require 'persistent-shell-history/version' -require 'persistent-shell-history/bash-history' +require 'persistent-shell-history/history' module Persistent module Shell diff --git a/lib/persistent-shell-history/history.rb b/lib/persistent-shell-history/history.rb index af0cd15..5e8a93c 100644 --- a/lib/persistent-shell-history/history.rb +++ b/lib/persistent-shell-history/history.rb @@ -3,15 +3,22 @@ require 'persistent-shell-history/command' module Persistent module Shell + # Abstract storage for command history class History + def initialize() + @cmds = Array.new + end + def commands; @cmds; end + def commands=(cmds); @cmds = cmds; end + def <<(arg); @cmds << arg; end + end + class BashHistory < History def initialize(filename = '~/.bash_history') @filename = File.expand_path(filename) end + def commands; (@cmds.nil? or @cmds.empty?) ? (@cmds = parse) : @cmds; end def file; @filename; end def file=(filename); @filename = File.expand_path(filename); end - def commands - @cmds ||= parse - end def parse(filename = @filename) cmds = Array.new open(filename) do |f| diff --git a/lib/persistent-shell-history/old-history-store.rb b/lib/persistent-shell-history/old-history-store.rb index 0469347..53828ea 100644 --- a/lib/persistent-shell-history/old-history-store.rb +++ b/lib/persistent-shell-history/old-history-store.rb @@ -4,6 +4,8 @@ require 'gdbm' require 'yaml' require 'persistent-shell-history/abstract-history-store' +require 'persistent-shell-history/history' +require 'persistent-shell-history/command' module Persistent module Shell @@ -16,7 +18,7 @@ module Persistent } def initialize(opts = {}) @options = OPTIONS.merge(opts) - _load unless db.has_key? "schema_version" + #load() unless db.has_key? "schema_version" end def time_format; @options[:time_format]; end def time_format=(tf); @options[:time_format] = tf; end @@ -52,8 +54,7 @@ module Persistent }.flatten end - def _load(filename = @options[:file]) - #History.new(file).parse + def load(filename = @options[:file]) open(filename) do |f| f.each_line do |line| if line =~ /^#(.*)$/ @@ -88,6 +89,19 @@ module Persistent end end + # returns a Persistent::Shell::History object from the current GDBM database. + # intended for marshalling to other history-stores + 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 + + # create an output that looks like a regular ~/.bash_history file def render(file) File.open(file,'w+') do |f| values.each do |v|