Top Level Namespace

Defined Under Namespace

Classes: BashHistoryParser, HistoryParserFactory, HistoryParserInterface, UnknownShell, ZshHistoryParser

Constant Summary collapse

ZSH_SHELL_PATH =
'/bin/zsh'
BASH_SHELL_PATH =
'/bin/bash'
ZSH_HISTORY_FILE_PATH =
'~/.zsh_history'
BASH_HISTORY_FILE_PATH =
'~/.bash_history'
NUM_COMMANDS_TO_PRINT =
10

Instance Method Summary collapse

Instance Method Details

#analyze_map(command_map, commands_to_print: NUM_COMMANDS_TO_PRINT) ⇒ Object

analyze our map to find the most frequent shell commands



14
15
16
17
18
19
# File 'lib/analyze.rb', line 14

def analyze_map(command_map, commands_to_print: NUM_COMMANDS_TO_PRINT)
  sorted_map = command_map.sort_by { |_, count| count }
  # print the top N commands
  reversed_sorted_map = sorted_map.reverse[0...commands_to_print]
  reversed_sorted_map.each { |command, count| puts "#{command}: #{count}" }
end

#create_mappingObject

create a mapping of command => count from the list of all commands in our history file.



7
8
9
10
11
# File 'lib/analyze.rb', line 7

def create_mapping
  parser = HistoryParserFactory.create
  commands = parser.commands
  Hash.new(0).tap { |h| commands.each { |command| h[command] += 1 } }
end

#parse_argumentsObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lazy.rb', line 8

def parse_arguments
  options = {}
  OptionParser.new do |opts|
    opts.banner = 'Usage: main.rb [options]'

    opts.on('-n', '--num num', Integer, 'Number of commands to print') do |num|
      options[:num] = num
    end
  end.parse!(ARGV)
  options
end

#runObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/lazy.rb', line 20

def run
  options = parse_arguments
  command_map = create_mapping

  if options[:num]
    analyze_map(command_map, commands_to_print: options[:num])
  else
    analyze_map(command_map)
  end
end