From 4f9dfc2805ab2d8940052cacf7dd4b208b2a2958 Mon Sep 17 00:00:00 2001 From: Baptiste Fontaine Date: Wed, 23 Dec 2015 02:07:22 +0100 Subject: [PATCH 1/5] AliasChecker: modernize; fallback if no cache --- lib/huffshell.rb | 1 - lib/shell/alias_checker.rb | 40 +++++++++++++++++-------- lib/shell/alias_line.rb | 20 ------------- spec/command/command_suggestion_spec.rb | 10 ++++++- spec/shell/alias_checker_spec.rb | 3 +- spec/shell/alias_line_spec.rb | 27 ----------------- 6 files changed, 37 insertions(+), 64 deletions(-) delete mode 100644 lib/shell/alias_line.rb delete mode 100644 spec/shell/alias_line_spec.rb diff --git a/lib/huffshell.rb b/lib/huffshell.rb index c3078f1..4db52f4 100644 --- a/lib/huffshell.rb +++ b/lib/huffshell.rb @@ -21,7 +21,6 @@ def green require 'command/command_suggestor' require 'command/command_suggestion' -require 'shell/alias_line' require 'shell/script_line' require 'shell/alias_checker' require 'shell/binary_checker' diff --git a/lib/shell/alias_checker.rb b/lib/shell/alias_checker.rb index 3e8eb8e..4962f33 100644 --- a/lib/shell/alias_checker.rb +++ b/lib/shell/alias_checker.rb @@ -2,23 +2,37 @@ class AliasChecker attr_accessor :filename, :aliases def initialize(filename) - @filename = filename - - if !File.exist?(File.expand_path(filename)) - return - end - - file_contents = File.open(File.expand_path(filename), 'r').read - lines = file_contents.split("\n") + @filename = File.expand_path(filename) @aliases = {} - lines.each do |l| - al = AliasLine.new(l) - next unless al.valid? - @aliases[al.alias_name] = al.command + + if File.exist?(@filename) + load_aliases File.read(@filename) + else + case ENV["SHELL"] + when /bash/ + load_aliases `bash --login -i -c alias` + when /zsh/ + load_aliases `zsh --login -i -c alias` + end end end def exist?(alias_name) - @aliases && !!@aliases[alias_name] + @aliases.has_key? alias_name + end + + def get(alias_name) + @aliases[alias_name] + end + + private + + def load_aliases(content) + content.lines.each do |l| + al, command = l.chomp.split("=", 2) + next if command.nil? + command = command[1..-2] if command.start_with? "'" + @aliases[al] = command + end end end diff --git a/lib/shell/alias_line.rb b/lib/shell/alias_line.rb deleted file mode 100644 index 0292247..0000000 --- a/lib/shell/alias_line.rb +++ /dev/null @@ -1,20 +0,0 @@ -class AliasLine - attr_accessor :line - - def initialize(line) - @line = line - end - - def alias_name - @line.split("=").first - end - - def valid? - @line.scan("=") != [] - end - - def command - c = @line.split("=")[1] - c[1..(c.size-2)] if c - end -end \ No newline at end of file diff --git a/spec/command/command_suggestion_spec.rb b/spec/command/command_suggestion_spec.rb index d85ce5f..556d548 100644 --- a/spec/command/command_suggestion_spec.rb +++ b/spec/command/command_suggestion_spec.rb @@ -1,8 +1,16 @@ require 'spec_helper' +class DummyAliasChecker < AliasChecker + def initialize + @aliases = {} + end +end + describe CommandSuggestion do + dummy_alias_checker = DummyAliasChecker.new + before do - AliasChecker.stub(:new).and_return(nil) + AliasChecker.stub(:new).and_return(dummy_alias_checker) end it "create with list of words" do diff --git a/spec/shell/alias_checker_spec.rb b/spec/shell/alias_checker_spec.rb index d53bf54..f390e54 100644 --- a/spec/shell/alias_checker_spec.rb +++ b/spec/shell/alias_checker_spec.rb @@ -7,8 +7,7 @@ logs='nocorrect logs' mkdir='nocorrect mkdir' ZSHHISTORY - file = stub(:read => alias_file) - File.stub!(:open).and_return(file) + File.stub!(:read).and_return(alias_file) end it "is inialized with a filename" do diff --git a/spec/shell/alias_line_spec.rb b/spec/shell/alias_line_spec.rb deleted file mode 100644 index 287a9d8..0000000 --- a/spec/shell/alias_line_spec.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'spec_helper' - -describe AliasLine do - before(:each) do - @al = AliasLine.new("gap='git add -p'") - end - - it "is created with a string" do - AliasLine.new("gap='git add -p'") - end - - it "can tell if it is valid" do - al = AliasLine.new("gap='git add -p'") - al.should be_valid - - al = AliasLine.new("gap!LKJDSGHFHSD") - al.should_not be_valid - end - - it "returns the alias_name" do - @al.alias_name.should == "gap" - end - - it "returns the command" do - @al.command.should == "git add -p" - end -end From 84887d7c8ea24f9fb9e2ed2b5132dff24abd2aaa Mon Sep 17 00:00:00 2001 From: Baptiste Fontaine Date: Wed, 23 Dec 2015 02:08:23 +0100 Subject: [PATCH 2/5] =?UTF-8?q?Don=E2=80=99t=20suggest=20already-aliased?= =?UTF-8?q?=20commands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/command/command_suggestion.rb | 24 +++++++++++------------- lib/command/command_suggestor.rb | 7 +++---- lib/shell/alias_checker.rb | 2 +- spec/shell/alias_checker_spec.rb | 7 +++++++ 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/command/command_suggestion.rb b/lib/command/command_suggestion.rb index ad0f803..204dcf2 100644 --- a/lib/command/command_suggestion.rb +++ b/lib/command/command_suggestion.rb @@ -5,36 +5,34 @@ class CommandSuggestion def initialize(wordlist) @wordlist = wordlist + @command = wordlist.join " " + @alias_checker = AliasChecker.new("~/.aliases.cache") end def to_s if exists? - "'#{command}' => #{abbreviation} taken :(" + "'#{@command}' => #{abbreviation} taken :(" else - "'#{command}' => " + "#{abbreviation}".green + "'#{@command}' => " + "#{abbreviation}".green end end def abbreviation - wordlist.map{|w| CommandWord.suggested_letter(w) }.join.downcase + @abbreviation || @abbreviation = \ + wordlist.map{|w| CommandWord.suggested_letter(w) }.join.downcase end -private - - def command - wordlist.join(" ") + def valid? + abbreviation != @command && @alias_checker.get(abbreviation) != @command end +private + def exists? - binary_checker.exist? || (alias_checker && alias_checker.exist?(abbreviation)) + binary_checker.exist? || @alias_checker.exist?(abbreviation) end def binary_checker BinaryChecker.new(abbreviation) end - - def alias_checker - AliasChecker.new("~/.aliases.cache") - end - end diff --git a/lib/command/command_suggestor.rb b/lib/command/command_suggestor.rb index 1f91d30..11dcec2 100644 --- a/lib/command/command_suggestor.rb +++ b/lib/command/command_suggestor.rb @@ -7,8 +7,7 @@ def initialize() def add(line) sl = ScriptLine.new(line) - return unless sl.valid? - wordtree.add(sl) + wordtree.add(sl) if sl.valid? end def truncate!(minimum) @@ -22,7 +21,7 @@ def commands def to_tree wordtree.root.map do |n| cs = CommandSuggestion.new(n.word_list) - "#{n.to_tree.chomp} #{cs.to_s}" - end + "#{n.to_tree.chomp} #{cs.to_s}" if cs.valid? + end.compact end end diff --git a/lib/shell/alias_checker.rb b/lib/shell/alias_checker.rb index 4962f33..9ceba5e 100644 --- a/lib/shell/alias_checker.rb +++ b/lib/shell/alias_checker.rb @@ -32,7 +32,7 @@ def load_aliases(content) al, command = l.chomp.split("=", 2) next if command.nil? command = command[1..-2] if command.start_with? "'" - @aliases[al] = command + @aliases[al.sub(/^alias /, "")] = command end end end diff --git a/spec/shell/alias_checker_spec.rb b/spec/shell/alias_checker_spec.rb index f390e54..011a2a2 100644 --- a/spec/shell/alias_checker_spec.rb +++ b/spec/shell/alias_checker_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe AliasChecker do + shell = ENV["SHELL"] + before(:each) do alias_file = <<-ZSHHISTORY ls='ls -G' @@ -8,6 +10,11 @@ mkdir='nocorrect mkdir' ZSHHISTORY File.stub!(:read).and_return(alias_file) + ENV["SHELL"] = "not bash/zsh" + end + + after do + ENV["SHELL"] = shell end it "is inialized with a filename" do From 943b119cbd40eaa3f99806b6da450069f7ca080c Mon Sep 17 00:00:00 2001 From: Baptiste Fontaine Date: Wed, 23 Dec 2015 13:30:13 +0100 Subject: [PATCH 3/5] bin/huffshell: simplified code --- bin/huffshell | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/bin/huffshell b/bin/huffshell index 16fabde..2d62979 100755 --- a/bin/huffshell +++ b/bin/huffshell @@ -25,24 +25,21 @@ end puts cs.wordtree.root.line_count -puts "" -puts "=============" -puts [cs.commands.size.to_s.green, "unique keywords"].join(" ") +puts "\n=============" +puts "#{cs.commands.size.to_s.green} unique keywords" -puts "" -[250, 100, 50, 20, 10, 5, 2, 1].reverse.each do |count| - puts [cs.wordtree.root.minimum(count).keys.size.to_s.green, "commands appear > #{count} times"].join(" ") +puts +[1, 2, 5, 10, 20, 50, 100, 250].each do |count| + puts "#{cs.wordtree.root.minimum(count).size.to_s.green} commands appear > #{count} times" end -puts "" -puts "Most common commands:" +puts "\nMost common commands:" -cs.wordtree.root.minimum(10).sort{|a, b| a[1].line_count <=> b[1].line_count }.reverse[0,10].each do |word, node| - puts [node.word, node.line_count.to_s.green].join(" ") +cs.wordtree.root.minimum(10).sort_by {|e| e[1].line_count }.reverse[0,10].each do |word, node| + puts "#{node.word} #{node.line_count.to_s.green}" end cs.truncate!(30) -puts "" -puts "Command tree:" +puts "\nCommand tree:" puts cs.to_tree From 9e39f197291fef4b336dab7a86ff5496b519564e Mon Sep 17 00:00:00 2001 From: Baptiste Fontaine Date: Wed, 23 Dec 2015 13:40:12 +0100 Subject: [PATCH 4/5] Don't suggest special chars --- lib/command/command_word.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/command/command_word.rb b/lib/command/command_word.rb index 5f7a848..d90f60e 100644 --- a/lib/command/command_word.rb +++ b/lib/command/command_word.rb @@ -4,10 +4,10 @@ def self.clean(word) end def self.suggested_letter(word) - char = word.gsub(/(^\W)+/,"")[0,1].downcase + char = word.gsub(/^\W+/,"")[0,1].downcase return nil if char.empty? char rescue nil end -end \ No newline at end of file +end From 9959ab61e80d48b2832e353a6bf7efeaad19fc7a Mon Sep 17 00:00:00 2001 From: Baptiste Fontaine Date: Wed, 23 Dec 2015 13:40:36 +0100 Subject: [PATCH 5/5] Load the AliasChecker once and for all --- lib/command/command_suggestion.rb | 4 ++-- lib/command/command_suggestor.rb | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/command/command_suggestion.rb b/lib/command/command_suggestion.rb index 204dcf2..f6b6a2f 100644 --- a/lib/command/command_suggestion.rb +++ b/lib/command/command_suggestion.rb @@ -3,10 +3,10 @@ class CommandSuggestion SHORT_ENOUGH = 3 - def initialize(wordlist) + def initialize(wordlist, alias_checker=nil) @wordlist = wordlist @command = wordlist.join " " - @alias_checker = AliasChecker.new("~/.aliases.cache") + @alias_checker = alias_checker || AliasChecker.new("~/.aliases.cache") end def to_s diff --git a/lib/command/command_suggestor.rb b/lib/command/command_suggestor.rb index 11dcec2..b950860 100644 --- a/lib/command/command_suggestor.rb +++ b/lib/command/command_suggestor.rb @@ -19,8 +19,9 @@ def commands end def to_tree + alias_checker = AliasChecker.new "~/.aliases.cache" wordtree.root.map do |n| - cs = CommandSuggestion.new(n.word_list) + cs = CommandSuggestion.new(n.word_list, alias_checker) "#{n.to_tree.chomp} #{cs.to_s}" if cs.valid? end.compact end