diff options
| author | Markus Reiter | 2016-10-15 18:51:11 +0200 |
|---|---|---|
| committer | GitHub | 2016-10-15 18:51:11 +0200 |
| commit | c233cacaf283a42940bdf3f607d82dab07d746a4 (patch) | |
| tree | 2c27248754a6c2b234adc7faae70e28c67ca0d17 /Library/Homebrew/cask | |
| parent | 527a62b64bd8c578af08540166a8b1fd5bce521f (diff) | |
| parent | 581a1245bf2c038a2f35fb10445592c7655108e3 (diff) | |
| download | brew-c233cacaf283a42940bdf3f607d82dab07d746a4.tar.bz2 | |
Merge pull request #1208 from reitermarkus/puts-columns
Refactor `puts_columns` to allow `$stderr.puts_columns`.
Diffstat (limited to 'Library/Homebrew/cask')
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/cli/install.rb | 9 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/cli/list.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cask/lib/hbc/cli/search.rb | 2 | ||||
| -rw-r--r-- | Library/Homebrew/cask/spec/formatter_spec.rb | 56 |
4 files changed, 63 insertions, 6 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/cli/install.rb b/Library/Homebrew/cask/lib/hbc/cli/install.rb index 996de8d1b..3e9ce4e4f 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/install.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/install.rb @@ -42,13 +42,14 @@ module Hbc def self.warn_unavailable_with_suggestion(cask_token, e) exact_match, partial_matches = Search.search(cask_token) - errmsg = e.message + error_message = e.message if exact_match - errmsg.concat(". Did you mean:\n#{exact_match}") + error_message.concat(". Did you mean:\n#{exact_match}") elsif !partial_matches.empty? - errmsg.concat(". Did you mean one of:\n#{puts_columns(partial_matches.take(20))}\n") + error_message.concat(". Did you mean one of:\n") + .concat(Formatter.columns(partial_matches.take(20))) end - onoe errmsg + onoe error_message end def self.help diff --git a/Library/Homebrew/cask/lib/hbc/cli/list.rb b/Library/Homebrew/cask/lib/hbc/cli/list.rb index 3a993f8e6..330c81b90 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/list.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/list.rb @@ -68,7 +68,7 @@ module Hbc elsif @options[:versions] puts installed_casks.map(&method(:format_versioned)) else - puts_columns installed_casks.map(&:to_s) + puts Formatter.columns(installed_casks.map(&:to_s)) end installed_casks.empty? ? nil : true diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index b671ea862..147e6d194 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -47,7 +47,7 @@ module Hbc else ohai "Partial matches" end - puts_columns partial_matches + puts Formatter.columns(partial_matches) end end diff --git a/Library/Homebrew/cask/spec/formatter_spec.rb b/Library/Homebrew/cask/spec/formatter_spec.rb new file mode 100644 index 000000000..7e143d933 --- /dev/null +++ b/Library/Homebrew/cask/spec/formatter_spec.rb @@ -0,0 +1,56 @@ +require "spec_helper" +require "utils/formatter" +require "utils/tty" + +describe Formatter do + describe "::columns" do + let(:input) { + [ + 'aa', + 'bbb', + 'ccc', + 'dd' + ] + } + subject { described_class.columns(input) } + + it "doesn't output columns if $stdout is not a TTY." do + allow_any_instance_of(IO).to receive(:tty?).and_return(false) + allow(Tty).to receive(:width).and_return(10) + + expect(subject).to eq( + "aa\n" \ + "bbb\n" \ + "ccc\n" \ + "dd\n" + ) + end + + describe "$stdout is a TTY" do + it "outputs columns" do + allow_any_instance_of(IO).to receive(:tty?).and_return(true) + allow(Tty).to receive(:width).and_return(10) + + expect(subject).to eq( + "aa ccc\n" \ + "bbb dd\n" + ) + end + + it "outputs only one line if everything fits" do + allow_any_instance_of(IO).to receive(:tty?).and_return(true) + allow(Tty).to receive(:width).and_return(20) + + expect(subject).to eq( + "aa bbb ccc dd\n" + ) + end + end + + describe "with empty input" do + let(:input) { [] } + + it { is_expected.to eq("\n") } + end + end +end |
