agen 0.1.2 → 0.1.7

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: 43ec40b6400566c0adf7aed5269f7993406411cf1037168bb9ce08b5470d780b
4
- data.tar.gz: 2d14004d0980aac0df863d338d57b578fcfa6ad05c9be420284749a25e0fd955
3
+ metadata.gz: 88c5c183242fa63c57940eba901e982778e930921fdaf8eda8ba2c64a23801f0
4
+ data.tar.gz: 262a494cd57ad6d3ee4474eee51a686a94309084222b5fc4b128be7ce35e5f40
5
5
  SHA512:
6
- metadata.gz: a29757826256b1216ff6a64bf55a7319e8387b316d6f348fb8b331ec03e019d32b6ef6b9333a13e4009581746b826717860f620970a68014d64f334080f1f726
7
- data.tar.gz: da2af239a0df07715433ba9699ac5e2f55cf0041a14aee9c5967b967624f9112cf0ddbe456d8d57c1cb615e5eb61b6ec264f4ba2e65d607a8f1ec5161370c709
6
+ metadata.gz: d994611c28f6f086450a4baf40e842cff15108cedaeba542710827ec77499f41086bcc1f334866873c1f3ef167588885ede145de26afb60eb30f6644036ba0ca
7
+ data.tar.gz: d553d15e0420538459819547d86faa117244833080ad27dc35177d776a2e37a2996c047042acdd0aa5d426da02df1060e0413f9cc5c09590b24a9b3a50804f52
@@ -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,15 @@
1
- ## [Unreleased]
1
+ ## [Released]
2
2
 
3
- ## [0.1.0] - 2021-04-24
3
+ ## [0.1.7] - 2021-04-26
4
+ - Fix: Ensures the same aliases is not built for multiple commands in the same run.
4
5
 
5
- - Initial release
6
+ ## [0.1.6] - 2021-04-25
7
+ - Adds ability to specify number of aliases to generate.
8
+
9
+ ## [0.1.5] - 2021-04-24
10
+
11
+ - Initial release that anyone should reasonably use.
12
+ - Support for .zshrc/.zsh_history.
13
+ - Automatically writes top 5 aliases for commands 6 characters or longer.
14
+ - Avoids most alias naming conflicts with commands that already exist (e.g. `ls`).
15
+ - 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.2)
4
+ agen (0.1.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,50 +1,18 @@
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. Then
10
+ be sure to `source ~/.zshrc` before using the new aliases. Use `agen -h` to see
11
+ available options.
42
12
 
43
- $ gem install agen
44
-
45
- ## Usage
46
-
47
- TODO: Write usage instructions here
13
+ Right now, this will only work with `zsh`, but support for other shells is on
14
+ the very lengthy todo list. By default, agen reads from `.zsh_history` and
15
+ writes to `.zshrc`.
48
16
 
49
17
  ## Development
50
18
 
@@ -54,7 +22,22 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
54
22
 
55
23
  ## Contributing
56
24
 
57
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/agen.
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JonathanWThom/agen.
26
+
27
+ ## Roadmap
28
+
29
+ * CLI will let you see proposed aliases and accept/decline them interactively.
30
+ OR, in "auto mode", commands will be added automatically (as in 1). Auto
31
+ should be default?
32
+ * CLI should have helpful output for `-h/--help`.
33
+ * CLI will support any (or most common) shells, and will find history and rc
34
+ file dynamically.
35
+ * CLI will let you specific which history file to read form, and which file to output aliases to.
36
+ * CLI will let you interactively modify proposed aliases.
37
+ * CLI will let you "ignore" commands you don't want to alias, forever.
38
+ * CLI will let you specify "meta" vs "full" commands.
39
+ - Full command would be `git checkout branch-name`, meta command would be
40
+ `git checkout`.
58
41
 
59
42
  ## License
60
43
 
data/exe/agen CHANGED
@@ -1,7 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- $LOAD_PATH.unshift("#{__dir__}/../lib")
4
-
5
3
  require "agen"
6
4
 
7
5
  Agen::CLI.new(ARGV).run
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,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,22 @@ 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|
20
- puts "Writing aliases to #{rcfile}:"
22
+ puts "Writing new aliases to #{rcfile}:"
21
23
  aliases.each do |al|
22
24
  puts al
23
25
  file.puts(al)
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.2"
4
+ VERSION = "0.1.7"
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.2
4
+ version: 0.1.7
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-27 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.