Delegate --profile tabulation to `terminal-table` (#7627)

Merge pull request 7627
This commit is contained in:
Ashwin Maroli 2019-08-05 01:33:56 +05:30 committed by jekyllbot
parent 91f82907a3
commit f446aebf07
3 changed files with 19 additions and 61 deletions

View File

@ -46,6 +46,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("pathutil", "~> 0.9") s.add_runtime_dependency("pathutil", "~> 0.9")
s.add_runtime_dependency("rouge", "~> 3.0") s.add_runtime_dependency("rouge", "~> 3.0")
s.add_runtime_dependency("safe_yaml", "~> 1.0") s.add_runtime_dependency("safe_yaml", "~> 1.0")
s.add_runtime_dependency("terminal-table", "~> 1.8")
s.post_install_message = <<~MSG s.post_install_message = <<~MSG
------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------

View File

@ -10,72 +10,29 @@ module Jekyll
end end
def to_s(num_of_rows = 50) def to_s(num_of_rows = 50)
data = data_for_table(num_of_rows) tabulate(data_for_table(num_of_rows))
widths = table_widths(data)
generate_table(data, widths)
end end
private private
def generate_table(data, widths) def tabulate(data)
str = +"\n" require "terminal-table"
table_head = data.shift header = data.shift
table_foot = data.pop footer = data.pop
output = +"\n"
str << generate_row(table_head, widths) table = Terminal::Table.new do |t|
str << generate_table_head_border(table_head, widths) t << header
t << :separator
data.each do |row_data| data.each { |row| t << row }
str << generate_row(row_data, widths) t << :separator
t << footer
t.style = { :alignment => :right, :border_top => false, :border_bottom => false }
t.align_column(0, :left)
end end
str << generate_table_head_border(table_foot, widths) output << table.to_s << "\n"
str << generate_row(table_foot, widths).rstrip
str << "\n"
str
end
def generate_table_head_border(row_data, widths)
str = +""
row_data.each_index do |cell_index|
str << "-" * widths[cell_index]
str << "-+-" unless cell_index == row_data.length - 1
end
str << "\n"
str
end
def generate_row(row_data, widths)
str = +""
row_data.each_with_index do |cell_data, cell_index|
str << if cell_index.zero?
cell_data.ljust(widths[cell_index], " ")
else
cell_data.rjust(widths[cell_index], " ")
end
str << " | " unless cell_index == row_data.length - 1
end
str << "\n"
str
end
def table_widths(data)
widths = []
data.each do |row|
row.each_with_index do |cell, index|
widths[index] = [cell.length, widths[index]].compact.max
end
end
widths
end end
# rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/AbcSize

View File

@ -16,9 +16,9 @@ class TestLiquidRenderer < JekyllUnitTest
# rubocop:disable Metrics/LineLength # rubocop:disable Metrics/LineLength
expected = [ expected = [
%r!^Filename\s+|\s+Count\s+|\s+Bytes\s+|\s+Time$!, %r!^\| Filename\s+|\s+Count\s+|\s+Bytes\s+|\s+Time$!,
%r!^-+\++-+\++-+\++-+$!, %r!^\+(?:-+\+){4}$!,
%r!^_posts/2010-01-09-date-override\.markdown\s+|\s+\d+\s+|\s+\d+\.\d{2}K\s+|\s+\d+\.\d{3}$!, %r!^\|_posts/2010-01-09-date-override\.markdown\s+|\s+\d+\s+|\s+\d+\.\d{2}K\s+|\s+\d+\.\d{3}$!,
] ]
# rubocop:enable Metrics/LineLength # rubocop:enable Metrics/LineLength