From bf2315b1f4ae0cb7c9bb80b651ef6389f66020b2 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 7 Dec 2015 13:11:34 +0000 Subject: [PATCH] Use `(installed)` and emoji ticks consistently. Across info, search and update. Closes Homebrew/homebrew#45131. Signed-off-by: Mike McQuaid --- Library/Homebrew/cmd/info.rb | 18 +------------ Library/Homebrew/cmd/search.rb | 5 ++-- Library/Homebrew/cmd/update.rb | 28 +++++++-------------- Library/Homebrew/utils.rb | 46 +++++++++++++++++++++++++++------- 4 files changed, 49 insertions(+), 48 deletions(-) diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index 68e4c54257..a11044d5c9 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -147,24 +147,8 @@ module Homebrew end def decorate_dependencies(dependencies) - # necessary for 1.8.7 unicode handling since many installs are on 1.8.7 - tick = ["2714".hex].pack("U*") - cross = ["2718".hex].pack("U*") - deps_status = dependencies.collect do |dep| - if dep.installed? - color = Tty.green - symbol = tick - else - color = Tty.red - symbol = cross - end - if ENV["HOMEBREW_NO_EMOJI"] - colored_dep = "#{color}#{dep}" - else - colored_dep = "#{dep} #{color}#{symbol}" - end - "#{colored_dep}#{Tty.reset}" + dep.installed? ? pretty_installed(dep) : pretty_uninstalled(dep) end deps_status * ", " end diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index 3bf25a4b6b..1bd9420946 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -42,8 +42,7 @@ module Homebrew query = ARGV.first rx = query_regexp(query) local_results = search_formulae(rx) - local_results_installed = local_results.select { |f| f.end_with? "(installed)" } - puts_columns(local_results, local_results_installed) + puts_columns(local_results) tap_results = search_taps(rx) puts_columns(tap_results) @@ -157,7 +156,7 @@ module Homebrew if aliases.include?(name) && results.include?(canonical_full_name) next elsif (HOMEBREW_CELLAR/canonical_name).directory? - "#{name} (installed)" + pretty_installed(name) else name end diff --git a/Library/Homebrew/cmd/update.rb b/Library/Homebrew/cmd/update.rb index 6ee2726a94..f4cd9d712e 100644 --- a/Library/Homebrew/cmd/update.rb +++ b/Library/Homebrew/cmd/update.rb @@ -455,30 +455,20 @@ class Report end def dump_formula_report(key, title) - formula = select_formula(key) - unless formula.empty? - # Determine list item indices of installed formulae. - formula_installed_index = formula.each_index.select do |index| - name, newname = formula[index] - installed?(name) || (newname && installed?(newname)) - end - - # Format list items of renamed formulae. + formula = select_formula(key).map do |name, new_name| + # Format list items of renamed formulae if key == :R - formula.map! { |oldname, newname| "#{oldname} -> #{newname}" } + new_name = pretty_installed(new_name) if installed?(name) + "#{name} -> #{new_name}" + else + installed?(name) ? pretty_installed(name) : name end + end - # Append suffix '(installed)' to list items of installed formulae. - formula_installed_index.each do |index| - formula[index] += " (installed)" - end - - # Fetch list items of installed formulae for highlighting. - formula_installed = formula.values_at(*formula_installed_index) - + unless formula.empty? # Dump formula list. ohai title - puts_columns(formula, formula_installed) + puts_columns(formula) end end diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 5700314f28..c874ec03cc 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -9,6 +9,20 @@ require "open-uri" class Tty class << self + def tick + # necessary for 1.8.7 unicode handling since many installs are on 1.8.7 + @tick ||= ["2714".hex].pack("U*") + end + + def cross + # necessary for 1.8.7 unicode handling since many installs are on 1.8.7 + @cross ||= ["2718".hex].pack("U*") + end + + def strip_ansi(string) + string.gsub(/\033\[\d+(;\d+)*m/, "") + end + def blue bold 34 end @@ -103,6 +117,26 @@ def odie(error) exit 1 end +def pretty_installed(f) + if !$stdout.tty? + "#{f}" + elsif ENV["HOMEBREW_NO_EMOJI"] + "#{Tty.highlight}#{Tty.green}#{f} (installed)#{Tty.reset}" + else + "#{Tty.highlight}#{f} #{Tty.green}#{Tty.tick}#{Tty.reset}" + end +end + +def pretty_uninstalled(f) + if !$stdout.tty? + "#{f}" + elsif ENV["HOMEBREW_NO_EMOJI"] + "#{Tty.red}#{f} (uninstalled)#{Tty.reset}" + else + "#{f} #{Tty.red}#{Tty.cross}#{Tty.reset}" + end +end + def pretty_duration(s) return "2 seconds" if s < 3 # avoids the plural problem ;) return "#{s.to_i} seconds" if s < 120 @@ -267,7 +301,7 @@ def curl(*args) safe_system curl, *args end -def puts_columns(items, highlight = []) +def puts_columns(items) return if items.empty? unless $stdout.tty? @@ -278,7 +312,8 @@ def puts_columns(items, highlight = []) # TTY case: If possible, output using multiple columns. console_width = Tty.width console_width = 80 if console_width <= 0 - max_len = items.max_by(&:length).length + plain_item_lengths = items.map { |s| Tty.strip_ansi(s).length } + max_len = plain_item_lengths.max col_gap = 2 # number of spaces between columns gap_str = " " * col_gap cols = (console_width + col_gap) / (max_len + col_gap) @@ -286,13 +321,6 @@ def puts_columns(items, highlight = []) rows = (items.size + cols - 1) / cols cols = (items.size + rows - 1) / rows # avoid empty trailing columns - plain_item_lengths = items.map(&:length) if cols >= 2 - if highlight && highlight.any? - items = items.map do |item| - highlight.include?(item) ? "#{Tty.highlight}#{item}#{Tty.reset}" : item - end - end - if cols >= 2 col_width = (console_width + col_gap) / cols - col_gap items = items.each_with_index.map do |item, index|