Class: ChefCLI::Policyfile::Reports::TablePrinter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(ui) {|_self| ... } ⇒ TablePrinter

Returns a new instance of TablePrinter.

Yields:

  • (_self)

Yield Parameters:



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

#uiObject (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 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