agen 0.1.1 → 0.1.6

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: 49b4f1125783920bc7b25522ff96b7ef31f606b901ac192c024931bd0aa20fc5
4
- data.tar.gz: b4777fa42d27ec416573a867565563427b216422f12522f964645d32f1fd0ef8
3
+ metadata.gz: f90fceac9fed45a59113e45eb28e8eef96f7552b487d4ccc90f037765bdd96d3
4
+ data.tar.gz: 60e5528277c8cd195d43e67bf4653d70ddddb9cd93d1109c623e4d5ded9c546f
5
5
  SHA512:
6
- metadata.gz: cdf675badd54139d2f548cccb525ab228ab68236abac838e69de9d993cc848f8d15e6ed4b9cec30434d1d39e2eaace17b2e86ae8d8104dbfddd6df13924ac861
7
- data.tar.gz: 4247034d929c40541592da4bb6232d464560aef83f2fe97541851e3b701c03eba5737b84579c48e9564471be872471f8f0f671d0de5baf0cb56d52721ada4067
6
+ metadata.gz: 138b4c4c646a0b62b1665c057fa5dd659c13643fe751c2909c281c9f5446ca23ab2c67a8c51dc967ba38fb43ef1b8f4017e7d019337cbe378f27bd2f89b7119b
7
+ data.tar.gz: 43ad014f35c326d08f2c242680eaccd36e55a4bb14c316ab5cf8c6136fa82ed4956c15d3ab88d18670a2403e46f7f57aee103bb9c60ad111e83645e6d253a281
@@ -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,12 @@
1
- ## [Unreleased]
1
+ ## [Released]
2
2
 
3
- ## [0.1.0] - 2021-04-24
3
+ ## [0.1.6] - 2021-04-25
4
+ - Adds ability to specify number of aliases to generate.
4
5
 
5
- - Initial release
6
+ ## [0.1.5] - 2021-04-24
7
+
8
+ - Initial release that anyone should reasonably use.
9
+ - Support for .zshrc/.zsh_history.
10
+ - Automatically writes top 5 aliases for commands 6 characters or longer.
11
+ - Avoids most alias naming conflicts with commands that already exist (e.g. `ls`).
12
+ - 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.1)
4
+ agen (0.1.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,50 +1,17 @@
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. Use
10
+ `agen -h` to see available options.
42
11
 
43
- $ gem install agen
44
-
45
- ## Usage
46
-
47
- TODO: Write usage instructions here
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
14
+ writes to `.zshrc`.
48
15
 
49
16
  ## Development
50
17
 
@@ -54,7 +21,22 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
54
21
 
55
22
  ## Contributing
56
23
 
57
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/agen.
24
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JonathanWThom/agen.
25
+
26
+ ## Roadmap
27
+
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
+ * CLI will let you "ignore" commands you don't want to alias, forever.
37
+ * 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`.
58
40
 
59
41
  ## License
60
42
 
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/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,17 @@ 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
+ end.parse!
21
+
22
+ Runner.new(**options).run
12
23
  end
13
24
  end
14
25
  end
data/lib/agen/finder.rb CHANGED
@@ -2,20 +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
- begin
12
- cmd = line.split(";").last.delete("\n")
13
- if cmd != ""
14
- commands[line.split(";").last.delete("\n")] += 1
15
- end
16
- rescue
17
- end
18
- 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
19
30
  end
20
31
  end
21
32
  end
data/lib/agen/runner.rb CHANGED
@@ -4,20 +4,24 @@ 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(histfile: DEFAULT_HISTFILE, rcfile: DEFAULT_RCFILE, number: DEFAULT_NUMBER)
11
12
  @histfile = histfile
12
13
  @rcfile = rcfile
14
+ @number = number
13
15
  end
14
16
 
15
17
  def run
16
- commands = Finder.new(histfile).commands
17
- aliases = Builder.new(commands).aliases
18
+ commands = Finder.new(histfile).commands(limit: @number)
19
+ aliases = Builder.new(commands, rcfile).aliases
18
20
 
19
21
  File.open(rcfile, "a") do |file|
22
+ puts "Writing new aliases to #{rcfile}:"
20
23
  aliases.each do |al|
24
+ puts al
21
25
  file.puts(al)
22
26
  end
23
27
  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.1"
4
+ VERSION = "0.1.6"
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.1
4
+ version: 0.1.6
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-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
106
- rubygems_version: 3.1.4
106
+ rubygems_version: 3.2.15
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: Generate shell aliases based on commonly entered commands.