rearranging classes and beginning to enable the history to have various

datastore types (not just GDBM)
This commit is contained in:
Vincent Batts 2012-08-23 21:09:09 -04:00
parent eb3cdd71c8
commit 7f97ccc568
8 changed files with 164 additions and 89 deletions

View File

@ -29,7 +29,7 @@ OptionParser.new do |opts|
end
end.parse!(ARGV)
bh = BashHistory.new(bh_options)
bh = Persistent::Shell::DataStore.new(bh_options)
if options[:inspect]
p bh
@ -61,3 +61,4 @@ elsif options[:list]
end
end
# vim: set sts=2 sw=2 et ai:

View File

@ -3,8 +3,7 @@ require 'persistent-shell-history/bash-history'
module Persistent
module Shell
module History
# Your code goes here...
end
end
end
# vim: set sts=2 sw=2 et ai:

View File

@ -0,0 +1,17 @@
module Persistent
module Shell
class AbstractHistoryStore
SCHEMA_VERSION = "1"
def commands; end
def db; end
def shema_match?; db.has_key? "schema_version" and db["schema_version"] == SCHEMA_VERSION; end
def shema_version; db["schema_version"] if db.has_key? "schema_version" ; end
end # class AbstractHistoryStore
end
end
# vim: set sts=2 sw=2 et ai:

View File

@ -3,8 +3,12 @@ require 'digest/md5'
require 'gdbm'
require 'yaml'
class BashHistory
attr_reader :db
require 'persistent-shell-history/abstract-history-store'
module Persistent
module Shell
class HistoryStore < AbstractHistoryStore
SCHEMA_VERSION = "1"
OPTIONS = {
:file => File.expand_path("~/.bash_history"),
:archive_file => File.expand_path("~/.bash_history.db"),
@ -12,7 +16,7 @@ class BashHistory
}
def initialize(opts = {})
@options = OPTIONS.merge(opts)
_parse
_load if schema_match?
end
def time_format; @options[:time_format]; end
def time_format=(tf); @options[:time_format] = tf; end
@ -48,8 +52,9 @@ class BashHistory
}.flatten
end
def _parse
open(@options[:file]) do |f|
def _load(filename = @options[:file])
#History.new(file).parse
open(filename) do |f|
f.each_line do |line|
if line =~ /^#(.*)$/
l = f.readline.chomp
@ -82,6 +87,7 @@ class BashHistory
end
end
end
def render(file)
File.open(file,'w+') do |f|
values.each do |v|
@ -90,5 +96,8 @@ class BashHistory
end
end
end
end
end # class DataStore
end # module Shell
end # module Persistent
# vim: set sts=2 sw=2 et ai:

View File

@ -0,0 +1,16 @@
require 'digest/md5'
module Persistent
module Shell
class Command < Struct.new(:cmd, :time)
def md5
Digest::MD5.hexdigest(cmd)
end
def to_h
{ :cmd => cmd, :time => time, }
end
end
end
end
# vim: set sts=2 sw=2 et ai:

View File

@ -0,0 +1,33 @@
require 'persistent-shell-history/command'
module Persistent
module Shell
class History
def initialize(filename = '~/.bash_history')
@filename = File.expand_path(filename)
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|
f.each_line do |line|
if line =~ /^#(.*)$/
l = f.readline.chomp
cmds << Command.new(l, $1)
else
cmds << Command.new(line, "0")
end
end
end
return cmds
end
end
end
end
# vim: set sts=2 sw=2 et ai:

View File

@ -1,7 +1,7 @@
module Persistent
module Shell
module History
VERSION = "0.0.1"
end
end
end
# vim: set sts=2 sw=2 et ai:

View File

@ -4,7 +4,7 @@ require "persistent-shell-history/version"
Gem::Specification.new do |s|
s.name = "persistent-shell-history"
s.version = Persistent::Shell::History::VERSION
s.version = Persistent::Shell::VERSION
s.authors = ["Vincent Batts"]
s.email = ["vbatts@hashbangbash.com"]
s.homepage = ""