agen 0.1.3 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df67e0e93c7d387a143caea30f6227dcbe30b3adcb6aab2ef1c820a4acd1dbba
4
- data.tar.gz: dc330d76655aefdf9f0ceaafa5d421426e4883d422c5cfe48347609e3734acdc
3
+ metadata.gz: 774809f0ab3f7e1b8bab9f9209f71f2e616f5da62edf2bf74f8f47f15d82b5ad
4
+ data.tar.gz: 20e8af719cc7093968b107a32d98963febb0b03e1c9dddda3a111e1170cded9a
5
5
  SHA512:
6
- metadata.gz: 1739ef52ab785d37134f7f91bbcc3875997086fe53f4e142afe35770e5fb28075a24dc9727eee7b986ceecc36f6f206cd84690cee5f10065eb44817de77db9c5
7
- data.tar.gz: c05fab9189e7300f4915a293ee8b55d5a6a1e41a91003e786d46ad76bfc7f2c31ca1b2933524f45d0e1d537bdec3bd70604d863372eaec95a290e4995af5cba7
6
+ metadata.gz: 66e2e642b09fc276d27ad922f219be8431c2dc2014726227189c1d2a2cb28960a061cb2da11d45fddc9dce5ff292bb70136b71fd8ebd0ca36f9f015385048686
7
+ data.tar.gz: 1b6beea0fb2a953cd710dc02974ce50aa1198234807cc776e1a5dfd0010dad241f9dbc160c89456b455749381601d9e5d0e03ab198997590068e48cec650cdae
@@ -1,4 +1,4 @@
1
- name: Ruby
1
+ name: Build
2
2
 
3
3
  on: [push,pull_request]
4
4
 
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
- ## [Unreleased]
1
+ ## [Released]
2
2
 
3
- ## [0.1.0] - 2021-04-24
3
+ ## [0.2.0] - 2021-04-30
4
+ - Fix: Ensures the same aliases is not built for multiple commands in the same run.
4
5
 
