-- splitting th loading out to a different step

-- making an abstract history class
-- adding a History marshaller to the oldHistory.
This commit is contained in:
Vincent Batts 2012-09-20 22:26:27 -04:00
parent 76afa0e48c
commit 789ea644ac
4 changed files with 29 additions and 7 deletions

View File

@ -30,6 +30,7 @@ OptionParser.new do |opts|
end.parse!(ARGV) end.parse!(ARGV)
bh = Persistent::Shell::OldHistoryStore.new(bh_options) bh = Persistent::Shell::OldHistoryStore.new(bh_options)
bh.load()
if options[:inspect] if options[:inspect]
p bh p bh

View File

@ -1,5 +1,5 @@
require 'persistent-shell-history/version' require 'persistent-shell-history/version'
require 'persistent-shell-history/bash-history' require 'persistent-shell-history/history'
module Persistent module Persistent
module Shell module Shell

View File

@ -3,15 +3,22 @@ require 'persistent-shell-history/command'
module Persistent module Persistent
module Shell module Shell
# Abstract storage for command history
class 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') def initialize(filename = '~/.bash_history')
@filename = File.expand_path(filename) @filename = File.expand_path(filename)
end end
def commands; (@cmds.nil? or @cmds.empty?) ? (@cmds = parse) : @cmds; end
def file; @filename; end def file; @filename; end
def file=(filename); @filename = File.expand_path(filename); end def file=(filename); @filename = File.expand_path(filename); end
def commands
@cmds ||= parse
end
def parse(filename = @filename) def parse(filename = @filename)
cmds = Array.new cmds = Array.new
open(filename) do |f| open(filename) do |f|

View File

@ -4,6 +4,8 @@ require 'gdbm'
require 'yaml' require 'yaml'
require 'persistent-shell-history/abstract-history-store' require 'persistent-shell-history/abstract-history-store'
require 'persistent-shell-history/history'
require 'persistent-shell-history/command'
module Persistent module Persistent
module Shell module Shell
@ -16,7 +18,7 @@ module Persistent
} }
def initialize(opts = {}) def initialize(opts = {})
@options = OPTIONS.merge(opts) @options = OPTIONS.merge(opts)
_load unless db.has_key? "schema_version" #load() unless db.has_key? "schema_version"
end end
def time_format; @options[:time_format]; end def time_format; @options[:time_format]; end
def time_format=(tf); @options[:time_format] = tf; end def time_format=(tf); @options[:time_format] = tf; end
@ -52,8 +54,7 @@ module Persistent
}.flatten }.flatten
end end
def _load(filename = @options[:file]) def load(filename = @options[:file])
#History.new(file).parse
open(filename) do |f| open(filename) do |f|
f.each_line do |line| f.each_line do |line|
if line =~ /^#(.*)$/ if line =~ /^#(.*)$/
@ -88,6 +89,19 @@ module Persistent
end end
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) def render(file)
File.open(file,'w+') do |f| File.open(file,'w+') do |f|
values.each do |v| values.each do |v|