agen 0.1.5 → 0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dcbe6011253c346bb0abe4d9147146610cbc9fc98f7769f31b960ba20304af17
4
- data.tar.gz: 5e8035ec1fb0aec32b660395ea24d6d5a8a88aa9e8cc88d0122115d6a2358150
3
+ metadata.gz: edd821d0314f78d537a2228181e4f1dfd0ecaceddc8d8a67bb783499911e7ccb
4
+ data.tar.gz: 721e0d0871630027ac23c0e08d7dd54e3e20ef26fda674fe755ebb02e0e4be48
5
5
  SHA512:
6
- metadata.gz: 4ca5c06be491092c201c8dcbce7623f0a887d4fbd52e36ab9afc27281ced79fbc97ecb878fc016f4c958893e82bd6718cd08f978c2619fc7a8f59bee7e84358a
7
- data.tar.gz: 6e62b98c8745005dd1c1632abdc44adef8b55c9e47cfab0318652248b6e5df125109f378b53ca549c8521b6fa07bb70554397b09bcdb63e6bfdb24342f20e30b
6
+ metadata.gz: f2c802aa56ab03338dbe58638a5ab5cf525c13ffef6b0a6b11e5aff39b430d4645f3116d6d49e37425df1704029148ca8df3d8d5dc4af84d0b3dff8ff8263d1c
7
+ data.tar.gz: d02a95bbee062a61ebe93160c03f773706e45264b09cd4cae9a2aab2c1a45c97078192b399466854e22e87d80266c6aa29e61167ce3f8b726b4a07180311a450
@@ -12,5 +12,7 @@ jobs:
12
12
  with:
13
13
  ruby-version: 3.0.1
14
14
  bundler-cache: true
15
- - name: Run the default task
15
+ - name: Lint
16
+ run: bundle exec rake standard
17
+ - name: Test
16
18
  run: bundle exec rake
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  ## [Released]
2
2
 
3
+ ## [0.3] - 2021-06-17
4
+ - Adds support for bash shell.
5
+ - Adds ability to pass custom rcfile and histfile options.
6
+
7
+ ## [0.2.1] - 2021-05-01
8
+ - Adds ability to modify aliases before writing them in interactive mode.
9
+
10
+ ## [0.2.0] - 2021-04-30
11
+ - Adds interactive mode, allowing aliases to be accepted or rejected. On by
12
+ default.
13
+
14
+ ## [0.1.7] - 2021-04-26
15
+ - Fix: Ensures the same aliases is not built for multiple commands in the same run.
16
+
17
+ ## [0.1.6] - 2021-04-25
18
+ - Adds ability to specify number of aliases to generate.
19
+
3
20
  ## [0.1.5] - 2021-04-24
4
21
 
5
22
  - Initial release that anyone should reasonably use.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- agen (0.1.5)
