Ruport::Formatter::Text

This class provides text output for Ruport's Row, Table, Group, and Grouping renderers

It handles things like automatically truncating tables that go off the edge of the screen in the console, proper column alignment, and pretty output that looks something like this:

+------------------------------+
| apple | banana | strawberry  |
+------------------------------+
| yes   | no     | yes         |
| yes   | yes    | red snapper |
| what  | the    | red snapper |
+------------------------------+

Supported Options

:max_col_width: Ordinal array of column widths. Set automatically but can be overridden.

:alignment: Defaults to left justify text and right justify numbers. Centers all fields when set to :center.

:table_width: Will truncate rows at this limit.

:show_table_headers: Defaults to true

:show_group_headers: Defaults to true

:ignore_table_width: When set to true, outputs full table without truncating it. Useful for file output.

Public Instance Methods

apply_template() click to toggle source

Hook for setting available options using a template. See the template documentation for the available options and their format.

# File lib/ruport/formatter/text.rb, line 57
def apply_template
  apply_table_format_template(template.table_format)
  apply_column_format_template(template.column_format)
  apply_grouping_format_template(template.grouping_format)
end
build_group_body() click to toggle source

Creates the group body. Since group data is a table, just uses the Table renderer.

# File lib/ruport/formatter/text.rb, line 145
def build_group_body
  render_table data, options
end
build_group_header() click to toggle source

Renders the header for a group using the group name.

# File lib/ruport/formatter/text.rb, line 138
def build_group_header
  output << "#{data.name}:\n\n"
end
build_grouping_body() click to toggle source

Generates the body for a grouping. Iterates through the groups and renders them using the group renderer.

# File lib/ruport/formatter/text.rb, line 152
def build_grouping_body
  render_inline_grouping(options)
end
build_row() click to toggle source

Generates a formatted text row.

Defaults to numeric values being right justified, and other values being left justified. Can be changed to support centering of output by setting options.alignment to :center

Uses fit_to_width to truncate the row if necessary.

# File lib/ruport/formatter/text.rb, line 121
def build_row
  max_col_widths_for_row(data) unless max_col_width

  data.enum_for(:each_with_index).inject(line=[]) { |s,e|
    field,index = e
    if alignment.eql? :center
      line << field.to_s.center(max_col_width[index])
    else
      align = field.is_a?(Numeric) ? :rjust : :ljust
      line << field.to_s.send(align, max_col_width[index])
    end
  }
  output << fit_to_width("| #{line.join(' | ')} |\n")
end
build_table_body() click to toggle source

Generates the body of the text table.

Defaults to numeric values being right justified, and other values being left justified. Can be changed to support centering of output by setting options.alignment to :center

Uses fit_to_width to truncate the table if necessary.

# File lib/ruport/formatter/text.rb, line 95
def build_table_body
  output << fit_to_width(hr)
  return if data.empty?

  calculate_max_col_widths unless max_col_width

  render_data_by_row do |rend|
    rend.options do |o|
      o.max_col_width = max_col_width
      o.alignment     = alignment
      o.table_width   = table_width   
      o.ignore_table_width = ignore_table_width
    end
  end

  output << fit_to_width(hr)
end
build_table_header() click to toggle source

Uses the column names from the given Data::Table to generate a table header.

Calls fit_to_width to truncate the table heading if necessary.

# File lib/ruport/formatter/text.rb, line 77
def build_table_header
  return unless should_render_column_names?

  c = data.column_names.enum_for(:each_with_index).map { |f,i|
    f.to_s.center(max_col_width[i])
  }

  output << fit_to_width("#{hr}| #{c.join(' | ')} |\n")
end
calculate_max_col_widths() click to toggle source

Determines the text widths for each column.

# File lib/ruport/formatter/text.rb, line 192
def calculate_max_col_widths
  # allow override
  return if max_col_width

  options.max_col_width = []

  unless data.column_names.empty?
    data.column_names.each_index do |i| 
      max_col_width[i] = data.column_names[i].to_s.length
    end
  end
      
  data.each { |r| max_col_widths_for_row(r) } 
end
fit_to_width(s) click to toggle source

Truncates a string so that it does not exceed Text#width

# File lib/ruport/formatter/text.rb, line 181
def fit_to_width(s)      
  return s if options.ignore_table_width
  # workaround for Rails setting terminal_width to 1
  max_width = width < 2 ? 80 : width
  
  s.split("\n").each { |r|
     r.gsub!(/\A.{#{max_width+1},}/) { |m| m[0,max_width-2] + ">>" }
  }.join("\n") + "\n"
end
hr() click to toggle source

Generates the horizontal rule by calculating the total table width and then generating a bar that looks like this:

"+------------------+"
# File lib/ruport/formatter/text.rb, line 167
def hr
  ref = data.column_names.empty? ? data[0].to_a : data.column_names
  len = max_col_width.inject(ref.length * 3) {|s,e|s+e}
  "+" + "-"*(len-1) + "+\n"
end
max_col_widths_for_row(row) click to toggle source

Used to calculate the max_col_widths array. Override this to tweak the automatic column size adjustments.

# File lib/ruport/formatter/text.rb, line 209
def max_col_widths_for_row(row)
  options.max_col_width ||= []
  row.each_with_index do |f,i|
    if !max_col_width[i] || f.to_s.length > max_col_width[i]
      max_col_width[i] = f.to_s.length
    end
  end
end
prepare_table() click to toggle source

Checks to ensure the table is not empty and then calls calculate_max_col_widths.

# File lib/ruport/formatter/text.rb, line 66
def prepare_table
  raise Ruport::FormatterError, "Can't output table without " +
    "data or column names." if data.empty? && data.column_names.empty?
  calculate_max_col_widths
end
should_render_column_names?() click to toggle source

Returns false if column_names are empty or options.show_table_headers is false/nil. Returns true otherwise.

# File lib/ruport/formatter/text.rb, line 159
def should_render_column_names?
  not data.column_names.empty? || !show_table_headers
end
width() click to toggle source

Returns options.table_width if specified.

Otherwise, uses SystemExtensions to determine terminal width.

# File lib/ruport/formatter/text.rb, line 176
def width
  table_width || SystemExtensions.terminal_width
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.