lib/tabulo/table.rb
def add_column(label, align_body: nil, align_header: nil, before: nil, formatter: nil,
      header: nil, header_styler: nil, padding: nil, styler: nil, width: nil, wrap_preserve: nil, &extractor)

      column_label = normalize_column_label(label)

      left_padding, right_padding =
        if padding
          Array === padding ? padding : [padding, padding]
        else
          [@left_column_padding, @right_column_padding]
        end

      if column_registry.include?(column_label)
        raise InvalidColumnLabelError, "Column label already used in this table."
      end

      column = Column.new(
        align_body: align_body || @align_body,
        align_header: align_header || @align_header,
        extractor: extractor || label.to_proc,
        formatter: formatter || @formatter,
        header: (header || label).to_s,
        header_styler: header_styler || @header_styler,
        index: column_registry.count,
        left_padding: left_padding,
        padding_character: PADDING_CHARACTER,
        right_padding: right_padding,
        styler: styler || @styler,
        truncation_indicator: @truncation_indicator,
        wrap_preserve: wrap_preserve || @wrap_preserve,
        width: width || @column_width,
      )

      if before == nil
        add_column_final(column, column_label)
      else
        add_column_before(column, column_label, before)
      end
    end
# Adds a column to the Table.
#
# @param [Symbol, String, Integer] label A unique identifier for this column, which by
#   default will also be used as the column header text (see also the header param). If the
#   extractor argument is not also provided, then the label argument should correspond to
#   a method to be called on each item in the table sources to provide the content
#   for this column. If a String is passed as the label, then it will be converted to
#   a Symbol for the purpose of serving as this label.
# @param [:left, :center, :right, :auto, nil] align_body Specifies how the cell body contents
#   should be aligned. If <tt>nil</tt> is passed, then the alignment is determined
#   by the Table-level setting passed to the <tt>align_body</tt> option on Table initialization
#   (which itself defaults to <tt>:auto</tt>). Otherwise this option determines the alignment of
#   this column. If <tt>:auto</tt> is passed, the alignment is determined by the type of the cell
#   value, with numbers aligned right, booleans center-aligned, and other values left-aligned.
#   Note header text alignment is configured separately using the :align_header param.
# @param [:left, :center, :right, nil] align_header Specifies how the header text
#   should be aligned. If <tt>nil</tt> is passed, then the alignment is determined
#   by the Table-level setting passed to the <tt>align_header</tt> (which itself defaults
#   to <tt>:center</tt>). Otherwise, this option determines the alignment of the header
#   content for this column.
# @param [Symbol, String, Integer, nil] before The label of the column before (i.e. to
#   the left of) which the new column should inserted. If <tt>nil</tt> is passed, it will be
#   inserted after all other columns. If there is no column with the given label, then an
#   {InvalidColumnLabelError} will be raised. A non-Integer labelled column can be identified
#   in either String or Symbol form for this purpose.
# @param [#to_proc] formatter A lambda or other callable object that
#   will be passed the calculated value of each cell to determine how it should be displayed. This
#   is distinct from the extractor and the styler (see below).
#   For example, if the extractor for this column generates a Date, then the formatter might format
#   that Date in a particular way.
#   * If <tt>nil</tt> is provided, then the callable that was passed to the `formatter` option
#    of the table itself on its creation (see {#initialize}) (which itself defaults to
#    `:to_s.to_proc`), will be used as the formatter for the column.
#   * If a 1-parameter callable is passed, then this callable will be called with the calculated
#     value of the cell; it should then return a String, and this String will be displayed as
#     the formatted value of the cell.
#   * If a 2-parameter callable is passed, then the first parameter represents the calculated
#     value of the cell, and the second parameter is a {CellData} instance, containing
#     additional information about the cell that may be relevant to what formatting should
#     be applied. For example, the {CellData#row_index} attribute can be inspected to determine
#     whether the {Cell} is an odd- or even-numbered {Row}, to arrange for different formatting
#     to be applied to alternating rows.
#     See the documentation for {CellData} for more.
# @param [nil, #to_s] header Text to be displayed in the column header. If passed nil,
#   the column's label will also be used as its header text.
# @param [nil, #to_proc] header_styler (nil) A lambda or other callable object that will
#   determine the colors or other styling applied to the header content. Can be passed
#   <tt>nil</tt>, or can be passed a callable that takes 1, 2 or 3 parameters:
#   * If passed <tt>nil</tt>, then no additional styling will be applied to the cell content
#     (other than what was already applied by the <tt>formatter</tt>).
#   * If passed a callable, then that callable will be called for each line of content within
#     the header cell, and the resulting string rendered in place of that line.
#     The extra width of the string returned by the <tt>header_styler</tt> is not taken into
#     consideration by the internal table and cell width calculations involved in rendering the
#     table. Thus it can be used to apply ANSI escape codes to header cell content, to color the
#     cell content for example, without breaking the table formatting.
#     * If the passed callable takes 1 parameter, then the first parameter is a string
#       representing a single formatted line within the header cell. For example, if the header
#       cell content is wrapped over three lines, then the <tt>header_styler</tt> will be called
#       three times for that header cell, once for each line of content.
#     * If the passed callable takes 2 parameters, then the first parameter is as above, and the
#       second parameter is an Integer representing the positional index of this header's {Column},
#       with the leftmost column having index 0, the next having index 1 etc.. This can be
#       used, for example, to apply different styles to alternating {Column}s.
#     * If the passed callable takes 3 parameters, then the first and second parameters are as above,
#       and the third parameter is an Integer representing the index of the line within the
#       header cell that is currently being styled. For example, if the cell content is wrapped over 3
#       lines, then the callable will be called first with a line index of 0, to style the first line,
#       then with a line index of 1, to style the second line, and finally with a line index of 2, for
#       the third and final wrapped line of the cell.
#
#   Note that if the header content is truncated, then any <tt>header_styler</tt> will be applied to the
#   truncation indicator character as well as to the truncated content.
# @param [nil, Integer, Array] padding Determines the amount of blank space with which to
#   pad either side of the column. If passed nil, then the `column_padding` setting of the
#   {Table} will determine the column's padding. (See {#initialize}.) Otherwise, this option
#   overrides, for this column, the `column_padding` that was set at the table level: if passed an Integer,
#   then the given amount of padding is applied to either side of the column; or if passed a two-element Array,
#   then the first element of the Array indicates the amount of padding to apply to the left of the column,
#   and the second element indicates the amount to apply to the right.
# @param [nil, #to_proc] styler A lambda or other callable object that will determine
#   the colors or other styling applied to the formatted value of the cell. Can be passed
#   <tt>nil</tt>, or can be passed a callable that takes either 2 or 3 parameters:
#   * If passed <tt>nil</tt>, then no additional styling will be applied to the cell content
#     (other than what was already applied by the <tt>formatter</tt>).
#   * If passed a callable, then that callable will be called for each line of content within
#     the cell, and the resulting string rendered in place of that line.
#     The <tt>styler</tt> option differs from the <tt>formatter</tt> option in that the extra width of the
#     string returned by <tt>styler</tt> is not taken into consideration by the internal table and
#     cell width calculations involved in rendering the table. Thus it can be used to apply
#     ANSI escape codes to cell content, to color the cell content for example, without
#     breaking the table formatting.
#     * If the passed callable takes 2 parameters, then the first parameter is the calculated
#       value of the cell (prior to the <tt>formatter</tt> being applied); and the second parameter is
#       a string representing a single formatted line within the cell. For example, if the cell
#       content is wrapped over three lines, then for that cell, the <tt>styler</tt> will be called
#       three times, once for each line of content within the cell.
#     * If the passed callable takes 3 parameters, then the first two parameters are as above,
#       and the third parameter is a {CellData} instance, containing additional information
#       about the cell that may be relevant to what styles should be applied. For example, the
#       {CellData#row_index} attribute can be inspected to determine whether the {Cell} is an
#       odd- or even-numbered {Row}, to arrange for different styling to be applied to
#       alternating rows. See the documentation for {CellData} for more.
#     * If the passed callable takes 4 parameters, then the first three parameters are as above,
#       and the fourth parameter is an Integer representing the index of the line within the
#       cell that is currently being styled. For example, if the cell content is wrapped over 3
#       lines, then the callable will be called first with a line index of 0, to style the first
#       line, then with a line index of 1, to style the second line, and finally with a line
#       index of 2, to style the third and final wrapped line of the cell.
#
#   Note that if the content of a cell is truncated, then the whatever styling is applied by the
#   <tt>styler</tt> to the cell content will also be applied to the truncation indicator character.
# @param [Integer] width Specifies the width of the column, excluding padding. If
#   nil, then the column will take the width provided by the `column_width` param
#   with which the Table was initialized.
# @param [Symbol] wrap_preserve Specifies how to wrap column content when the column width is reached.
#   * If passed `nil`, or not provided, then the value passed to the `wrap_preserve` param of
#     {#initialize} will be used.
#   * If passed `rune` or word, then the option passed to {#initialize} will be overridden for
#     this column. See the documentation under {#initialize} for the behaviour of each option.
# @param [#to_proc] extractor A block or other callable that will be passed each of the {Table}
#   sources to determine the value in each cell of this column.
#   * If this is not provided, then the column label will be treated as a method to be called on
#     each source item to determine each cell's value.
#   * If provided a single-parameter callable, then this callable will be passed each of the
#     {Table} sources to determine the cell value for each row in this column.
#   * If provided a 2-parameter callable, then for each of the {Table} sources, this callable
#     will be passed the source, and the row index, to determine the cell value for that row.
#     For this purpose, the first body row (not counting the header row) has an index of 0,
#     the next an index of 1, etc..
# @raise [InvalidColumnLabelError] if label has already been used for another column in this
#   Table. (This is case-sensitive, but is insensitive to whether a String or Symbol is passed
#   to the label parameter.)
Adds a column to the Table.
Adds a column to the Table .
def add_column(label, align_body: nil, align_header: nil, before: nil, formatter: nil,
      header: nil, header_styler: nil, padding: nil, styler: nil, width: nil, wrap_preserve: nil, &extractor)
      column_label = normalize_column_label(label)
      left_padding, right_padding =
        if padding
          Array === padding ? padding : [padding, padding]
        else
          [@left_column_padding, @right_column_padding]
        end
      if column_registry.include?(column_label)
        raise InvalidColumnLabelError, "Column label already used in this table."
      end
      column = Column.new(
        align_body: align_body || @align_body,
        align_header: align_header || @align_header,
        extractor: extractor || label.to_proc,
        formatter: formatter || @formatter,
        header: (header || label).to_s,
        header_styler: header_styler || @header_styler,
        index: column_registry.count,
        left_padding: left_padding,
        padding_character: PADDING_CHARACTER,
        right_padding: right_padding,
        styler: styler || @styler,
        truncation_indicator: @truncation_indicator,
        wrap_preserve: wrap_preserve || @wrap_preserve,
        width: width || @column_width,
      )
      if before == nil
        add_column_final(column, column_label)
      else
        add_column_before(column, column_label, before)
      end
    end
