Move Formatter.columns into utils/formatter.
This commit is contained in:
parent
ef70677e88
commit
581a1245bf
@ -1,4 +1,3 @@
|
|||||||
require "utils/formatter/columns"
|
|
||||||
require "utils/tty"
|
require "utils/tty"
|
||||||
|
|
||||||
module Formatter
|
module Formatter
|
||||||
@ -50,4 +49,45 @@ module Formatter
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
private_class_method :prefix
|
private_class_method :prefix
|
||||||
|
|
||||||
|
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 { |index|
|
||||||
|
objects[index] + "".rjust(col_width - object_lengths[index])
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,46 +0,0 @@
|
|||||||
require "utils/tty"
|
|
||||||
|
|
||||||
module Formatter
|
|
||||||
module_function
|
|
||||||
|
|
||||||
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 { |index|
|
|
||||||
objects[index] + "".rjust(col_width - object_lengths[index])
|
|
||||||
}
|
|
||||||
|
|
||||||
# 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
|
|
||||||
end
|
|
||||||
Loading…
x
Reference in New Issue
Block a user