Class: Persistent::Shell::BashHistory

Inherits:
History
  • Object
show all
Defined in:
lib/persistent-shell-history/history.rb

Instance Method Summary collapse

Methods inherited from History

#<<, #commands=, #to_a, #to_json

Constructor Details

#initialize(filename = '~/.bash_history') ⇒ BashHistory

Returns a new instance of BashHistory.



19
20
21
# File 'lib/persistent-shell-history/history.rb', line 19

def initialize(filename = '~/.bash_history')
  @filename = File.expand_path(filename)
end

Instance Method Details

#commandsObject



22
# File 'lib/persistent-shell-history/history.rb', line 22

def commands; (@cmds.nil? or @cmds.empty?) ? (@cmds = parse) : @cmds; end

#fileObject



23
# File 'lib/persistent-shell-history/history.rb', line 23

def file; @filename; end

#file=(filename) ⇒ Object



24
# File 'lib/persistent-shell-history/history.rb', line 24

def file=(filename); @filename = File.expand_path(filename); end

#parse(filename = @filename) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/persistent-shell-history/history.rb', line 25

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