Module: Formatter

Defined in:
Library/Homebrew/utils/formatter.rb

Class Method Summary collapse

Class Method Details

.arrow(string, color: nil) ⇒ Object


6
7
8
# File 'Library/Homebrew/utils/formatter.rb', line 6

def arrow(string, color: nil)
  prefix("==>", string, color)
end

.columns(*objects, gap_size: 2) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'Library/Homebrew/utils/formatter.rb', line 53

def columns(*objects, gap_size: 2)
  objects = objects.flatten.map(&:to_s)

  fallback = proc do
    return objects.join("\n").concat("\n")
  end

  fallback.call if objects.empty?
  fallback.call if respond_to?(:tty?) ? !tty? : !$stdout.tty?

  console_width = Tty.width
  object_lengths = objects.map { |obj| Tty.strip_ansi(obj).length }
  cols = (console_width + gap_size) / (object_lengths.max + gap_size)

  fallback.call if cols < 2

  rows = (objects.count + cols - 1) / cols
  cols = (objects.count + rows - 1) / rows # avoid empty trailing columns

  col_width = (console_width + gap_size) / cols - gap_size

  gap_string = "".rjust(gap_size)

  output = ""

  rows.times do |row_index|
    item_indices_for_row = row_index.step(objects.size - 1, rows).to_a

    first_n = item_indices_for_row[0...-1].map do |index|
      objects[index] + "".rjust(col_width - object_lengths[index])
    end

    # don't add trailing whitespace to last column
    last = objects.values_at(item_indices_for_row.last)

    output.concat((first_n + last).join(gap_string)).concat("\n")
  end

  output
end

.error(string, label: nil) ⇒ Object


26
27
28
# File 'Library/Homebrew/utils/formatter.rb', line 26

def error(string, label: nil)
  label(label, string, :red)
end

.headline(string, color: nil) ⇒ Object


10
11
12
# File 'Library/Homebrew/utils/formatter.rb', line 10

def headline(string, color: nil)
  arrow("#{Tty.bold}#{string}#{Tty.reset}", color: color)
end

.identifier(string) ⇒ Object


14
15
16
# File 'Library/Homebrew/utils/formatter.rb', line 14

def identifier(string)
  "#{Tty.green}#{string}#{Tty.default}"
end

.pluralize(count, singular, plural = nil, show_count: true) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'Library/Homebrew/utils/formatter.rb', line 94

def pluralize(count, singular, plural = nil, show_count: true)
  return (show_count ? "#{count} #{singular}" : singular.to_s) if count == 1

  *adjectives, noun = singular.to_s.split(" ")

  plural ||= {
    "formula" => "formulae",
  }.fetch(noun, "#{noun}s")

  words = adjectives.push(plural).join(" ")

  show_count ? "#{count} #{words}" : words
end

.success(string, label: nil) ⇒ Object


18
19
20
# File 'Library/Homebrew/utils/formatter.rb', line 18

def success(string, label: nil)
  label(label, string, :green)
end

.url(string) ⇒ Object


30
31
32
# File 'Library/Homebrew/utils/formatter.rb', line 30

def url(string)
  "#{Tty.underline}#{string}#{Tty.no_underline}"
end

.warning(string, label: nil) ⇒ Object


22
23
24
# File 'Library/Homebrew/utils/formatter.rb', line 22

def warning(string, label: nil)
  label(label, string, :yellow)
end