zimilar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 48c78e3c0883df0fd2de6ca1af109f5853dffe3e
4
+ data.tar.gz: 41a3afa994bd5e697593767013060ae574694044
5
+ SHA512:
6
+ metadata.gz: 81e6fadf3267fffc61de4158bea761cdc1ee5a83ec8684e76169ebc75ddbb26d864b96ed8eab249bbb7728d44544151a2c27cf061fc2c661fe384c99b96f3029
7
+ data.tar.gz: cf0f53974d8dd001f88c131974ebff6f79b8d11bde7fc4fea10ecc6e266d2ba2b07dc639f15ed4fa3a00d2337384fe21409f923acbeb49ffd29b919d7f956c55
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,48 @@
1
+ # Zimilar
2
+
3
+ This small gem extends your zsh to autocorrect inputs that would led in a 'command not found'. It basically looks into your history file and tries to match the most suitable command found.
4
+
5
+ There is no reason why this should only work with zsh, but I haven't tested it with other shells yet and there is currently no logic in ```zimilar``` that tries to find out your current running shell.
6
+
7
+ ```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.
8
+
9
+ 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.
10
+
11
+ ## Installation
12
+
13
+ Install it yourself as:
14
+
15
+ $ gem install zimilar
16
+
17
+ Install from source:
18
+
19
+ $ git clone https://github.com/lotherk/zimilar.git
20
+ $ cd zimilar
21
+ $ rake build
22
+ $ gem install pkg/zimilar-VERSION.gem
23
+
24
+ Or simply copy the bin/zimilar to anywhere you want. Please ensure to install its dependencies.
25
+
26
+ ## Usage
27
+
28
+ Add the following into your .zshrc or any other file that is being ```source```ed during your login:
29
+
30
+ ```zsh
31
+ function command_not_found_handler() {
32
+ zimilar $@
33
+ }
34
+ ```
35
+
36
+ ## Example:
37
+ ```
38
+ kl@kbook:~/ $ vm
39
+ Auto-guessed 'vim', 60.0%
40
+ kl@kbook:~/ $
41
+ ```
42
+ ## Contributing
43
+
44
+ 1. Fork it ( http://github.com/<my-github-username>/zimilar/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/zimilar ADDED
@@ -0,0 +1,97 @@
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
+ history = File.read(histfile).split("\n")
33
+
34
+ candidates = {}
35
+
36
+ history.each do |elem|
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
+ end
61
+
62
+ candidates = Hash[candidates.sort_by { |k, v| k.to_f }.reverse.uniq]
63
+
64
+ while candidates.count > 0 do
65
+ candidate = candidates.shift
66
+ sim = candidate[0]
67
+ cands = candidate[1]
68
+
69
+ if cands.count == 1
70
+ begin
71
+ puts "Auto-guessed '#{cands[0]}', #{sim.round(2)}%"
72
+ slp = case sim
73
+ when 0..24 then 5
74
+ when 25..49 then 3
75
+ when 50..74 then 2
76
+ when 75..89 then 1
77
+ else 0
78
+ end
79
+ sleep slp
80
+ error = nil
81
+ begin
82
+ exec cands[0], *ARGV
83
+ rescue Exception => e
84
+ error = e
85
+ end
86
+
87
+ exit 0 unless error
88
+ rescue
89
+ end
90
+ elsif cands.count > 1
91
+ # abort here, do not iterate them all.
92
+ puts "Multiple candidates found:"
93
+ puts " " + cands.join(", ")
94
+ exit 1
95
+ end
96
+ end
97
+ exit 1
@@ -0,0 +1,3 @@
1
+ module Zimilar
2
+ VERSION = "0.0.1"
3
+ end
data/lib/zimilar.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "zimilar/version"
2
+
3
+ module Zimilar
4
+ # Your code goes here...
5
+ end
data/zimilar.gemspec ADDED
@@ -0,0 +1,23 @@
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{Zimilar}
12
+ spec.description = %q{Zimilar}
13
+ spec.homepage = ""
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
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zimilar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Konrad Lother
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Zimilar
42
+ email:
43
+ - konrad@corpex.de
44
+ executables:
45
+ - zimilar
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/zimilar
55
+ - lib/zimilar.rb
56
+ - lib/zimilar/version.rb
57
+ - zimilar.gemspec
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.1.10
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Zimilar
82
+ test_files: []