5
- - Initial release
6
+ ## [0.1.7] - 2021-04-26
7
+ - Fix: Ensures the same aliases is not built for multiple commands in the same run.
8
+
9
+ ## [0.1.6] - 2021-04-25
10
+ - Adds ability to specify number of aliases to generate.
11
+
12
+ ## [0.1.5] - 2021-04-24
13
+
14
+ - Initial release that anyone should reasonably use.
15
+ - Support for .zshrc/.zsh_history.
16
+ - Automatically writes top 5 aliases for commands 6 characters or longer.
17
+ - Avoids most alias naming conflicts with commands that already exist (e.g. `ls`).
18
+ - 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.3)
4
+ agen (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,50 +1,24 @@
1
1
  # Agen
2
2
 
3
- Generate shell aliases based on commonly entered commands.
3
+ [![Build](https://github.com/JonathanWThom/agen/actions/workflows/main.yml/badge.svg)](https://github.com/JonathanWThom/agen/actions/workflows/main.yml)
4
4
 
5
- ## Roadmap
6
-
7
- 1. CLI will read 5 most common full commands from .zsh_history file, and create
8
- an alias for them in the .zshrc file.
9
- - It should not create aliases out of old aliases.
10
- - It should handle a very short or shell history without many different
11
- commands.
12
- - It should output progress for very long/slow shell histories.
13
- - It should not create the same alias for two different commands in the same
14
- session.
15
- - It should not create an alias that is longer than the original command.
16
- 1. CLI will let you see proposed aliases and accept/decline them interactively.
17
- OR, in "auto mode", commands will be added automatically (as in 1).
18
- 1. CLI should have helpful output for `-h/--help`.
19
- 1. CLI will let you specify number of aliases you want to create.
20
- 1. CLI will support any (or most common) shells, and will find history and rc
21
- file dynamically.
22
- 1. CLI will let you specific which history file to read form, and which file to output aliases to.
23
- 1. CLI will let you interactively modify proposed aliases.
24
- 1. CLI will let you "ignore" commands you don't want to alias, forever.
25
- 1. CLI will let you specify "meta" vs "full" commands.
26
- - Full command would be `git checkout branch-name`, meta command would be
27
- `git checkout`.
5
+ Generate shell aliases based on your most commonly entered commands.
28
6
 
29
- ## Installation
7
+ ## Installation & Usage
30
8
 
31
- Add this line to your application's Gemfile:
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.
32
12
 
33
- ```ruby
34
- gem 'agen'
13
+ ```
14
+ Usage: agen [options]
15
+ -n, --number=NUMBER Number of aliases to generate
16
+ -a, --auto Aliases will be generated and applied automatically
35
17
  ```
36
18
 
37
- And then execute:
38
-
39
- $ bundle install
40
-
41
- Or install it yourself as:
42
-
43
- $ gem install agen
44
-
45
- ## Usage
46
-
47
- TODO: Write usage instructions here
19
+ Right now, this will only work with `zsh`, but support for other shells is on
20
+ the very lengthy todo list. By default, agen reads from `.zsh_history` and
21
+ writes to `.zshrc`.
48
22
 
49
23
  ## Development
50
24
 
@@ -54,7 +28,18 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
54
28
 
55
29
  ## Contributing
56
30
 
57
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/agen.
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JonathanWThom/agen.
32
+
33
+ ## Roadmap
34
+
35
+ * CLI will support any (or most common) shells, and will find history and rc
36
+ file dynamically.
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.
41
+ - Full command would be `git checkout branch-name`, meta command would be
42
+ `git checkout`.
58
43
 
59
44
  ## License
60
45
 
data/lib/agen/builder.rb CHANGED
@@ -2,15 +2,48 @@
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
- "alias #{aliaz}=\"#{cmd}\""
14
+
15
+ # Is is possibly we could overwrite a command here still? Sure.
16
+ # I will live with it for now.
17
+ if command_already_exists?(aliaz) || aliases.include?(aliaz)
18
+ # We could improve to look more like the original command, but again, works for now.
19
+ aliaz += aliaz[-1]
20
+ end
21
+
22
+ candidate = "alias #{aliaz}=\"#{cmd}\""
23
+ if alias_does_not_exist?(candidate)
24
+ aliases << aliaz
25
+ candidate
26
+ end
27
+ end.compact
28
+ end
29
+
30
+ private
31
+
32
+ def alias_does_not_exist?(aliaz)
33
+ File.readlines(@rcfile).none? { |line| line.include?(aliaz) }
34
+ end
35
+
36
+ # Shoutout to Stack Overflow: https://stackoverflow.com/a/5471032
37
+ def command_already_exists?(cmd)
38
+ exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
39
+ ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
40
+ exts.each do |ext|
41
+ exe = File.join(path, "#{cmd}#{ext}")
42
+ return true if File.executable?(exe) && !File.directory?(exe)
43
+ end
13
44
  end
45
+
46
+ false
14
47
  end
15
48
  end
16
49
  end
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/finder.rb CHANGED
@@ -19,7 +19,7 @@ module Agen
19
19
  .sort_by { |k, v| -v }
20
20
  .to_h
21
21
  .keys
22
- .select { |line| line.length >= min_chars }
22
+ .select { |cmd| cmd.length >= min_chars }
23
23
  .first(limit)
24
24
  end
25
25
 
data/lib/agen/runner.rb CHANGED
@@ -4,25 +4,59 @@ 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
+ aliases = Builder.new(commands, rcfile).aliases
18
26
 
19
27
  File.open(rcfile, "a") do |file|
20
- puts "Writing aliases to #{rcfile}:"
28
+ puts "Writing new aliases to #{rcfile}:"
21
29
  aliases.each do |al|
22
- puts al
23
- file.puts(al)
30
+ if auto
31
+ write_auto(file, al)
32
+ else
33
+ write_interactive(file, al)
34
+ end
24
35
  end
25
36
  end
26
37
  end
38
+
39
+ private
40
+
41
+ attr_reader :auto
42
+
43
+ def write_auto(file, aliaz)
44
+ puts aliaz
45
+ file.puts(aliaz)
46
+ end
47
+
48
+ def write_interactive(file, aliaz)
49
+ puts "Proposed alias: #{aliaz}"
50
+ print "Accept? [n to reject, any other key to accept]: "
51
+ response = gets.chomp
52
+ if response != "n"
53
+ file.puts(aliaz)
54
+ puts "Alias written"
55
+ else
56
+ puts "Alias skipped"
57
+ end
58
+
59
+ puts
60
+ end
27
61
  end
28
62
  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.3"
4
+ VERSION = "0.2.0"
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.3
4
+ version: 0.2.0
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