agen 0.1.6 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f90fceac9fed45a59113e45eb28e8eef96f7552b487d4ccc90f037765bdd96d3
4
- data.tar.gz: 60e5528277c8cd195d43e67bf4653d70ddddb9cd93d1109c623e4d5ded9c546f
3
+ metadata.gz: 3fff6346fa930fe70a1f586b9bb6d0441f3cce432e799f11722eb61da51fc6a4
4
+ data.tar.gz: ccb5265d8550cfb15d152d8a1044aefee4596199571e218c6b9fa12117e3d8de
5
5
  SHA512:
6
- metadata.gz: 138b4c4c646a0b62b1665c057fa5dd659c13643fe751c2909c281c9f5446ca23ab2c67a8c51dc967ba38fb43ef1b8f4017e7d019337cbe378f27bd2f89b7119b
7
- data.tar.gz: 43ad014f35c326d08f2c242680eaccd36e55a4bb14c316ab5cf8c6136fa82ed4956c15d3ab88d18670a2403e46f7f57aee103bb9c60ad111e83645e6d253a281
6
+ metadata.gz: 607ad6a1cc1c7dd89bead3464dd0b0fa047c9eace831aaa26c0d4bc779b14973a26e45690636486885de4cdbdfc707a546ec7019a8ef927a3fa5860d8cf65fe7
7
+ data.tar.gz: 9c4bf033685b91a936892f3a761943b0584903e8306d545d7fd0ce3ad72d060255e2bd76c4de675456fbb30e0274431a7e0905d7fc7177cd7a8e70dd89321ba1
@@ -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,19 @@
1
1
  ## [Released]
2
2
 
3
+ ## [0.3.1] - 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
+
3
17
  ## [0.1.6] - 2021-04-25
4
18
  - Adds ability to specify number of aliases to generate.
5
19
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- agen (0.1.6)
4
+ agen (0.3.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -4,13 +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. Use
10
- `agen -h` to see available options.
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
+ -v, --version Version
19
+ -n, --number=NUMBER Number of aliases to generate
20
+ -a, --auto Aliases will be generated and applied automatically
21
+ -r, --rcfile=RCFILE Path to shell rc file
22
+ -s, --shell-history=HISTFILE Path to shell history file
23
+ ```
11
24
 
12
- Right now, this will only work with `zsh`, but support for other shells is on
13
- the very lengthy todo list. By default, agen reads from `.zsh_history` and
25
+ 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
14
26
  writes to `.zshrc`.
15
27
 
16
28
  ## Development
@@ -25,18 +37,10 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/Jonath
25
37
 
26
38
  ## Roadmap
27
39
 
28
- * CLI will let you see proposed aliases and accept/decline them interactively.
29
- OR, in "auto mode", commands will be added automatically (as in 1). Auto
30
- should be default?
31
- * CLI should have helpful output for `-h/--help`.
32
- * CLI will support any (or most common) shells, and will find history and rc
33
- file dynamically.
34
- * CLI will let you specific which history file to read form, and which file to output aliases to.
35
- * CLI will let you interactively modify proposed aliases.
36
40
  * CLI will let you "ignore" commands you don't want to alias, forever.
41
+ * CLI will raise user friendly errors if you specify shell configuration files that don't exist.
37
42
  * CLI will let you specify "meta" vs "full" commands.
38
- - Full command would be `git checkout branch-name`, meta command would be
39
- `git checkout`.
43
+ - Full command would be `git checkout branch-name`, meta command would be `git checkout`.
40
44
 
41
45
  ## License
42
46
 
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
@@ -14,11 +14,31 @@ module Agen
14
14
  OptionParser.new do |opts|
15
15
  opts.banner = "Usage: agen [options]"
16
16
 
17
+ opts.on("-v", "--version", String, "Version") do |v|
18
+ puts Agen::VERSION
19
+ return false
20
+ end
21
+
17
22
  opts.on("-nNUMBER", "--number=NUMBER", Integer, "Number of aliases to generate") do |n|
18
23
  options[:number] = n
19
24
  end
25
+
26
+ opts.on("-a", "--auto", "Aliases will be generated and applied automatically") do |a|
27
+ options[:auto] = a
28
+ end
29
+
30
+ opts.on("-rRCFILE", "--rcfile=RCFILE", String, "Path to shell rc file") do |r|
31
+ options[:rcfile] = r
32
+ end
33
+
34
+ opts.on("-sHISTFILE", "--shell-history=HISTFILE", String, "Path to shell history file") do |s|
35
+ options[:histfile] = s
36
+ end
20
37
  end.parse!
21
38
 
39
+ options = Shell.new(options).add_options
40
+ return unless options
41
+
22
42
  Runner.new(**options).run
23
43
  end
24
44
  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,29 +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
7
  DEFAULT_NUMBER = 5
8
8
 
9
9
  attr_reader :histfile, :rcfile
10
10
 
11
- def initialize(histfile: DEFAULT_HISTFILE, rcfile: DEFAULT_RCFILE, number: DEFAULT_NUMBER)
11
+ def initialize(
12
+ histfile: DEFAULT_HISTFILE,
13
+ rcfile: DEFAULT_RCFILE,
14
+ number: DEFAULT_NUMBER,
15
+ auto: false
16
+ )
12
17
  @histfile = histfile
13
18
  @rcfile = rcfile
14
19
  @number = number
20
+ @auto = auto
15
21
  end
16
22
 
17
23
  def run
18
24
  commands = Finder.new(histfile).commands(limit: @number)
19
- aliases = Builder.new(commands, rcfile).aliases
25
+ @builder = Builder.new(commands, rcfile)
26
+ aliases = builder.aliases
20
27
 
28
+ # TODO: This could be its own class too
21
29
  File.open(rcfile, "a") do |file|
22
- puts "Writing new aliases to #{rcfile}:"
30
+ puts "Writing new aliases to #{rcfile}:\n\n"
23
31
  aliases.each do |al|
24
- puts al
25
- file.puts(al)
32
+ if auto
33
+ write_auto(file, al[:full_alias])
34
+ else
35
+ write_interactive(file, al)
36
+ end
26
37
  end
27
38
  end
28
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
29
78
  end
30
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.6"
4
+ VERSION = "0.3.1"
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.6
4
+ version: 0.3.1
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-26 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