Class: AliasMetrics::CommandHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/alias_metrics/command_history.rb

Constant Summary collapse

ZSH_HISTORY_FILE =
"#{ENV["HOME"]}/.zsh_history"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands, alias_list) ⇒ CommandHistory

Returns a new instance of CommandHistory.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/alias_metrics/command_history.rb', line 37

def initialize(commands, alias_list)
  self.alias_list = alias_list
  self.commands = []
  self.shorten_count = 0
  self.shortenables = Hash.new
  self.alias_usages = Hash.new
  alias_list.alias_hash.each_pair do |alias_, value|
    self.alias_usages[alias_] = AliasUsage.new(alias_, value)
    self.shortenables[alias_] = Shortenable.new(alias_, value)
  end
  self.fragment = Hash.new{|h, key| h[key] = Fragment.new(key)}

  commands.each do |command|
    update_shortenables(command)
    update_alias_usages(command)
    command_expanded = self.alias_list.expand_command(command)
    self.shorten_count += [0, command_expanded.length - command.length].max
    self.commands << command_expanded
    fragments(command_expanded).each do |fragment_body|
      self.fragment[fragment_body].count += 1
    end
  end
  self.alias_list     = self.alias_list.freeze
  self.commands       = self.commands.freeze
  self.shorten_count = self.shorten_count.freeze
  self.shortenables    = self.shortenables.freeze
  self.alias_usages   = self.alias_usages.freeze
end

Instance Attribute Details

#alias_listObject

Returns the value of attribute alias_list.



34
35
36
# File 'lib/alias_metrics/command_history.rb', line 34

def alias_list
  @alias_list
end

#alias_usagesObject

Returns the value of attribute alias_usages.



33
34
35
# File 'lib/alias_metrics/command_history.rb', line 33

def alias_usages
  @alias_usages
end

#commandsObject

Returns the value of attribute commands.



30
31
32
# File 'lib/alias_metrics/command_history.rb', line 30

def commands
  @commands
end

#fragmentObject

Returns the value of attribute fragment.



35
36
37
# File 'lib/alias_metrics/command_history.rb', line 35

def fragment
  @fragment
end

#shorten_countObject

Returns the value of attribute shorten_count.



31
32
33
# File 'lib/alias_metrics/command_history.rb', line 31

def shorten_count
  @shorten_count
end

#shortenablesObject

Returns the value of attribute shortenables.



32
33
34
# File 'lib/alias_metrics/command_history.rb', line 32

def shortenables
  @shortenables
end

Class Method Details

.load_from_zsh_history(alias_list, history_file = ZSH_HISTORY_FILE) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/alias_metrics/command_history.rb', line 6

def load_from_zsh_history(alias_list, history_file = ZSH_HISTORY_FILE)
  commands = []
  open(history_file) do |fh|
    fh.each do |line|
      line.chomp!
      next if line == ""
      begin
        command = parse_command_by_zsh_history_line(line)
        commands << command if command
      rescue ArgumentError #invalid byte sequence in UTF-8
        #do nothing
      end
    end
  end
  CommandHistory.new(commands, alias_list)
end