lz 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ef9c2aa962aecd38b4951d2b49d6bcbc94bffe3f57e7e1b04d120cdf252fbcee
4
+ data.tar.gz: ef23b828bd67148c94309172fda35838e63708ee7d96068844b143c45f917dee
5
+ SHA512:
6
+ metadata.gz: a9ff04ff68ef8b3cae0cb3a79649bf4db6a29b9a07a5d5cc04354c2eb9473b4a9915ff878446cf4001d60dd330bdc2597eb7c2d89dc6696ce646bdc3c137c83e
7
+ data.tar.gz: b315031cc9901bdc3d25ed3ba9d0825a496a9ca9dd35af8b31a010b17ffe8f15dc3ccb6be631a6b304e302e16124525ac9acb185c96dd44b46074567dac32f5c
data/bin/lazy_cli ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') unless $LOAD_PATH.include?(File.dirname(__FILE__) + '/../lib')
5
+
6
+ require 'lazy'
7
+
8
+ run
data/lib/analyze.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './parser/history_parser_factory'
4
+
5
+ # create a mapping of command => count from the list of
6
+ # all commands in our history file.
7
+ def create_mapping
8
+ parser = HistoryParserFactory.create
9
+ commands = parser.commands
10
+ Hash.new(0).tap { |h| commands.each { |command| h[command] += 1 } }
11
+ end
12
+
13
+ # analyze our map to find the most frequent shell commands
14
+ def analyze_map(command_map, commands_to_print: NUM_COMMANDS_TO_PRINT)
15
+ sorted_map = command_map.sort_by { |_, count| count }
16
+ # print the top N commands
17
+ reversed_sorted_map = sorted_map.reverse[0...commands_to_print]
18
+ reversed_sorted_map.each { |command, count| puts "#{command}: #{count}" }
19
+ end
data/lib/constants.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ ZSH_SHELL_PATH = '/bin/zsh'
4
+ BASH_SHELL_PATH = '/bin/bash'
5
+ ZSH_HISTORY_FILE_PATH = '~/.zsh_history'
6
+ BASH_HISTORY_FILE_PATH = '~/.bash_history'
7
+
8
+ NUM_COMMANDS_TO_PRINT = 10
data/lib/exceptions.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Error for when trying to determine the calling systems shell but no
4
+ # known value is found
5
+ class UnknownShell < StandardError
6
+ def initialize(msg = 'An unknown shell has been encountered', exception_type = 'custom')
7
+ @exception_type = exception_type
8
+ super(msg)
9
+ end
10
+ end
data/lib/lazy.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+
5
+ require_relative './constants'
6
+ require_relative './analyze'
7
+
8
+ def parse_arguments
9
+ options = {}
10
+ OptionParser.new do |opts|
11
+ opts.banner = 'Usage: main.rb [options]'
12
+
13
+ opts.on('-n', '--num num', Integer, 'Number of commands to print') do |num|
14
+ options[:num] = num
15
+ end
16
+ end.parse!(ARGV)
17
+ options
18
+ end
19
+
20
+ def run
21
+ options = parse_arguments
22
+ command_map = create_mapping
23
+
24
+ if options[:num]
25
+ analyze_map(command_map, commands_to_print: options[:num])
26
+ else
27
+ analyze_map(command_map)
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './history_parser_interface'
4
+
5
+ # Parse the Bash history file
6
+ class BashHistoryParser < HistoryParserInterface
7
+ def history_file
8
+ File.expand_path(BASH_HISTORY_FILE_PATH)
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './bash_history_parser'
4
+ require_relative './zsh_history_parser'
5
+ require_relative '../exceptions'
6
+
7
+ # Simple parser to handling line items from the systems history
8
+ # file.
9
+ class HistoryParserFactory
10
+ def self.create(input_shell: ENV['SHELL'])
11
+ case input_shell
12
+ when ZSH_SHELL_PATH
13
+ ZshHistoryParser.new
14
+ when BASH_SHELL_PATH
15
+ BashHistoryParser.new
16
+ else
17
+ raise UnknownShell
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Interface for the history parsers.
4
+ class HistoryParserInterface
5
+ def parse_command
6
+ raise 'Not implemented'
7
+ end
8
+
9
+ def commands
10
+ raise 'Not implemented'
11
+ end
12
+
13
+ def history_file
14
+ raise 'Not implemented'
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './history_parser_interface'
4
+
5
+ # Parse the ZSH history file
6
+ class ZshHistoryParser < HistoryParserInterface
7
+ def parse_command(history_line_entry)
8
+ # get everything on the left hand side of the ';'
9
+ # and join the content. We want to join the content
10
+ # in the case that the command itself has a ';' e.g.
11
+ # : 1650408552:0;git status; git clear
12
+ split_history = history_line_entry.split(';')
13
+ command_part = split_history.drop(1)
14
+ command_part.join(';').strip
15
+ end
16
+
17
+ def commands
18
+ commands = []
19
+ File.open(history_file, 'r') do |f|
20
+ f.each_line do |line|
21
+ commands.push(parse_command(line))
22
+ end
23
+ end
24
+ commands
25
+ end
26
+
27
+ def history_file
28
+ File.expand_path(ZSH_HISTORY_FILE_PATH)
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - AndrewRPorter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - pavelow53.andrew@gmail.com
16
+ executables:
17
+ - lazy_cli
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/lazy_cli
22
+ - lib/analyze.rb
23
+ - lib/constants.rb
24
+ - lib/exceptions.rb
25
+ - lib/lazy.rb
26
+ - lib/parser/bash_history_parser.rb
27
+ - lib/parser/history_parser_factory.rb
28
+ - lib/parser/history_parser_interface.rb
29
+ - lib/parser/zsh_history_parser.rb
30
+ homepage: http://github.com/AndrewRPorter/lazy
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.0.3.1
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Analyze command history and suggest aliases so you can type less.
53
+ test_files: []