4
+ agen (0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -4,12 +4,25 @@
4
4
 
5
5
  Generate shell aliases based on your most commonly entered commands.
6
6
 
7
+ https://user-images.githubusercontent.com/22665228/116796045-5e5a8280-aa8e-11eb-8b4c-4408a252c143.mov
8
+
9
+
7
10
  ## Installation & Usage
8
11
 
9
- Install with `gem install agen` and then run `agen` to build your aliases.
12
+ Install with `gem install agen` and then run `agen` to build your aliases. Then
13
+ be sure to `source ~/.zshrc` before using the new aliases. Use `agen -h` to see
14
+ available options.
15
+
16
+ ```
17
+ Usage: agen [options]
18
+ -n, --number=NUMBER Number of aliases to generate
19
+ -a, --auto Aliases will be generated and applied automatically
20
+ -r, --rcfile=RCFILE Path to shell rc file
21
+ -h, --histfile=HISTFILE Path to shell history file
22
+ ```
10
23
 
11
- Right now, this will only work with `zsh`, but support for other shells is on
12
- the very lengthy todo list.
24
+ Right now, this will only work with `zsh` or `bash`, but you can specify unique shell config files using the `-r` and `-h` options (though there is no guarantee that your history file will be read properly). By default, agen reads from `.zsh_history` and
25
+ writes to `.zshrc`.
13
26
 
14
27
  ## Development
15
28
 
@@ -23,19 +36,10 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/Jonath
23
36
 
24
37
  ## Roadmap
25
38
 
26
- * CLI will let you specify number of aliases you want to create.
27
- * CLI will let you see proposed aliases and accept/decline them interactively.
28
- OR, in "auto mode", commands will be added automatically (as in 1). Auto
29
- should be default?
30
- * CLI should have helpful output for `-h/--help`.
31
- * CLI will support any (or most common) shells, and will find history and rc
32
- file dynamically.
33
- * CLI will let you specific which history file to read form, and which file to output aliases to.
34
- * CLI will let you interactively modify proposed aliases.
35
39
  * CLI will let you "ignore" commands you don't want to alias, forever.
40
+ * CLI will raise user friendly errors if you specify shell configuration files that don't exist.
36
41
  * CLI will let you specify "meta" vs "full" commands.
37
- - Full command would be `git checkout branch-name`, meta command would be
38
- `git checkout`.
42
+ - Full command would be `git checkout branch-name`, meta command would be `git checkout`.
39
43
 
40
44
  ## License
41
45
 
data/lib/agen.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "agen/version"
4
+ require_relative "agen/base_options"
5
+ require_relative "agen/bash_options"
6
+ require_relative "agen/zsh_options"
7
+ require_relative "agen/shell"
4
8
  require_relative "agen/cli"
5
9
  require_relative "agen/builder"
6
10
  require_relative "agen/finder"
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Agen
4
+ class BaseOptions
5
+ def initialize(options)
6
+ @options = options
7
+ end
8
+
9
+ def set_histfile
10
+ # handle not set here
11
+ if !defined?(self.class::HISTFILE)
12
+ puts "Please specify shell history file with -h option. See agen --help."
13
+ return false
14
+ end
15
+
16
+ options.tap do |opts|
17
+ opts[:histfile] = self.class::HISTFILE
18
+ end
19
+ end
20
+
21
+ def set_rcfile
22
+ if !defined?(self.class::RCFILE)
23
+ puts "Please specify shell rc file with -r option. See agen --help."
24
+ return false
25
+ end
26
+
27
+ options.tap do |opts|
28
+ opts[:rcfile] = self.class::RCFILE
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :options
35
+ end
36
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Agen
4
+ class BashOptions < BaseOptions
5
+ HISTFILE = "#{Dir.home}/.bash_history"
6
+ RCFILE = "#{Dir.home}/.bashrc"
7
+ end
8
+ end
data/lib/agen/builder.rb CHANGED
@@ -2,27 +2,35 @@
2
2
 
3
3
  module Agen
4
4
  class Builder
5
- def initialize(commands, rcfile = Runner::DEFAULT_RCFILE)
5
+ def initialize(commands = [], rcfile = Runner::DEFAULT_RCFILE)
6
6
  @commands = commands
7
7
  @rcfile = rcfile
8
8
  end
9
9
 
10
10
  def aliases
11
+ aliases = []
11
12
  @commands.map do |cmd|
12
13
  aliaz = cmd.scan(/\b\w/).join
13
14
 
14
15
  # Is is possibly we could overwrite a command here still? Sure.
15
16
  # I will live with it for now.
16
- if command_already_exists?(aliaz)
17
+ if command_already_exists?(aliaz) || aliases.include?(aliaz)
17
18
  # We could improve to look more like the original command, but again, works for now.
18
19
  aliaz += aliaz[-1]
19
20
  end
20
21
 
21
- candidate = "alias #{aliaz}=\"#{cmd}\""
22
- candidate if alias_does_not_exist?(candidate)
22
+ candidate = construct_alias(aliaz, cmd)
23
+ if alias_does_not_exist?(candidate)
24
+ aliases << aliaz
25
+ {alias: aliaz, full_alias: candidate, command: cmd}
26
+ end
23
27
  end.compact
24
28
  end
25
29
 
30
+ def construct_alias(aliaz, cmd)
31
+ "alias #{aliaz}=\"#{cmd}\""
32
+ end
33
+
26
34
  private
27
35
 
28
36
  def alias_does_not_exist?(aliaz)
data/lib/agen/cli.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "optparse"
4
+
3
5
  module Agen
4
6
  class CLI
5
7
  def initialize(args = [])
@@ -7,8 +9,32 @@ module Agen
7
9
  end
8
10
 
9
11
  def run
10
- # TODO: Parse args/options
11
- Runner.new.run
12
+ options = {}
13
+
14
+ OptionParser.new do |opts|
15
+ opts.banner = "Usage: agen [options]"
16
+
17
+ opts.on("-nNUMBER", "--number=NUMBER", Integer, "Number of aliases to generate") do |n|
18
+ options[:number] = n
19
+ end
20
+
21
+ opts.on("-a", "--auto", "Aliases will be generated and applied automatically") do |a|
22
+ options[:auto] = a
23
+ end
24
+
25
+ opts.on("-rRCFILE", "--rcfile=RCFILE", String, "Path to shell rc file") do |r|
26
+ options[:rcfile] = r
27
+ end
28
+
29
+ opts.on("-hHISTFILE", "--histfile=HISTFILE", String, "Path to shell history file") do |h|
30
+ options[:histfile] = h
31
+ end
32
+ end.parse!
33
+
34
+ options = Shell.new(options).add_options
35
+ return unless options
36
+
37
+ Runner.new(**options).run
12
38
  end
13
39
  end
14
40
  end
data/lib/agen/finder.rb CHANGED
@@ -14,7 +14,8 @@ module Agen
14
14
  .each_with_object(Hash.new(0)) do |line, commands|
15
15
  cmd = line.split(";").last.delete("\n")
16
16
  commands[cmd] += 1 if cmd != ""
17
- rescue
17
+ rescue => e
18
+ puts e
18
19
  end
19
20
  .sort_by { |k, v| -v }
20
21
  .to_h
data/lib/agen/runner.rb CHANGED
@@ -2,27 +2,78 @@
2
2
 
3
3
  module Agen
4
4
  class Runner
5
- DEFAULT_HISTFILE = "#{Dir.home}/.zsh_history"
6
- DEFAULT_RCFILE = "#{Dir.home}/.zshrc"
5
+ DEFAULT_HISTFILE = ZshOptions::HISTFILE
6
+ DEFAULT_RCFILE = ZshOptions::RCFILE
7
+ DEFAULT_NUMBER = 5
7
8
 
8
9
  attr_reader :histfile, :rcfile
9
10
 
10
- def initialize(histfile: DEFAULT_HISTFILE, rcfile: DEFAULT_RCFILE)
11
+ def initialize(
12
+ histfile: DEFAULT_HISTFILE,
13
+ rcfile: DEFAULT_RCFILE,
14
+ number: DEFAULT_NUMBER,
15
+ auto: false
16
+ )
11
17
  @histfile = histfile
12
18
  @rcfile = rcfile
19
+ @number = number
20
+ @auto = auto
13
21
  end
14
22
 
15
23
  def run
16
- commands = Finder.new(histfile).commands
17
- aliases = Builder.new(commands, rcfile).aliases
24
+ commands = Finder.new(histfile).commands(limit: @number)
25
+ @builder = Builder.new(commands, rcfile)
26
+ aliases = builder.aliases
18
27
 
28
+ # TODO: This could be its own class too
19
29
  File.open(rcfile, "a") do |file|
20
- puts "Writing new aliases to #{rcfile}:"
30
+ puts "Writing new aliases to #{rcfile}:\n\n"
21
31
  aliases.each do |al|
22
- puts al
23
- file.puts(al)
32
+ if auto
33
+ write_auto(file, al[:full_alias])
34
+ else
35
+ write_interactive(file, al)
36
+ end
24
37
  end
25
38
  end
26
39
  end
40
+
41
+ private
42
+
43
+ attr_reader :auto, :builder
44
+
45
+ def write_auto(file, full_alias)
46
+ puts full_alias
47
+ file.puts(full_alias)
48
+ end
49
+
50
+ def write_interactive(file, aliaz)
51
+ puts "Proposed alias: #{aliaz[:full_alias]}"
52
+ print "Accept? [n to reject, m to modify alias, any other key to accept]: "
53
+ response = gets.chomp
54
+ case response
55
+ when "n"
56
+ puts "Alias skipped"
57
+ when "m"
58
+ modify_alias(file, aliaz)
59
+ else
60
+ file.puts(aliaz[:full_alias])
61
+ puts "Alias written"
62
+ end
63
+
64
+ puts
65
+ end
66
+
67
+ def modify_alias(file, aliaz)
68
+ print "Enter new alias [replacing #{aliaz[:alias]}]: "
69
+ replacement = gets.chomp
70
+ if replacement == ""
71
+ modify_alias(file, aliaz)
72
+ return
73
+ end
74
+
75
+ file.puts(builder.construct_alias(replacement, aliaz[:command]))
76
+ puts "Alias written"
77
+ end
27
78
  end
28
79
  end
data/lib/agen/shell.rb ADDED
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Agen
4
+ class Shell
5
+ def initialize(options)
6
+ @options = options
7
+ end
8
+
9
+ def add_options
10
+ if !@options[:histfile]
11
+ @options = opts_klass.set_histfile
12
+ end
13
+
14
+ if @options && !@options[:rcfile]
15
+ @options = opts_klass.set_rcfile
16
+ end
17
+
18
+ @options
19
+ end
20
+
21
+ private
22
+
23
+ def bash?
24
+ @_bash ||= shell.match?(/bash/)
25
+ end
26
+
27
+ def opts_klass
28
+ @_opts_klass ||= if bash?
29
+ BashOptions
30
+ elsif zsh?
31
+ ZshOptions
32
+ else
33
+ BaseOptions
34
+ end.new(@options)
35
+ end
36
+
37
+ def shell
38
+ @_shell ||= ENV["SHELL"]
39
+ end
40
+
41
+ def zsh?
42
+ @_zsh ||= shell.match?(/zsh/)
43
+ end
44
+ end
45
+ end
data/lib/agen/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Agen
4
- VERSION = "0.1.5"
4
+ VERSION = "0.3"
5
5
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Agen
4
+ class ZshOptions < BaseOptions
5
+ HISTFILE = "#{Dir.home}/.zsh_history"
6
+ RCFILE = "#{Dir.home}/.zshrc"
7
+ end
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Thom
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-25 00:00:00.000000000 Z
11
+ date: 2021-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -75,11 +75,15 @@ files:
75
75
  - bin/setup
76
76
  - exe/agen
77
77
  - lib/agen.rb
78
+ - lib/agen/base_options.rb
79
+ - lib/agen/bash_options.rb
78
80
  - lib/agen/builder.rb
79
81
  - lib/agen/cli.rb
80
82
  - lib/agen/finder.rb
81
83
  - lib/agen/runner.rb
84
+ - lib/agen/shell.rb
82
85
  - lib/agen/version.rb
86
+ - lib/agen/zsh_options.rb
83
87
  homepage: https://github.com/JonathanWThom/agen
84
88
  licenses:
85
89
  - MIT