Method not available on this version
This method is only available on newer versions. The first available version (v2_6_3) is shown here.
print_table(array, options = {})public
Prints a table.
Parameters
Array[Array[String, String, …]]
Options
|
Indent the first column by indent value. | |
|
Force the first column to colwidth spaces wide. |
# File lib/bundler/vendor/thor/lib/thor/shell/basic.rb, line 157
def print_table(array, options = {}) # rubocop:disable MethodLength
return if array.empty?
formats = []
indent = options[:indent].to_i
colwidth = options[:colwidth]
options[:truncate] = terminal_width if options[:truncate] == true
formats << "%-#{colwidth + 2}s".dup if colwidth
start = colwidth ? 1 : 0
colcount = array.max { |a, b| a.size <=> b.size }.size
maximas = []
start.upto(colcount - 1) do |index|
maxima = array.map { |row| row[index] ? row[index].to_s.size : 0 }.max
maximas << maxima
formats << if index == colcount - 1
# Don't output 2 trailing spaces when printing the last column
"%-s".dup
else
"%-#{maxima + 2}s".dup
end
end
formats[0] = formats[0].insert(0, " " * indent)
formats << "%s"
array.each do |row|
sentence = "".dup
row.each_with_index do |column, index|
maxima = maximas[index]
f = if column.is_a?(Numeric)
if index == row.size - 1
# Don't output 2 trailing spaces when printing the last column
"%#{maxima}s"
else
"%#{maxima}s "
end
else
formats[index]
end
sentence << f % column.to_s
end
sentence = truncate(sentence, options[:truncate]) if options[:truncate]
stdout.puts sentence
end
end Related methods
- Instance methods
- ask
- error
- file_collision
- indent
- mute
- mute?
- no?
- padding=
- print_in_columns
- print_table
- print_wrapped
- say
- say_status
- set_color
- terminal_width
- yes?
- Class methods
- new
- Protected methods
-
as_unicode -
ask_filtered -
ask_simply -
can_display_colors? -
dynamic_width -
dynamic_width_stty -
dynamic_width_tput -
file_collision_help -
is? -
lookup_color -
prepare_message -
quiet? -
show_diff -
stderr -
stdout -
truncate -
unix?