def add_column ( label , align_body : nil , align_header : nil , before : nil , formatter : nil , header : nil , header_styler : nil , padding : nil , styler : nil , width : nil , wrap_preserve : nil , & extractor ) column_label = normalize_column_label ( label ) left_padding , right_padding = if padding Array === padding ? padding : [ padding , padding ] else [ @left_column_padding , @right_column_padding ] end if column_registry . include? ( column_label ) raise InvalidColumnLabelError , "Column label already used in this table." end column = Column . new ( align_body : align_body || @align_body , align_header : align_header || @align_header , extractor : extractor || label . to_proc , formatter : formatter || @formatter , header : ( header || label ) . to_s , header_styler : header_styler || @header_styler , index : column_registry . count , left_padding : left_padding , padding_character : PADDING_CHARACTER , right_padding : right_padding , styler : styler || @styler , truncation_indicator : @truncation_indicator , wrap_preserve : wrap_preserve || @wrap_preserve , width : width || @column_width , ) if before == nil add_column_final ( column , column_label ) else add_column_before ( column , column_label , before ) end end
Adds a column to the Table.
Adds a column to the Table .
[InvalidColumnLabelError] if label has already been used for another column in this Table. (This is case-sensitive, but is insensitive to whether a String or Symbol is passed to the label parameter.)
[ InvalidColumnLabelError ] if label has already been used for another column in this Table . ( This is case - sensitive but is insensitive to whether a String or Symbol is passed to the label parameter . )
label
align_body
align_header
before
formatter
header
header_styler
padding
styler
width
wrap_preserve
extractor
extractor A block or other callable that will be passed each of the {Table} sources to determine the value in each cell of this column. * If this is not provided, then the column label will be treated as a method to be called on each source item to determine each cell's value. * If provided a single-parameter callable, then this callable will be passed each of the {Table} sources to determine the cell value for each row in this column. * If provided a 2-parameter callable, then for each of the {Table} sources, this callable will be passed the source, and the row index, to determine the cell value for that row. For this purpose, the first body row (not counting the header row) has an index of 0, the next an index of 1, etc
extractor A block or other callable that will be passed each of the { Table } sources to determine the value in each cell of this column . * If this is not provided then the column label will be treated as a method to be called on each source item to determine each cell ' s value . * If provided a single - parameter callable then this callable will be passed each of the { Table } sources to determine the cell value for each row in this column . * If provided a 2 - parameter callable then for each of the { Table } sources this callable will be passed the source and the row index to determine the cell value for that row . For this purpose the first body row ( not counting the header row ) has an index of 0 the next an index of 1 etc