class Tablo::Table(T)
- Tablo::Table(T)
- Reference
- Object
Included Modules
- Enumerable(Tablo::Row(T))
Defined in:
table.crConstructors
-
.new(sources : Enumerable(T), *, title : Title = Config.title, subtitle : SubTitle = Config.subtitle, footer : Footer = Config.footer, group_alignment : Justify = Config.group_alignment, group_formatter : TextCellFormatter = Config.group_formatter, group_styler : TextCellStyler = Config.group_styler, header_alignment : Justify | Nil = Config.header_alignment, header_formatter : DataCellFormatter = Config.header_formatter, header_styler : DataCellStyler = Config.header_styler, body_alignment : Justify | Nil = Config.body_alignment, body_formatter : DataCellFormatter = Config.body_formatter, body_styler : DataCellStyler = Config.body_styler, border : Border = Config.border, left_padding : Int32 = Config.left_padding, right_padding : Int32 = Config.right_padding, padding_character : String = Config.padding_character, truncation_indicator : String = Config.truncation_indicator, width : Int32 = Config.width, header_frequency : Int32 | Nil = Config.header_frequency, row_divider_frequency : Int32 | Nil = Config.row_divider_frequency, wrap_mode : WrapMode = Config.wrap_mode, header_wrap : Int32 | Nil = Config.header_wrap, body_wrap : Int32 | Nil = Config.body_wrap, masked_headers : Bool = Config.masked_headers?, omit_group_header_rule : Bool = Config.omit_group_header_rule?, omit_last_rule : Bool = Config.omit_last_rule?)
Primary constructor, without block given.
-
.new(sources : Enumerable(T), *, title : Title = Config.title, subtitle : SubTitle = Config.subtitle, footer : Footer = Config.footer, group_alignment : Justify = Config.group_alignment, group_formatter : TextCellFormatter = Config.group_formatter, group_styler : TextCellStyler = Config.group_styler, header_alignment : Justify | Nil = Config.header_alignment, header_formatter : DataCellFormatter = Config.header_formatter, header_styler : DataCellStyler = Config.header_styler, body_alignment : Justify | Nil = Config.body_alignment, body_formatter : DataCellFormatter = Config.body_formatter, body_styler : DataCellStyler = Config.body_styler, border : Border = Config.border, left_padding : Int32 = Config.left_padding, right_padding : Int32 = Config.right_padding, padding_character : String = Config.padding_character, truncation_indicator : String = Config.truncation_indicator, width : Int32 = Config.width, header_frequency : Int32 | Nil = Config.header_frequency, row_divider_frequency : Int32 | Nil = Config.row_divider_frequency, wrap_mode : WrapMode = Config.wrap_mode, header_wrap : Int32 | Nil = Config.header_wrap, body_wrap : Int32 | Nil = Config.body_wrap, masked_headers : Bool = Config.masked_headers?, omit_group_header_rule : Bool = Config.omit_group_header_rule?, omit_last_rule : Bool = Config.omit_last_rule?, &)
Second constructor, with same parameters as the first one, but with a block given
Class Method Summary
-
.rowtype_memory : RowType | Nil
class property to manage transition between successive rows issued from data source
-
.rowtype_memory=(rowtype_memory : RowType | Nil)
class property to manage transition between successive rows issued from data source
-
.transition_footer : Footer | Nil
Class properties to manage row types framing and summary table linking for summary table
-
.transition_footer=(transition_footer : Footer | Nil)
Class properties to manage row types framing and summary table linking for summary table
Macro Summary
-
initialize(block_given)
The
initialize
macro generates two `initialize' method, one with block_given = true and one with block_given = false
Instance Method Summary
-
#add_column(label : LabelType, *, header = label.to_s, header_alignment = header_alignment, header_formatter = header_formatter, header_styler = header_styler, body_alignment = body_alignment, body_formatter = body_formatter, body_styler = body_styler, left_padding = left_padding, right_padding = right_padding, padding_character = padding_character, width = width, truncation_indicator = truncation_indicator, wrap_mode = wrap_mode, &extractor : T, Int32 -> CellType)
Adds a column to the table
-
#add_group(label, *, header = label.to_s, alignment = group_alignment, formatter = group_formatter, styler = group_styler, padding_character = padding_character, truncation_indicator = truncation_indicator, wrap_mode = wrap_mode)
Adds a group to the table
- #border
- #border=(border : Tablo::Border)
- #footer
- #footer=(footer : Tablo::Footer)
-
#horizontal_rule(position = Position::Bottom, groups = nil)
Produce a horizontal dividing line suitable for printing at the top, bottom or middle of the table, or before or after the table title, if any.
- #old2_pack(width : TableWidth | Nil = GetWidthFrom::Screen, *, starting_widths : StartingWidths = Config.starting_widths, except : Except | Nil = nil)
- #old_each(&)
- #old_pack(width : TableWidth | Nil = GetWidthFrom::Screen, *, init : PackInit | Nil = PackInit::AutoSize, except : Except | Nil = nil)
-
#pack(width : Int32 | Nil = nil, *, starting_widths : StartingWidths = Config.starting_widths, except : Except | Nil = nil)
The
#pack
method allows for adapting the total width of the table. -
#reset_sources(to src : Enumerable(T))
Changes the sources data used by the table
-
#sources
Table parameters
- #subtitle
- #subtitle=(subtitle : Tablo::SubTitle)
-
#summary(summary_def, **summary_options)
The summary method, with two parameters, is used to define a new SummaryTable by applying user-defined functions to the numerical values of columns, such as Sum or Average functions.
-
#summary
Returns a previously defined summary table
- #title
- #title=(title : Tablo::Title)
-
#total_table_width
returns the total actual width of the table as a whole TODO : to be renamed to 'width' ??? but wait for global refactoring and renaming
- #transpose(**opts)
- #zzz_update_summary_widths
Constructor Detail
Class Method Detail
class property to manage transition between successive rows issued from data source
class property to manage transition between successive rows issued from data source
Macro Detail
The initialize
macro generates two `initialize' method, one with block_given = true
and one with block_given = false
Instance Method Detail
Adds a column to the table
Parameters:
-
label is the only (positional) mandatory parameter, of type
LabelType
-
A block (&extractor) must be provided to the method call to extract the cell raw data from the source. Extractor type is
Proc(T, Int32, CellType)
.
All other are optional named parameters, and have default values taken from Table parameters, except header which defaults to label
Returns an instance of class Column(T)
Adds a group to the table
A group is the set of the last defined columns not attached to a group yet.
Parameters:
- label is the only (positional) mandatory parameter, of type
LabelType
All other are optional named parameters, and have default values taken from Table parameters, except header which defaults to label
Returns an instance of class TextCell
Produce a horizontal dividing line suitable for printing at the top, bottom or middle of the table, or before or after the table title, if any.
This method is also suitable to customize output of a table, for example, to list a table with an horizontal rule between rows, as in :
table.each_with_index do |row, i|
puts table.horizontal_rule(Tablo::Position::Middle) unless i == 0
puts row
end
puts table.horizontal_rule(Tablo::Position::Bottom)
The method calculates an array of column widths (including padding), passing
it to the Border.horizontal_rule
method.
-
position indicates the type of horizontal rule expected
-
Returns a String representing the formatted horizontal rule
The #pack
method allows for adapting the total width of the table.
It accepts 3 parameters, all optional:
-
width
: total width required for the formatted table. If nowidth
is given and if the value of parameterConfig.terminal_capped_width
is true, the value ofwidth
is read from the size of the terminal, otherwise its value isnil
and in that case, onlystarting_widths == AutoSized
has an effect. -
starting_widths
: column widths taken as starting point for resizing, possible values are :Current
: resizing starts from columns current widthInitial
: current values are reset to their initial values, at column definition timeAutoSized
: current values are set to their 'best fit' values, ie they are automatically adapted to their largest content
-
except
: column or array of columns excluded from being resized, identified by their label
The following examples will illustrate the behaviour of the different parameters values, starting from the 'standard' one, with all column widths to their default value : 12 characters.
returns the Table itself
Changes the sources data used by the table
- reset the summary table to nil
- returns the new sources
The summary method, with two parameters, is used to define a new SummaryTable by applying user-defined functions to the numerical values of columns, such as Sum or Average functions.
parameters:
- summary_def, of type
Hash(LabelType, NamedTuple)
allows you to define column content and formatting.
Permitted NamedTuple keys and value types are :
- body_alignment:
Justify
- header_alignment:
Justify
- header:
String
- body_formatter:
DataCellFormatter
- header_formatter:
DataCellFormatter
- body_styler:
DataCellStyler
- header_styler:
DataCellStyler
and - any (unique) key:
SummaryNumCol
orSummaryNumCols
for example:
{
"A" => {
header: "Sum",
header_formatter: ->(c : Tablo::CellType) { c.to_s.downcase },
body_styler: ->(c : Tablo::CellType, s : String) { s.colorize(:yellow).to_s },
proc: ->(ary : Tablo::NumCol) { ary.sum.to_i },
},
"B" => {
header: "Sum/Avg",
proc1: ->(ary : Tablo::NumCol) { ary.sum.to_i },
proc2: ->(ary : Tablo::NumCols) { ary["B"].size > 0 ? (ary["B"].sum/ary["B"].size).to_s : "NA" },
},
}
- summary_options , of type
Hash(LabelType, NamedTuple)
allows you to redefine table initialization parameters. By default, the current parameters of the main table are used.
Parameters | Default values
-----------------------+------------------------------
title | default Table value
subtitle | idem
footer | idem
footer | idem
Returns the summary table
returns the total actual width of the table as a whole
TODO : to be renamed to 'width' ??? but wait for global refactoring and renaming