Terminal Table

repository·master·Indexed 23 days ago

https://github.com/tj/terminal-table

A 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.

Tokens
3.1K
Snippets
9
Records
25
Agent score
81%

What's inside terminal-table

  1. Create a basic table with Terminal::Table

    master

    To 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
  2. Configure table styles and borders

    master
    The :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.
  3. Select a border style using symbols

    master

    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)
  4. Add headings and a title to a table

    master

    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 = rows
  5. Add rows and separators to a table

    master

    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]
    end
  6. Use Unicode, Markdown, or ASCII borders

    master

    Terminal 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 }
  7. Customize row separator border types

    master

    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, dot4
    • thick, thick_dash, thick_dot3, thick_dot4
    • heavy, heavy_dash, heavy_dot3, heavy_dot4
    • bold, bold_dash, bold_dot3, bold_dot4
    • double

    If 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.render
  8. Render a row to a string

    master
    The render 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.
  9. Initialize a Terminal::Table

    master

    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.