agen 0.1.4 → 0.2.1

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: ac1f9e3386a6c2aa6a792d31040e869cd0cc2243335b0406d8636003cdd91737
4
- data.tar.gz: c8465c062ca6dba68556e80a22ba7600688f7a60e237c1e2f01352c6fffd7a1e
3
+ metadata.gz: 567b4d1df4a054da853eb05a9476bcf9579758cac435e562842c74655e773198
4
+ data.tar.gz: 86e7947aac95a1fc80822158509b1d2e762a11727eca8abda0001b7de87d2115
5
5
  SHA512:
6
- metadata.gz: 2ce071d533b7af081c55e7cd1f91c8326a9145c4731c2e891e84c1b93544207df5159824646a78623707afd1e0bfb5d6062bdeab65bf94cd4afd1a8da0d58d7b
7
- data.tar.gz: 66138b4626cb5c8b257e5325904a9efd8d9ee8e3623bc59ec78e2b58e46e654c4b3ff90e1e702bb5b03c57c5218f9bb844330b4bf659a0184e0f1d54157b0f0a
6
+ metadata.gz: cbaead32095bc304c5bf657426fa66f5b7196c90715a8b8466b597fc578d2c49414f25533bc815946b6bdf20c51af877299bdc3bcba886b4d575864fca04f324
7
+ data.tar.gz: 1c2b2388b097b4f7a95036be7395569d81b3de1e33630a2ffa1b5c262898dbda59742a62a6d88e9d0eb615dcba2375cbf7430472aeb99a77efe03fefa61e60c5
data/CHANGELOG.md CHANGED
@@ -1,10 +1,22 @@
1
1
  ## [Released]
2
2
 
3
- ## [0.1.4] - 2021-04-24
4
- - Avoids most alias naming conflicts with commands that already exist (e.g. `ls`)
3
+ ## [0.2.1] - 2021-05-01
4
+ - Adds ability to modify aliases before writing them in interactive mode.
5
5
 
6
- ## [0.1.3] - 2021-04-24
6
+ ## [0.2.0] - 2021-04-30
7
+ - Adds interactive mode, allowing aliases to be accepted or rejected. On by
8
+ default.
9
+
10
+ ## [0.1.7] - 2021-04-26
11
+ - Fix: Ensures the same aliases is not built for multiple commands in the same run.
12
+
13
+ ## [0.1.6] - 2021-04-25
14
+ - Adds ability to specify number of aliases to generate.
15
+
16
+ ## [0.1.5] - 2021-04-24
7
17
 
8
18
  - Initial release that anyone should reasonably use.
9
19
  - Support for .zshrc/.zsh_history.
10
20
  - Automatically writes top 5 aliases for commands 6 characters or longer.
21
+ - Avoids most alias naming conflicts with commands that already exist (e.g. `ls`).
22
+ - Avoids duplicating aliases that have already been written.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- agen (0.1.4)
4
+ agen (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -6,10 +6,19 @@ Generate shell aliases based on your most commonly entered commands.
6
6
 
7
7
  ## Installation & Usage
8
8
 
9
- Install with `gem install agen` and then run `agen` to build your aliases.
9
+ Install with `gem install agen` and then run `agen` to build your aliases. Then
10
+ be sure to `source ~/.zshrc` before using the new aliases. Use `agen -h` to see
11
+ available options.
12
+
13
+ ```
14
+ Usage: agen [options]
15
+ -n, --number=NUMBER Number of aliases to generate
16
+ -a, --auto Aliases will be generated and applied automatically
17
+ ```
10
18
 
11
19
  Right now, this will only work with `zsh`, but support for other shells is on
12
- the very lengthy todo list.
20
+ the very lengthy todo list. By default, agen reads from `.zsh_history` and
21
+ writes to `.zshrc`.
13
22
 
14
23
  ## Development
15
24
 
@@ -23,24 +32,12 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/Jonath
23
32
 
24
33
  ## Roadmap
25
34
 
26
- ~~1. CLI will read 5 most common full commands from .zsh_history file, and create
27
- an alias for them in the .zshrc file.~~
28
- - [x] It should handle a very short or shell history without many different
29
- commands.
30
- - [x] It should not create the same alias for two different commands in the same
31
- session.
32
- - [x] It should not create aliases for very short commands.
33
- ~~2. CLI should not duplicate aliases or commands that already exist on the system.~~
34
- 3. CLI will let you see proposed aliases and accept/decline them interactively. Maybe auto should be the default?
35
- OR, in "auto mode", commands will be added automatically (as in 1).
36
- 4. CLI should have helpful output for `-h/--help`.
37
- 5. CLI will let you specify number of aliases you want to create.
38
- 6. CLI will support any (or most common) shells, and will find history and rc
35
+ * CLI will support any (or most common) shells, and will find history and rc
39
36
  file dynamically.
40
- 7. CLI will let you specific which history file to read form, and which file to output aliases to.
41
- 8. CLI will let you interactively modify proposed aliases.
42
- 9. CLI will let you "ignore" commands you don't want to alias, forever.
43
- 10. CLI will let you specify "meta" vs "full" commands.
37
+ * CLI will let you specific which history file to read from, and which file to output aliases to.
38
+ * CLI will let you interactively modify proposed aliases.
39
+ * CLI will let you "ignore" commands you don't want to alias, forever.
40
+ * CLI will let you specify "meta" vs "full" commands.
44
41
  - Full command would be `git checkout branch-name`, meta command would be
45
42
  `git checkout`.
46
43
 
data/lib/agen/builder.rb CHANGED
@@ -2,23 +2,39 @@
2
2
 
3
3
  module Agen
4
4
  class Builder
5
- def initialize(commands)
5
+ def initialize(commands = [], rcfile = Runner::DEFAULT_RCFILE)
6
6
  @commands = commands
7
+ @rcfile = rcfile
7
8
  end
8
9
 
9
10
  def aliases
11
+ aliases = []
10
12
  @commands.map do |cmd|
11
13
  aliaz = cmd.scan(/\b\w/).join
12
14
 
13
15
  # Is is possibly we could overwrite a command here still? Sure.
14
16
  # I will live with it for now.
15
- if command_already_exists?(aliaz)
17
+ if command_already_exists?(aliaz) || aliases.include?(aliaz)
16
18
  # We could improve to look more like the original command, but again, works for now.
17
19
  aliaz += aliaz[-1]
18
20
  end
19
21
 
20
- "alias #{aliaz}=\"#{cmd}\""
21
- end
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
27
+ end.compact
28
+ end
29
+
30
+ def construct_alias(aliaz, cmd)
31
+ "alias #{aliaz}=\"#{cmd}\""
32
+ end
33
+
34
+ private
35
+
36
+ def alias_does_not_exist?(aliaz)
37
+ File.readlines(@rcfile).none? { |line| line.include?(aliaz) }
22
38
  end
23
39
 
24
40
  # Shoutout to Stack Overflow: https://stackoverflow.com/a/5471032
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,21 @@ 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
+ end.parse!
25
+
26
+ Runner.new(**options).run
12
27
  end
13
28
  end
14
29
  end
data/lib/agen/runner.rb CHANGED
@@ -4,25 +4,76 @@ module Agen
4
4
  class Runner
5
5
  DEFAULT_HISTFILE = "#{Dir.home}/.zsh_history"
6
6
  DEFAULT_RCFILE = "#{Dir.home}/.zshrc"
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).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 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/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Agen
4
- VERSION = "0.1.4"
4
+ VERSION = "0.2.1"
5
5
  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.4
4
+ version: 0.2.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-25 00:00:00.000000000 Z
11
+ date: 2021-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake