Class: 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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/alias_metrics/command_history.rb', line 10

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[value] = Shortenable.new(alias_, value)
  end

  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
  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.



6
7
8
# File 'lib/alias_metrics/command_history.rb', line 6

def alias_list
  @alias_list
end

#alias_usagesObject

Returns the value of attribute alias_usages.



5
6
7
# File 'lib/alias_metrics/command_history.rb', line 5

def alias_usages
  @alias_usages
end

#commandsObject

Returns the value of attribute commands.



2
3
4
# File 'lib/alias_metrics/command_history.rb', line 2

def commands
  @commands
end

#shorten_countObject

Returns the value of attribute shorten_count.



3
4
5
# File 'lib/alias_metrics/command_history.rb', line 3

def shorten_count
  @shorten_count
end

#shortenablesObject

Returns the value of attribute shortenables.



4
5
6
# File 'lib/alias_metrics/command_history.rb', line 4

def shortenables
  @shortenables
end

Class Method Details

.load_from_zsh_history(alias_list, history_file = ZSH_HISTORY_FILE) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/alias_metrics/command_history.rb', line 35

def self.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