Install terminal-table via gem
masterInstall the terminal-table gem using the standard Ruby gem command:
$ gem install terminal-tablerepository·master·Indexed 23 days ago
https://github.com/tj/terminal-tableA fast, feature-rich Ruby library for generating ASCII and Unicode formatted tables for terminal output. It supports customizable borders (including Markdown and Unicode presets), column alignment, cell spanning, and flexible row management via the Terminal::Table, Row, and Cell classes.
Install the terminal-table gem using the standard Ruby gem command:
$ gem install terminal-tableTo generate a table, require the library and provide an array of arrays to the Terminal::Table.new constructor. Each inner array represents a row in the table.
require 'terminal-table'
rows = []
rows << ['One', 1]
rows << ['Two', 2]
rows << ['Three', 3]
table = Terminal::Table.new :rows => rows
puts table:style option allows you to customize the table appearance. You can set properties like :width, :padding_left, and specific border characters. You can also enable/disable specific borders (top, bottom, left, right) or use the :all_separators => true option to add separators after every row.When setting the :border option in a style, you can pass a symbol. The library converts the symbol into a corresponding Border class. For example, :unicode_round will attempt to instantiate Terminal::Table::UnicodeRoundBorder.
Supported border classes include:
AsciiBorder (default)MarkdownBorder (uses | for all intersections)UnicodeBorder (standard Unicode characters)UnicodeRoundBorder (rounded corners)UnicodeThickEdgeBorder (thick outer edges)Use the :headings option to define the top row of the table and the :title option to add a centered title above the table.
# Using constructor options
table = Terminal::Table.new :title => "Cheatsheet", :headings => ['Word', 'Number'], :rows => rows
# Or using setter methods
table = Terminal::Table.new
table.title = "Cheatsheet"
table.headings = ['Word', 'Number']
table.rows = rowsalign_column(index, alignment). To align a specific cell, pass a hash containing the :value and :alignment instead of a raw value when adding the row.You can add rows to a table using the << operator (an alias for add_row) or the add_row method. You can also insert separators between rows using add_separator or by pushing the :separator symbol via <<.
table = Terminal::Table.new do |t|
t << ['One', 1] # Using << (push) as an alias for add_row
t << :separator # Using << with :separator as an alias for add_separator
t.add_row ['Two', 2]
t.add_separator # Allows setting the separator's border_type
t.add_row ['Three', 3]
endTerminal Table supports several border presets via the :border key in the style hash:
:unicode (standard box art):unicode_round (rounded corners):unicode_thick_edge (thick edges):markdown (Markdown compatible format):ascii (default ASCII borders)table.style = { :border => :unicode }
table.style = { :border => :unicode_round }
table.style = { :border => :markdown }
table.style = { :border => :ascii }When adding a separator via add_separator(border_type: :type), you can specify the visual style of the separator. Available border_type values include:
div, dash, dot3, dot4thick, thick_dash, thick_dot3, thick_dot4heavy, heavy_dash, heavy_dot3, heavy_dot4bold, bold_dash, bold_dot3, bold_dot4doubleIf using style: :all_separators => true, you can manually adjust separator rows by calling table.elaborate_rows and modifying the border_type of the resulting separator objects.
# Manually setting a separator type
add_separator(border_type: :heavy_dash)
# Adjusting implicit separators after elaboration
rows = table.elaborate_rows
rows[2].border_type = :heavy
puts table.renderrender method converts the row's cells into a formatted string suitable for terminal output. It respects the vertical alignment settings (vleft, vcenter, vright) defined in the parent table's style. The output is a multi-line string where each line corresponds to a line of text within the tallest cell in the row.You can create a new table by instantiating Terminal::Table.new. You can pass an options hash to pre-configure the table, or provide a block to configure it using instance_eval (if the block has no arguments) or by yielding the table instance (if the block has arguments).
Supported keys in the options hash:
:title: A string for the table title.:headings: An array of arrays representing the heading rows.:rows: An array of arrays representing the data rows.:style: A style object (or hash) for visual configuration.render or use the to_s alias. This will automatically handle row elaboration (inserting implicit separators for titles and headings) and column width calculations.