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

suggest replacements on "brew cask install" fail #2706

Merged
merged 1 commit into from
Mar 27, 2014
Merged
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
15 changes: 13 additions & 2 deletions lib/cask/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ def self.run(*args)
force = args.include? '--force'
cask_names.each do |cask_name|
odebug "Installing Cask #{cask_name}"
cask = Cask.load(cask_name)
Cask::Installer.new(cask).install(force)
begin
cask = Cask.load(cask_name)
Cask::Installer.new(cask).install(force)
rescue CaskUnavailableError => e
exact_match, partial_matches, search_term = Cask::CLI::Search.search(cask_name)
errmsg = "#{cask_name}"
if exact_match
errmsg.concat(". Did you mean:\n#{exact_match}")
elsif !partial_matches.empty?
errmsg.concat(". Did you mean one of:\n#{stringify_columns(partial_matches.take(20))}\n")
end
raise CaskUnavailableError.new(errmsg)
end
end
end

Expand Down
26 changes: 26 additions & 0 deletions lib/cask/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# see Homebrew Library/Homebrew/utils.rb

require 'yaml'
require 'open3'

# monkeypatch Tty
class Tty
Expand Down Expand Up @@ -65,3 +66,28 @@ def odumpcask cask
end
end
end

# from Homebrew puts_columns
def stringify_columns items, star_items=[]
return if items.empty?

if star_items && star_items.any?
items = items.map{|item| star_items.include?(item) ? "#{item}*" : item}
end

if $stdout.tty?
# determine the best width to display for different console sizes
console_width = `/bin/stty size`.chomp.split(" ").last.to_i
console_width = 80 if console_width <= 0
else
console_width = 80
end
longest = items.sort_by { |item| item.length }.last
optimal_col_width = (console_width.to_f / (longest.length + 2).to_f).floor
cols = optimal_col_width > 1 ? optimal_col_width : 1
Open3.popen3('/usr/bin/pr', "-#{cols}", '-t', "-w#{console_width}") do |stdin, stdout, stderr|
stdin.puts(items)
stdin.close
stdout.read
end
end
14 changes: 14 additions & 0 deletions test/cask/cli/install_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@
}.must_raise CaskUnavailableError
end

it "returns a suggestion for a misspelled Cask" do
e = lambda {
Cask::CLI::Install.run('googlechrome')
}.must_raise CaskUnavailableError
e.message.must_equal "No available cask for googlechrome\. Did you mean:\ngoogle-chrome"
end

it "returns multiple suggestions for a Cask fragment" do
e = lambda {
Cask::CLI::Install.run('google')
}.must_raise CaskUnavailableError
e.message.must_match %r{^No available cask for google\. Did you mean one of:\ngoogle}
end

it "raises an exception when no cask is specified" do
lambda {
Cask::CLI::Install.run
Expand Down