Module: Formatter
- Defined in:
- utils/formatter.rb
Overview
This module is part of an internal API. This module may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Helper module for formatting output.
Constant Summary collapse
- COMMAND_DESC_WIDTH =
This constant is part of an internal API. This constant may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
80- OPTION_DESC_WIDTH =
This constant is part of an internal API. This constant may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
45
Class Method Summary collapse
- .arrow(string, color: nil) ⇒ String private
-
.bold(string) ⇒ String
private
Keep in sync with
boldin Library/Homebrew/utils/formatter.sh. -
.columns(objects, gap_size: 2, min_width: 0) ⇒ String
internal
Layout objects in columns that fit the current terminal width.
- .disk_usage_readable(size_in_bytes) ⇒ String private
- .disk_usage_readable_size_unit(size_in_bytes, precision: nil) ⇒ Array<([Integer, Float], String)> private
-
.error(string, label: nil) ⇒ String
internal
Format a string as error, with an optional label.
-
.format_help_text(string, width: 172) ⇒ String
private
Wraps text to fit within a given number of columns using regular expressions that:.
-
.headline(string, color: nil) ⇒ String
internal
Format a string as headline.
- .identifier(string) ⇒ String private
- .number_readable(number) ⇒ String private
- .option(string) ⇒ String private
- .redact_secrets(input, secrets) ⇒ String private
-
.success(string, label: nil) ⇒ String
internal
Format a string as success, with an optional label.
-
.truncate(string, max: 30, omission: "...") ⇒ String
internal
Truncate a string to a specific length.
- .url(string) ⇒ String private
-
.warning(string, label: nil) ⇒ String
internal
Format a string as warning, with an optional label.
Class Method Details
.arrow(string, color: nil) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
14 15 16 |
# File 'utils/formatter.rb', line 14 def self.arrow(string, color: nil) prefix("==>", string, color) end |
.bold(string) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Keep in sync with bold in Library/Homebrew/utils/formatter.sh.
34 35 36 |
# File 'utils/formatter.rb', line 34 def self.bold(string) "#{Tty.bold}#{string}#{Tty.reset}" end |
.columns(objects, gap_size: 2, min_width: 0) ⇒ String
This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Layout objects in columns that fit the current terminal width.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'utils/formatter.rb', line 136 def self.columns(objects, gap_size: 2, min_width: 0) 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?) ? !T.unsafe(self).tty? : !$stdout.tty? console_width = Tty.width object_lengths = objects.map { |obj| Tty.strip_ansi(obj).length } max_length = [*object_lengths, min_width].max || 0 cols = (console_width + gap_size) / (max_length + gap_size) fallback.call if cols < 2 rows = (objects.count + cols - 1) / cols cols = (objects.count + rows - 1) / rows if min_width.zero? # 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 = T.cast(row_index.step(objects.size - 1, rows).to_a, T::Array[Integer]) first_n = T.must(item_indices_for_row[0...-1]).map do |index| objects.fetch(index) + "".rjust(col_width - object_lengths.fetch(index)) end # don't add trailing whitespace to last column last = objects.values_at(item_indices_for_row.fetch(-1)) output.concat((first_n + last) .join(gap_string)) .concat("\n") end output.freeze end |
.disk_usage_readable(size_in_bytes) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
198 199 200 201 202 203 204 205 206 |
# File 'utils/formatter.rb', line 198 def self.disk_usage_readable(size_in_bytes) size, unit = disk_usage_readable_size_unit(size_in_bytes) # avoid trailing zero after decimal point if ((size * 10).to_i % 10).zero? "#{size.to_i}#{unit}" else "#{format("%<size>.1f", size:)}#{unit}" end end |
.disk_usage_readable_size_unit(size_in_bytes, precision: nil) ⇒ Array<([Integer, Float], String)>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
185 186 187 188 189 190 191 192 193 194 195 |
# File 'utils/formatter.rb', line 185 def self.disk_usage_readable_size_unit(size_in_bytes, precision: nil) size = size_in_bytes unit = "B" %w[KB MB GB].each do |next_unit| break if (precision ? size.abs.round(precision) : size.abs) < 1000 size /= 1000.0 unit = next_unit end [size, unit] end |
.error(string, label: nil) ⇒ String
This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Format a string as error, with an optional label.
63 64 65 |
# File 'utils/formatter.rb', line 63 def self.error(string, label: nil) label(label, string, :red) end |
.format_help_text(string, width: 172) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Wraps text to fit within a given number of columns using regular expressions that:
- convert hard-wrapped paragraphs to a single line
- add line break and indent to subcommand descriptions
- find any option descriptions longer than a pre-set length and wrap between words with a hanging indent, without breaking any words that overflow
- wrap any remaining description lines that need wrapping with the same indent
- wrap all lines to the given width.
Note that an option (e.g. --foo) may not be at the beginning of a line,
so we always wrap one word before an option.
94 95 96 97 98 99 100 101 102 |
# File 'utils/formatter.rb', line 94 def self.format_help_text(string, width: 172) desc = OPTION_DESC_WIDTH indent = width - desc string.gsub(/(?<=\S) *\n(?=\S)/, " ") .gsub(/([`>)\]]:) /, "\\1\n ") .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 |
.headline(string, color: nil) ⇒ String
This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Format a string as headline.
Keep in sync with headline in Library/Homebrew/utils/formatter.sh.
23 24 25 |
# File 'utils/formatter.rb', line 23 def self.headline(string, color: nil) arrow("#{Tty.bold}#{string}#{Tty.reset}", color:) end |
.identifier(string) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
28 29 30 |
# File 'utils/formatter.rb', line 28 def self.identifier(string) "#{Tty.green}#{string}#{Tty.default}" end |
.number_readable(number) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
209 210 211 212 213 |
# File 'utils/formatter.rb', line 209 def self.number_readable(number) numstr = number.to_i.to_s (numstr.size - 3).step(1, -3) { |i| numstr.insert(i.to_i, ",") } numstr end |
.option(string) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
39 40 41 |
# File 'utils/formatter.rb', line 39 def self.option(string) bold(string) end |
.redact_secrets(input, secrets) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
216 217 218 219 220 |
# File 'utils/formatter.rb', line 216 def self.redact_secrets(input, secrets) secrets.compact .reduce(input) { |str, secret| str.gsub secret, "******" } .freeze end |
.success(string, label: nil) ⇒ String
This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Format a string as success, with an optional label.
47 48 49 |
# File 'utils/formatter.rb', line 47 def self.success(string, label: nil) label(label, string, :green) end |
.truncate(string, max: 30, omission: "...") ⇒ String
This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Truncate a string to a specific length.
71 72 73 74 75 76 77 78 |
# File 'utils/formatter.rb', line 71 def self.truncate(string, max: 30, omission: "...") return string if string.length <= max length_with_room_for_omission = max - omission.length truncated = string[0, length_with_room_for_omission] "#{truncated}#{omission}" end |
.url(string) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
105 106 107 |
# File 'utils/formatter.rb', line 105 def self.url(string) "#{Tty.underline}#{string}#{Tty.no_underline}" end |
.warning(string, label: nil) ⇒ String
This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Format a string as warning, with an optional label.
55 56 57 |
# File 'utils/formatter.rb', line 55 def self.warning(string, label: nil) label(label, string, :yellow) end |