Class: ChefCLI::Policyfile::Reports::TablePrinter
- Inherits:
-
Object
- Object
- ChefCLI::Policyfile::Reports::TablePrinter
- Defined in:
- lib/chef-cli/policyfile/reports/table_printer.rb
Overview
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.
Instance Attribute Summary collapse
-
#ui ⇒ Object
readonly
Returns the value of attribute ui.
Instance Method Summary collapse
-
#column(collection = []) ⇒ Object
Defines a column.
-
#initialize(ui) {|_self| ... } ⇒ TablePrinter
constructor
A new instance of TablePrinter.
-
#print_row(*cells) ⇒ Object
Print a row.
Constructor Details
#initialize(ui) {|_self| ... } ⇒ TablePrinter
Returns a new instance of TablePrinter.
30 31 32 33 34 35 |
# File 'lib/chef-cli/policyfile/reports/table_printer.rb', line 30 def initialize(ui) @ui = ui @column_widths = [] yield self end |
Instance Attribute Details
#ui ⇒ Object (readonly)
Returns the value of attribute ui.
28 29 30 |
# File 'lib/chef-cli/policyfile/reports/table_printer.rb', line 28 def ui @ui end |
Instance Method Details
#column(collection = []) ⇒ Object
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.
40 41 42 |
# File 'lib/chef-cli/policyfile/reports/table_printer.rb', line 40 def column(collection = []) @column_widths << (collection.map(&:to_s).map(&:size).max || 0) end |
#print_row(*cells) ⇒ Object
Print a row.
45 46 47 48 49 50 51 52 |
# File 'lib/chef-cli/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 |