agen 0.1.0 → 0.1.5

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: acebf067e22c29311b6a91e2b24e5e8c3f822e24ec8d0a3b5221721e8c147b6a
4
- data.tar.gz: 55129b76da51036069bfbc0890af80ef6b0d2ff57479666cd92d833327e81263
3
+ metadata.gz: dcbe6011253c346bb0abe4d9147146610cbc9fc98f7769f31b960ba20304af17
4
+ data.tar.gz: 5e8035ec1fb0aec32b660395ea24d6d5a8a88aa9e8cc88d0122115d6a2358150
5
5
  SHA512:
6
- metadata.gz: 385c8d66a02051d68bce4de28b3c81c8fb106c509e2f2bfa14825dd4440b21399f3060ddee26a78ba6e5e3638a34429d1c992cb04d12939b9bad935ef00373d0
7
- data.tar.gz: 433e67ddc92ac47d02c9618a1741bde74e3c8bd0f8621f1f4488270ea6d30a69f2664fd40c14eb97f70ae2f578e97c9180a969c66a5bae869d3b1fdf9c59df15
6
+ metadata.gz: 4ca5c06be491092c201c8dcbce7623f0a887d4fbd52e36ab9afc27281ced79fbc97ecb878fc016f4c958893e82bd6718cd08f978c2619fc7a8f59bee7e84358a
7
+ data.tar.gz: 6e62b98c8745005dd1c1632abdc44adef8b55c9e47cfab0318652248b6e5df125109f378b53ca549c8521b6fa07bb70554397b09bcdb63e6bfdb24342f20e30b
@@ -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,9 @@
1
- ## [Unreleased]
1
+ ## [Released]
2
2
 
3
- ## [0.1.0] - 2021-04-24
3
+ ## [0.1.5] - 2021-04-24
4
4
 
5
- - Initial release
5
+ - Initial release that anyone should reasonably use.
6
+ - Support for .zshrc/.zsh_history.
7
+ - Automatically writes top 5 aliases for commands 6 characters or longer.
8
+ - Avoids most alias naming conflicts with commands that already exist (e.g. `ls`).
9
+ - 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.0)
4
+ agen (0.1.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -49,6 +49,7 @@ GEM
49
49
  unicode-display_width (2.0.0)
50
50
 
51
51
  PLATFORMS
52
+ ruby
52
53
  x86_64-darwin-20
53
54
  x86_64-linux
54
55
 
data/README.md CHANGED
@@ -1,50 +1,15 @@
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`.
28
-
29
- ## Installation
30
-
31
- Add this line to your application's Gemfile:
32
-
33
- ```ruby
34
- gem 'agen'
35
- ```
36
-
37
- And then execute:
5
+ Generate shell aliases based on your most commonly entered commands.
38
6
 
39
- $ bundle install
7
+ ## Installation & Usage
40
8
 
41
- Or install it yourself as:
9
+ Install with `gem install agen` and then run `agen` to build your aliases.
42
10
 
43
- $ gem install agen
44
-
45
- ## Usage
46
-
47
- TODO: Write usage instructions here
11
+ Right now, this will only work with `zsh`, but support for other shells is on
12
+ the very lengthy todo list.
48
13
 
49
14
  ## Development
50
15
 
@@ -54,7 +19,23 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
54
19
 
55
20
  ## Contributing
56
21
 
57
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/agen.
22
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JonathanWThom/agen.
23
+
24
+ ## Roadmap
25
+
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
+ * CLI will let you "ignore" commands you don't want to alias, forever.
36
+ * 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`.
58
39
 
59
40
  ## License
60
41
 
data/lib/agen/builder.rb CHANGED
@@ -2,15 +2,44 @@
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
10
11
  @commands.map do |cmd|
11
12
  aliaz = cmd.scan(/\b\w/).join
12
- "alias #{aliaz}=\"#{cmd}\""
13
+
14
+ # Is is possibly we could overwrite a command here still? Sure.
15
+ # I will live with it for now.
16
+ if command_already_exists?(aliaz)
17
+ # We could improve to look more like the original command, but again, works for now.
18
+ aliaz += aliaz[-1]
19
+ end
20
+
21
+ candidate = "alias #{aliaz}=\"#{cmd}\""
22
+ candidate if alias_does_not_exist?(candidate)
23
+ end.compact
24
+ end
25
+
26
+ private
27
+
28
+ def alias_does_not_exist?(aliaz)
29
+ File.readlines(@rcfile).none? { |line| line.include?(aliaz) }
30
+ end
31
+
32
+ # Shoutout to Stack Overflow: https://stackoverflow.com/a/5471032
33
+ def command_already_exists?(cmd)
34
+ exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
35
+ ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
36
+ exts.each do |ext|
37
+ exe = File.join(path, "#{cmd}#{ext}")
38
+ return true if File.executable?(exe) && !File.directory?(exe)
39
+ end
13
40
  end
41
+
42
+ false
14
43
  end
15
44
  end
16
45
  end
data/lib/agen/finder.rb CHANGED
@@ -2,14 +2,31 @@
2
2
 
3
3
  module Agen
4
4
  class Finder
5
+ LIMIT = 5
6
+ MIN_CHARS = 6
7
+
5
8
  def initialize(histfile)
6
9
  @histfile = histfile
7
10
  end
8
11
 
9
- def commands(limit = 5)
10
- File.readlines(@histfile).reverse.each_with_object(Hash.new(0)) do |line, commands|
11
- commands[line.split(";").last.delete("\n")] += 1
12
- end.sort_by { |k, v| -v }.to_h.keys.first(limit)
12
+ def commands(limit: LIMIT, min_chars: MIN_CHARS)
13
+ lines
14
+ .each_with_object(Hash.new(0)) do |line, commands|
15
+ cmd = line.split(";").last.delete("\n")
16
+ commands[cmd] += 1 if cmd != ""
17
+ rescue
18
+ end
19
+ .sort_by { |k, v| -v }
20
+ .to_h
21
+ .keys
22
+ .select { |cmd| cmd.length >= min_chars }
23
+ .first(limit)
24
+ end
25
+
26
+ private
27
+
28
+ def lines
29
+ File.readlines(@histfile).reverse
13
30
  end
14
31
  end
15
32
  end
data/lib/agen/runner.rb CHANGED
@@ -14,10 +14,12 @@ module Agen
14
14
 
15
15
  def run
16
16
  commands = Finder.new(histfile).commands
17
- aliases = Builder.new(commands).aliases
17
+ aliases = Builder.new(commands, rcfile).aliases
18
18
 
19
19
  File.open(rcfile, "a") do |file|
20
+ puts "Writing new aliases to #{rcfile}:"
20
21
  aliases.each do |al|
22
+ puts al
21
23
  file.puts(al)
22
24
  end
23
25
  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.0"
4
+ VERSION = "0.1.5"
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.0
4
+ version: 0.1.5
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-24 00:00:00.000000000 Z
11
+ date: 2021-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake