Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several speed and aliases improvements #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions bin/huffshell
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 12 additions & 14 deletions lib/command/command_suggestion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,36 @@ class CommandSuggestion

SHORT_ENOUGH = 3

def initialize(wordlist)
def initialize(wordlist, alias_checker=nil)
@wordlist = wordlist
@command = wordlist.join " "
@alias_checker = 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
10 changes: 5 additions & 5 deletions lib/command/command_suggestor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -20,9 +19,10 @@ def commands
end

def to_tree
alias_checker = AliasChecker.new "~/.aliases.cache"
wordtree.root.map do |n|
cs = CommandSuggestion.new(n.word_list)
"#{n.to_tree.chomp} #{cs.to_s}"
end
cs = CommandSuggestion.new(n.word_list, alias_checker)
"#{n.to_tree.chomp} #{cs.to_s}" if cs.valid?
end.compact
end
end
4 changes: 2 additions & 2 deletions lib/command/command_word.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
1 change: 0 additions & 1 deletion lib/huffshell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
40 changes: 27 additions & 13 deletions lib/shell/alias_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.sub(/^alias /, "")] = command
end
end
end
20 changes: 0 additions & 20 deletions lib/shell/alias_line.rb

This file was deleted.

10 changes: 9 additions & 1 deletion spec/command/command_suggestion_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions spec/shell/alias_checker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
require 'spec_helper'

describe AliasChecker do
shell = ENV["SHELL"]

before(:each) do
alias_file = <<-ZSHHISTORY
ls='ls -G'
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)
ENV["SHELL"] = "not bash/zsh"
end

after do
ENV["SHELL"] = shell
end

it "is inialized with a filename" do
Expand Down
27 changes: 0 additions & 27 deletions spec/shell/alias_line_spec.rb

This file was deleted.