| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  | require "utils/tty" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 07:58:25 +02:00
										 |  |  | # Helper module for formatting output. | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2024-04-22 21:05:48 +02:00
										 |  |  | # @api internal | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  | module Formatter | 
					
						
							| 
									
										
										
										
											2024-04-03 20:17:02 -07:00
										 |  |  |   COMMAND_DESC_WIDTH = 80
 | 
					
						
							|  |  |  |   OPTION_DESC_WIDTH = 45
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.arrow(string, color: nil) | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |     prefix("==>", string, color) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-23 19:10:33 +02:00
										 |  |  |   # Format a string as headline. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api internal | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.headline(string, color: nil) | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |     arrow("#{Tty.bold}#{string}#{Tty.reset}", color:) | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.identifier(string) | 
					
						
							| 
									
										
										
										
											2016-10-17 00:45:37 -04:00
										 |  |  |     "#{Tty.green}#{string}#{Tty.default}" | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.option(string) | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |     "#{Tty.bold}#{string}#{Tty.reset}" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-22 21:05:48 +02:00
										 |  |  |   # Format a string as success, with an optional label. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api internal | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.success(string, label: nil) | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |     label(label, string, :green) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-22 21:05:48 +02:00
										 |  |  |   # Format a string as warning, with an optional label. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api internal | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.warning(string, label: nil) | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |     label(label, string, :yellow) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-22 21:05:48 +02:00
										 |  |  |   # Format a string as error, with an optional label. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api internal | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.error(string, label: nil) | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |     label(label, string, :red) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-09 13:00:15 -05:00
										 |  |  |   # Wraps text to fit within a given number of columns using regular expressions that: | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # 1. convert hard-wrapped paragraphs to a single line | 
					
						
							| 
									
										
										
										
											2020-04-18 12:07:28 -04:00
										 |  |  |   # 2. add line break and indent to subcommand descriptions | 
					
						
							|  |  |  |   # 3. find any option descriptions longer than a pre-set length and wrap between words | 
					
						
							| 
									
										
										
										
											2019-03-09 13:00:15 -05:00
										 |  |  |   #    with a hanging indent, without breaking any words that overflow | 
					
						
							| 
									
										
										
										
											2020-04-18 12:07:28 -04:00
										 |  |  |   # 4. wrap any remaining description lines that need wrapping with the same indent | 
					
						
							|  |  |  |   # 5. wrap all lines to the given width. | 
					
						
							| 
									
										
										
										
											2022-01-04 23:29:12 -05:00
										 |  |  |   # | 
					
						
							|  |  |  |   # 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. | 
					
						
							|  |  |  |   # @see https://github.com/Homebrew/brew/pull/12672 | 
					
						
							| 
									
										
										
										
											2019-03-09 13:00:15 -05:00
										 |  |  |   # @see https://macromates.com/blog/2006/wrapping-text-with-regular-expressions/ | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.format_help_text(string, width: 172) | 
					
						
							| 
									
										
										
										
											2019-03-09 13:00:15 -05:00
										 |  |  |     desc = OPTION_DESC_WIDTH | 
					
						
							|  |  |  |     indent = width - desc | 
					
						
							| 
									
										
										
										
											2023-03-08 15:30:25 +00:00
										 |  |  |     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") | 
					
						
							| 
									
										
										
										
											2018-09-08 22:21:04 +05:30
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.url(string) | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |     "#{Tty.underline}#{string}#{Tty.no_underline}" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.label(label, string, color) | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |     label = "#{label}:" unless label.nil? | 
					
						
							|  |  |  |     prefix(label, string, color) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |   private_class_method :label | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.prefix(prefix, string, color) | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  |     if prefix.nil? && color.nil? | 
					
						
							|  |  |  |       string | 
					
						
							|  |  |  |     elsif prefix.nil? | 
					
						
							|  |  |  |       "#{Tty.send(color)}#{string}#{Tty.reset}" | 
					
						
							|  |  |  |     elsif color.nil? | 
					
						
							|  |  |  |       "#{prefix} #{string}" | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       "#{Tty.send(color)}#{prefix}#{Tty.reset} #{string}" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |   private_class_method :prefix | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-22 21:05:48 +02:00
										 |  |  |   # Layout objects in columns that fit the current terminal width. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @api internal | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |   def self.columns(*objects, gap_size: 2) | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  |     objects = objects.flatten.map(&:to_s) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fallback = proc do | 
					
						
							|  |  |  |       return objects.join("\n").concat("\n") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fallback.call if objects.empty? | 
					
						
							| 
									
										
										
										
											2023-03-09 21:10:44 -08:00
										 |  |  |     fallback.call if respond_to?(:tty?) ? !T.unsafe(self).tty? : !$stdout.tty? | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     console_width = Tty.width | 
					
						
							|  |  |  |     object_lengths = objects.map { |obj| Tty.strip_ansi(obj).length } | 
					
						
							| 
									
										
										
										
											2023-03-09 21:10:44 -08:00
										 |  |  |     cols = (console_width + gap_size) / (T.must(object_lengths.max) + gap_size) | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fallback.call if cols < 2
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     rows = (objects.count + cols - 1) / cols | 
					
						
							|  |  |  |     cols = (objects.count + rows - 1) / rows # avoid empty trailing columns | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 10:13:43 +01:00
										 |  |  |     col_width = ((console_width + gap_size) / cols) - gap_size | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     gap_string = "".rjust(gap_size) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 17:33:02 +09:00
										 |  |  |     output = +"" | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     rows.times do |row_index| | 
					
						
							| 
									
										
										
										
											2023-03-09 21:10:44 -08:00
										 |  |  |       item_indices_for_row = T.cast(row_index.step(objects.size - 1, rows).to_a, T::Array[Integer]) | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 21:10:44 -08:00
										 |  |  |       first_n = T.must(item_indices_for_row[0...-1]).map do |index| | 
					
						
							|  |  |  |         objects[index] + "".rjust(col_width - object_lengths.fetch(index)) | 
					
						
							| 
									
										
										
										
											2016-10-22 13:32:46 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # don't add trailing whitespace to last column | 
					
						
							| 
									
										
										
										
											2023-03-09 21:10:44 -08:00
										 |  |  |       last = objects.values_at(item_indices_for_row.fetch(-1)) | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 17:33:02 +09:00
										 |  |  |       output.concat((first_n + last) | 
					
						
							|  |  |  |             .join(gap_string)) | 
					
						
							|  |  |  |             .concat("\n") | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 17:33:02 +09:00
										 |  |  |     output.freeze | 
					
						
							| 
									
										
										
										
											2016-10-15 17:15:20 +02:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-08-30 21:38:13 +02:00
										 |  |  | end |