class ChefDK::Policyfile::Reports::TablePrinter
Defines a table with a flexible number of columns and prints rows in the table. Columns are defined ahead of time, by calling the column
method, individual rows are printed by calling print_row
with the data for each cell.
Attributes
ui[R]
Public Class Methods
new(ui) { |self| ... }
click to toggle source
# File lib/chef-dk/policyfile/reports/table_printer.rb, line 30 def initialize(ui) @ui = ui @column_widths = [] yield self end
Public Instance Methods
column(collection = [])
click to toggle source
Defines a column. If a collection is given, it is mapped to an array of strings and the longest string is used as the left justify width for that column when rows are printed.
# File lib/chef-dk/policyfile/reports/table_printer.rb, line 40 def column(collection = []) @column_widths << (collection.map(&:to_s).map(&:size).max || 0) end
print_row(*cells)
click to toggle source
Print a row.
# File lib/chef-dk/policyfile/reports/table_printer.rb, line 45 def print_row(*cells) row = "" cells.each_with_index do |cell_data, i| row << cell_data.to_s.ljust(@column_widths[i]) row << " " end ui.msg(row.strip) end