Skip to content

Commit

Permalink
suggest replacements on "brew cask install" fail
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandwalker committed Mar 1, 2014
1 parent fe90e26 commit c95a7f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
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

3 comments on commit c95a7f2

@philoserf
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit may be the cause of this issue #4785
Additionally a test for a warning return on brew cask install <already_installed_cask> should be added.

The use case where this is a problem is brew bundle.

@philoserf
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of the difference between brew install vs. brew cask install in the case where a package is already installed:

Brew

brew install brew-cask
Warning: brew-cask-0.36.0 already installed

Brew Cask

brew cask install github
Error: Cask for github is already installed. Use `--force` to install anyways.

@rolandwalker
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is probably not exactly the cause of #4785, as there were a number of related changes, and the issue seems to have been around in one form or other for some time: #1347, #2677.

But, yes, this code you point out is being re-worked in #4787.

Please sign in to comment.