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

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
end.parse!(ARGV) end.parse!(ARGV)
bh = BashHistory.new(bh_options) bh = Persistent::Shell::DataStore.new(bh_options)
if options[:inspect] if options[:inspect]
p bh p bh
@ -61,3 +61,4 @@ elsif options[:list]
end end
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 Persistent
module Shell module Shell
module History
# Your code goes here...
end
end 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 'gdbm'
require 'yaml' require 'yaml'
class BashHistory require 'persistent-shell-history/abstract-history-store'
attr_reader :db
module Persistent
module Shell
class HistoryStore < AbstractHistoryStore
SCHEMA_VERSION = "1"
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"),
@ -12,7 +16,7 @@ class BashHistory
} }
def initialize(opts = {}) def initialize(opts = {})
@options = OPTIONS.merge(opts) @options = OPTIONS.merge(opts)
_parse _load if schema_match?
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
@ -48,8 +52,9 @@ class BashHistory
}.flatten }.flatten
end end
def _parse def _load(filename = @options[:file])
open(@options[:file]) do |f| #History.new(file).parse
open(filename) do |f|
f.each_line do |line| f.each_line do |line|
if line =~ /^#(.*)$/ if line =~ /^#(.*)$/
l = f.readline.chomp l = f.readline.chomp
@ -82,6 +87,7 @@ class BashHistory
end end
end end
end end
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|
@ -90,5 +96,8 @@ class BashHistory
end end
end 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 Persistent
module Shell module Shell
module History
VERSION = "0.0.1" VERSION = "0.0.1"
end end
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| Gem::Specification.new do |s|
s.name = "persistent-shell-history" s.name = "persistent-shell-history"
s.version = Persistent::Shell::History::VERSION s.version = Persistent::Shell::VERSION
s.authors = ["Vincent Batts"] s.authors = ["Vincent Batts"]
s.email = ["vbatts@hashbangbash.com"] s.email = ["vbatts@hashbangbash.com"]
s.homepage = "" s.homepage = ""