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

Since:

  • 0.4.0

Defined Under Namespace

Classes: BorderStyle, CellBuilder, RowBuilder, Style, TableBuilder, TableFormatter

Instance Method Summary collapse

Methods included from Indent

#indent, #initialize, #puts

Instance Method Details

Prints a table of rows.

Parameters:

  • rows (Array<Array>)

    The table rows.

  • header (Array, nil) (defaults to: nil)

    The optional header row.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

  • [Integer] (Hash)

    a customizable set of options

  • [:left, (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

Options Hash (**kwargs):

  • :border (:line, :double_line, nil, Hash{Symbol => String}, :ascii)

    The border style or a custom Hash of border characters.

Since:

  • 0.4.0



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