Module: CommandKit::Printing::Tables
- Includes:
- Indent
- Defined in:
- lib/command_kit/printing/tables.rb,
lib/command_kit/printing/tables/style.rb,
lib/command_kit/printing/tables/row_builder.rb,
lib/command_kit/printing/tables/border_style.rb,
lib/command_kit/printing/tables/cell_builder.rb,
lib/command_kit/printing/tables/table_builder.rb,
lib/command_kit/printing/tables/table_formatter.rb
Overview
Methods for printing tables.
Examples
include CommandKit::Printing::Tables
def run
header = ['A', 'B', 'C']
table = [
['AAAA', 'BBBB', 'CCCC'],
['AAAA', 'BBBB', 'CCCC'],
['AAAA', 'BBBB', 'CCCC']
]
print_table table
# AAAA BBBB CCCC
# AAAA BBBB CCCC
# AAAA BBBB CCCC
print_table table, header: header
# A B C
#
# AAAA BBBB CCCC
# AAAA BBBB CCCC
# AAAA BBBB CCCC
print_table table, header: header,
border: :ascii
# +------+------+------+
# | A | B | C |
# +------+------+------+
# | AAAA | BBBB | CCCC |
# | AAAA | BBBB | CCCC |
# | AAAA | BBBB | CCCC |
# +------+------+------+
print_table table, header: header,
border: :ascii,
separate_rows: true
# +------+------+------+
# | A | B | C |
# +------+------+------+
# | AAAA | BBBB | CCCC |
# +------+------+------+
# | AAAA | BBBB | CCCC |
# +------+------+------+
# | AAAA | BBBB | CCCC |
# +------+------+------+
print_table table, header: header,
border: :line
# ┌──────┬──────┬──────┐
# │ A │ B │ C │
# ├──────┼──────┼──────┤
# │ AAAA │ BBBB │ CCCC │
# │ AAAA │ BBBB │ CCCC │
# │ AAAA │ BBBB │ CCCC │
# └──────┴──────┴──────┘
print_table table, header: header,
border: :double_line
# ╔══════╦══════╦══════╗
# ║ A ║ B ║ C ║
# ╠══════╬══════╬══════╣
# ║ AAAA ║ BBBB ║ CCCC ║
# ║ AAAA ║ BBBB ║ CCCC ║
# ║ AAAA ║ BBBB ║ CCCC ║
# ╚══════╩══════╩══════╝
uneven_table = [
['AAAAAA', 'B', 'CCCCCCC'],
['AAA', 'BBBB', 'CCC' ],
['A', 'BBBBBBB', 'C' ]
]
print_table uneven_table, header: header,
justify: :left,
justify_header: :left,
border: :line
# ┌────────┬─────────┬─────────┐
# │ A │ B │ C │
# ├────────┼─────────┼─────────┤
# │ AAAAAA │ B │ CCCCCCC │
# │ AAA │ BBBB │ CCC │
# │ A │ BBBBBBB │ C │
# └────────┴─────────┴─────────┘
print_table uneven_table, header: header,
justify: :right,
justify_header: :right,
border: :line
# ┌────────┬─────────┬─────────┐
# │ A │ B │ C │
# ├────────┼─────────┼─────────┤
# │ AAAAAA │ B │ CCCCCCC │
# │ AAA │ BBBB │ CCC │
# │ A │ BBBBBBB │ C │
# └────────┴─────────┴─────────┘
print_table uneven_table, header: header,
justify: :center,
justify_header: :center,
border: :line
# ┌────────┬─────────┬─────────┐
# │ A │ B │ C │
# ├────────┼─────────┼─────────┤
# │ AAAAAA │ B │ CCCCCCC │
# │ AAA │ BBBB │ CCC │
# │ A │ BBBBBBB │ C │
# └────────┴─────────┴─────────┘
table_with_empty_cells = [
['AAAA', 'BBBB', 'CCCC'],
['AAAA', nil, 'CCCC'],
['AAAA', 'BBBB']
]
print_table table_with_empty_cells, header: header,
justify: :left,
border: :line
# ┌──────┬──────┬──────┐
# │ A │ B │ C │
# ├──────┼──────┼──────┤
# │ AAAA │ BBBB │ CCCC │
# │ AAAA │ │ CCCC │
# │ AAAA │ BBBB │ │
# └──────┴──────┴──────┘
multi_line_table = [
['AAAA', 'BBBB', "CCCC\nCC"],
['AAAA', "BBBB\nB", 'CCCC'],
["AAAA\nAA\nA", "BBBB", "CCCC"]
]
print_table multi_line_table, header: header,
justify: :left,
border: :line
# ┌──────┬──────┬──────┐
# │ A │ B │ C │
# ├──────┼──────┼──────┤
# │ AAAA │ BBBB │ CCCC │
# │ │ │ CC │
# │ AAAA │ BBBB │ CCCC │
# │ │ B │ │
# │ AAAA │ BBBB │ CCCC │
# │ AA │ │ │
# │ A │ │ │
# └──────┴──────┴──────┘
end
Defined Under Namespace
Classes: BorderStyle, CellBuilder, RowBuilder, Style, TableBuilder, TableFormatter
Instance Method Summary collapse
-
#print_table(rows, header: nil, **kwargs) ⇒ Object
Prints a table of rows.
Methods included from Indent
Instance Method Details
#print_table(rows, header: nil, **kwargs) ⇒ Object
Prints a table of rows.
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/command_kit/printing/tables.rb', line 195 def print_table(rows, header: nil, **kwargs) table = TableBuilder.new(rows, header: header) style = Style.new(**kwargs) formatter = TableFormatter.new(table,style) formatter.format do |line| puts line end return nil end |