zimilar 0.0.3 → 0.0.4

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
  SHA1:
3
- metadata.gz: 49c0ca0d74d35ec5a238c157ef939af258f20172
4
- data.tar.gz: 708def047463c1436c0da4494475f1475cbd4f84
3
+ metadata.gz: 849ad80d8f0d35154c4cffc6cba8eedb72fa7a16
4
+ data.tar.gz: 12dc89a4b854a2ba5d64df8adc7ef5483f5884e1
5
5
  SHA512:
6
- metadata.gz: bc1ad1c198101922349824eea7a6773d5ad196dba08da98866ffdd3f00226ce688ef281cd071c272abc601ef18a24951deaa2a2d93ec9a383b83595fb43583fb
7
- data.tar.gz: 1858c9c65e478aedde8c4c2461e106835200ae115b81847d5a0c8e6cae108f8fc50c119448398ae7aff62404786c4bdf5a628dfdd536dbb3b26ef063312a8aee
6
+ metadata.gz: db09d04c3c8efb415fcf24ae98cee089e1a9496836ad89b664f727cb0e8e73d61d4827a9f50359974a4295ae683c63255400cb9e763debd0852be36d6d83cbff
7
+ data.tar.gz: 1f20011960973f0a2f32972c4816d6122682baa6cad671757fac870ba3a89ecb53b046aa59662ff624766a9eb6bcb515fa1fa5cb716bb68cfed7fc6a18c843e4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zimilar.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Konrad Lother
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Zimilar
2
+
3
+ This small gem extends your shell (see ```Supported Shells```) to autocorrect inputs that would led in a 'command not found'. It basically parses your history file and tries to match to most suitable command found.
4
+
5
+ ```zimilar``` is just a small late-night hack I did and might be improved in the future. Its code is currently also not that optimized but for now, it works.
6
+
7
+ I also think that it might be better to write this as a native shell function in order to not require ruby to be installed. As times pass by, this might change.
8
+
9
+
10
+ ## Supported Shells
11
+
12
+ The following shells are supported by now:
13
+
14
+ - [zsh](http://www.zsh.org/)
15
+ - [bash](https://www.gnu.org/software/bash/bash.html)
16
+
17
+ ## Installation
18
+
19
+ Install it yourself as:
20
+
21
+ $ gem install zimilar
22
+
23
+ Install from source:
24
+
25
+ $ git clone https://github.com/lotherk/zimilar.git
26
+ $ cd zimilar
27
+ $ rake build
28
+ $ gem install pkg/zimilar-VERSION.gem
29
+
30
+ Or simply copy the ```bin/zimilar``` to anywhere you want. Please ensure to install its dependencies.
31
+
32
+
33
+ ## How it Works
34
+
35
+ ```zimilar``` parses your history file and compares the given command with all commands found in the history. It's using the [similar_text](https://github.com/seph/ruby-similar-text) gem to evaluate the matches.
36
+
37
+ If ```zimilar``` finds a suitable match, it replaces your command and runs it.
38
+
39
+ ## Usage
40
+
41
+ Please be aware of that this is a nightly hack and might, under worst circumstances, destroy all you know and love. Please do not use it without having this read.
42
+
43
+ ```zimilar``` doesn't correct to commands with a length less or equal than 2. This is because of most 'dangerous' commands are 2 charackters long, e.g. ```cp```, ```mv```, ```rm``` and many others.
44
+
45
+ But still, use it at your own risk.
46
+
47
+ ### zsh
48
+ Add the following into your .zshrc or any other file that is being ```source```ed during your login:
49
+
50
+ ```bash
51
+ function command_not_found_handler() {
52
+ zimilar $@
53
+ }
54
+ ```
55
+
56
+ ### bash
57
+ Add the following into your .bashrc or any other file that is being ```source```ed during your login:
58
+
59
+ ```bash
60
+ function command_not_found_handle {
61
+ zimilar $@
62
+ }
63
+ ```
64
+
65
+
66
+ ## Example
67
+ ```
68
+ kl@kbook:~/ $ vm
69
+ Auto-guessed 'vim', 60.0%
70
+ kl@kbook:~/ $
71
+
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it ( http://github.com/lotherk/zimilar/fork )
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/zimilar ADDED
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env ruby
2
+ require 'similar_text'
3
+
4
+ config = {
5
+ :zsh => '.zsh_history',
6
+ :bash => '.bash_history'
7
+ }
8
+
9
+ shell = ENV['SHELL'].split("/")[-1].to_sym # auto detect shell
10
+
11
+ unless shell
12
+ $stderr.puts "could not guess shell, aborting."
13
+ exit 1
14
+ end
15
+
16
+ unless config[shell]
17
+ $stderr.puts "unsupported shell '#{shell}'"
18
+ exit 1
19
+ end
20
+
21
+ histfile = ENV['HISTFILE']
22
+ histfile ||= ENV['HOME'] + "/" + config[shell]
23
+
24
+ unless histfile
25
+ $stderr.puts "could not read history file '#{histfile}'"
26
+ exit 1
27
+ end
28
+
29
+ command = ARGV.shift
30
+
31
+ # read history - maybe there's a better way?
32
+ candidates = {}
33
+ line_counter = 0
34
+ File.open(histfile, "r").each_line do |elem|
35
+ begin
36
+ line_counter += 1
37
+ next if elem.empty?
38
+ line = elem
39
+ if elem =~ /^:/ # zsh
40
+ time, line = elem.split(";")
41
+ next unless line
42
+ next if line.strip == command.strip
43
+ end
44
+ cmd, arg = line.split(" ", 2);
45
+ next if cmd.strip == command.strip
46
+ next if cmd.length <= 2 # most dangerous commands on unix have a length of 2. like mv, rm, cp, ...
47
+
48
+ min = command.length - 1
49
+ max = command.length + 1
50
+ next unless cmd.length == command.length or cmd.length == min or cmd.length == max
51
+
52
+ sim = cmd.similar(command)
53
+ sim -= 20 if cmd.length != command.length # hm..
54
+ next unless sim >= 60
55
+
56
+ candidates[sim] ||= []
57
+ next if candidates[sim].include? cmd
58
+
59
+ candidates[sim] << cmd
60
+ rescue ArgumentError => ae
61
+ match = ae.message.match(%r{^invalid byte sequence in (.*)$})
62
+ if match
63
+ $stderr.puts "[zimilar] Couldn't parse history at line #{line_counter} due to encoding problems."
64
+ $stderr.puts elem
65
+ puts ""
66
+ else
67
+ raise
68
+ end
69
+ rescue
70
+ raise
71
+ end
72
+ end
73
+
74
+
75
+
76
+
77
+ candidates = Hash[candidates.sort_by { |k, v| k.to_f }.reverse.uniq]
78
+
79
+ while candidates.count > 0 do
80
+ candidate = candidates.shift
81
+ sim = candidate[0]
82
+ cands = candidate[1]
83
+
84
+ if cands.count == 1
85
+ begin
86
+ puts "Auto-guessed '#{cands[0]}', #{sim.round(2)}%"
87
+ slp = case sim
88
+ when 0..24 then 5
89
+ when 25..49 then 3
90
+ when 50..74 then 2
91
+ when 75..89 then 1
92
+ else 0
93
+ end
94
+ sleep slp
95
+ error = nil
96
+ begin
97
+ exec cands[0], *ARGV
98
+ rescue Exception => e
99
+ error = e
100
+ end
101
+
102
+ exit 0 unless error
103
+ rescue
104
+ end
105
+ elsif cands.count > 1
106
+ # abort here, do not iterate them all.
107
+ puts "Multiple candidates found:"
108
+ puts " " + cands.join(", ")
109
+ exit 1
110
+ end
111
+ end
112
+ exit 1
data/lib/zimilar.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "zimilar/version"
2
+
3
+ module Zimilar
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module Zimilar
2
+ VERSION = "0.0.4"
3
+ end
data/zimilar.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zimilar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zimilar"
8
+ spec.version = Zimilar::VERSION
9
+ spec.authors = ["Konrad Lother"]
10
+ spec.email = ["konrad@corpex.de"]
11
+ spec.summary = %q{This small gem extends your shell to autocorrect inputs that would led in a 'command not found'.}
12
+ spec.description = %q{This small gem extends your shell to autocorrect inputs that would led in a 'command not found'.}
13
+ spec.homepage = "https://github.com/lotherk/zimilar"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "similar_text"
24
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zimilar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konrad Lother
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-19 00:00:00.000000000 Z
11
+ date: 2016-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,10 +56,20 @@ description: This small gem extends your shell to autocorrect inputs that would
56
56
  in a 'command not found'.
57
57
  email:
58
58
  - konrad@corpex.de
59
- executables: []
59
+ executables:
60
+ - zimilar
60
61
  extensions: []
61
62
  extra_rdoc_files: []
62
- files: []
63
+ files:
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/zimilar
70
+ - lib/zimilar.rb
71
+ - lib/zimilar/version.rb
72
+ - zimilar.gemspec
63
73
  homepage: https://github.com/lotherk/zimilar
64
74
  licenses:
65
75
  - MIT
@@ -80,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
90
  version: '0'
81
91
  requirements: []
82
92
  rubyforge_project:
83
- rubygems_version: 2.2.2
93
+ rubygems_version: 2.4.6
84
94
  signing_key:
85
95
  specification_version: 4
86
96
  summary: This small gem extends your shell to autocorrect inputs that would led in