class Table

Defined in:

crystal_fmt/table.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(data : Array(Array(String | Nil)) = Array(Array(String | Nil)).new) #

Can be initialized with an of cells: Array(Array(String | Nil)) or nothing and you can add rows of data later with add_rows Note: all rows of data must be the same length.


[View source]
def self.new(columns : Array(Column)) #

Allows you to initialize this with an array of columns. Note: when this initializer is used you will not be able to use the add_rows method


[View source]

Instance Method Detail

def add_row(row : Array(String | Nil)) #

Adds a row of data. Note: this must have the same number of items as all the other rows that have been added.


[View source]
def data : Array(Array(String?)) #

[View source]
def data=(data : Array(Array(Nil | String))) #

[View source]
def extract_columns(data : Array(Array(String | Nil))) : Array(Column) #

Extracts Column objects from the provided data primarily for use use by this class, but you may find it useful too.


[View source]
def format(options : Hash(Symbol, String | Bool) = Hash(Symbol, String | Bool).new) : String #

Formats the data into a textual table. By default it will:

  • start each row with "| "
  • end each row with " |"
  • separate each item with " | " These defaults can be overridden by specifying :left_border, :right_border, or :divider (respectively) in the options.

By default it assumes the first row is a header. To disable the divider under the first row set options[:show_header] = false To change the character used for the divider set options[:header_divider] =

Example: [["things", "stuff"],["a", "b"], ["c", nil]] Would become | things | stuff | | ------ | ----- | | a | b | | c | |


[View source]
def width : Int32 #

Tells you how wide the table is


[View source]