Module: Formatter

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

Class Method Summary collapse

Class Method Details

.arrow(string, color: nil) ⇒ Object



8
9
10
# File 'brew/Library/Homebrew/utils/formatter.rb', line 8

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

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'brew/Library/Homebrew/utils/formatter.rb', line 76

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.freeze
end

.error(string, label: nil) ⇒ Object



32
33
34
# File 'brew/Library/Homebrew/utils/formatter.rb', line 32

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

.headline(string, color: nil) ⇒ Object



12
13
14
# File 'brew/Library/Homebrew/utils/formatter.rb', line 12

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

.identifier(string) ⇒ Object



16
17
18
# File 'brew/Library/Homebrew/utils/formatter.rb', line 16

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

.option(string) ⇒ Object



20
21
22
# File 'brew/Library/Homebrew/utils/formatter.rb', line 20

def option(string)
  "#{Tty.bold}#{string}#{Tty.reset}"
end

.success(string, label: nil) ⇒ Object



24
25
26
# File 'brew/Library/Homebrew/utils/formatter.rb', line 24

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

.url(string) ⇒ Object



53
54
55
# File 'brew/Library/Homebrew/utils/formatter.rb', line 53

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

.warning(string, label: nil) ⇒ Object



28
29
30
# File 'brew/Library/Homebrew/utils/formatter.rb', line 28

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

.wrap(s, width = 172) ⇒ Object

Wraps text to fit within a given number of columns using regular expressions that:

  1. convert hard-wrapped paragraphs to a single line
  2. find any option descriptions longer than a pre-set length and wrap between words with a hanging indent, without breaking any words that overflow
  3. wrap any remaining description lines that need wrapping with the same indent
  4. wrap all lines to the given width.


44
45
46
47
48
49
50
51
# File 'brew/Library/Homebrew/utils/formatter.rb', line 44

def wrap(s, width = 172)
  desc = OPTION_DESC_WIDTH
  indent = width - desc
  s.gsub(/(?<=\S) *\n(?=\S)/, " ")
   .gsub(/^( +-.+  +(?=\S.{#{desc}}))(.{1,#{desc}})( +|$)\n?/, "\\1\\2\n" + " " * indent)
   .gsub(/^( {#{indent}}(?=\S.{#{desc}}))(.{1,#{desc}})( +|$)\n?/, "\\1\\2\n" + " " * indent)
   .gsub(/(.{1,#{width}})( +|$)\n?/, "\\1\n")
end