{"id":13819907,"url":"https://github.com/piotrmurach/tty-table","last_synced_at":"2025-06-12T13:09:28.418Z","repository":{"id":27033086,"uuid":"30498040","full_name":"piotrmurach/tty-table","owner":"piotrmurach","description":"A flexible and intuitive table generator","archived":false,"fork":false,"pushed_at":"2024-03-15T14:12:10.000Z","size":358,"stargazers_count":199,"open_issues_count":16,"forks_count":21,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-16T07:39:50.017Z","etag":null,"topics":["cli","ruby-gem","table","tty","tty-components"],"latest_commit_sha":null,"homepage":"https://ttytoolkit.org","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piotrmurach.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"piotrmurach"}},"created_at":"2015-02-08T16:57:02.000Z","updated_at":"2025-05-06T05:53:26.000Z","dependencies_parsed_at":"2024-05-28T19:27:47.647Z","dependency_job_id":"ac909fea-b724-4c01-b189-d68f042fc271","html_url":"https://github.com/piotrmurach/tty-table","commit_stats":{"total_commits":286,"total_committers":10,"mean_commits":28.6,"dds":0.05594405594405594,"last_synced_commit":"6cd0d003b8242cfed341fc3f8f595b1603e05d4b"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/piotrmurach/tty-table","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-table","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-table/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-table/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-table/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piotrmurach","download_url":"https://codeload.github.com/piotrmurach/tty-table/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotrmurach%2Ftty-table/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259470951,"owners_count":22862999,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","ruby-gem","table","tty","tty-components"],"created_at":"2024-08-04T08:00:54.778Z","updated_at":"2025-06-12T13:09:28.393Z","avatar_url":"https://github.com/piotrmurach.png","language":"Ruby","funding_links":["https://github.com/sponsors/piotrmurach"],"categories":["Happy Exploring 🤘"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://ttytoolkit.org\"\u003e\u003cimg width=\"130\" src=\"https://github.com/piotrmurach/tty/blob/master/images/tty.png\" alt=\"TTY Toolkit logo\" /\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n# TTY::Table [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]\n\n[![Gem Version](https://badge.fury.io/rb/tty-table.svg)][gem]\n[![Actions CI](https://github.com/piotrmurach/tty-table/workflows/CI/badge.svg?branch=master)][gh_actions_ci]\n[![Build status](https://ci.appveyor.com/api/projects/status/8fxrbawbyomqd979?svg=true)][appveyor]\n[![Code Climate](https://codeclimate.com/github/piotrmurach/tty-table/badges/gpa.svg)][codeclimate]\n[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-table/badge.svg?branch=master)][coverage]\n[![Inline docs](http://inch-ci.org/github/piotrmurach/tty-table.svg?branch=master)][inchpages]\n\n[gitter]: https://gitter.im/piotrmurach/tty\n[gem]: http://badge.fury.io/rb/tty-table\n[gh_actions_ci]: https://github.com/piotrmurach/tty-table/actions?query=workflow%3ACI\n[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-table\n[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-table\n[coverage]: https://coveralls.io/github/piotrmurach/tty-table\n[inchpages]: http://inch-ci.org/github/piotrmurach/tty-table\n\n\u003e A flexible and intuitive table formatting.\n\n**TTY::Table** provides independent table formatting component for [TTY](https://github.com/piotrmurach/tty) toolkit.\n\n## Features\n\n* Table behaves like an array with familiar API [see](#2-interface)\n* Create table once and render using custom view renderers [see](#3-rendering)\n* Rendering provides many display options [see](#33-options)\n* Easy custom border creation [see](#352-custom)\n* Supports multibyte character encodings\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"tty-table\"\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install tty-table\n\n## Contents\n\n* [1. Usage](#1-usage)\n* [2. Interface](#2-interface)\n  * [2.1 Initialization](#21-initialization)\n  * [2.2 Iteration](#22-iteration)\n  * [2.3 Access](#23-access)\n  * [2.4 Size](#24-size)\n* [3. Rendering](#3-rendering)\n  * [3.1 Render](#31-render)\n  * [3.2 Renderer](#32-renderer)\n    * [3.2.1 Basic renderer](#321-basic-renderer)\n    * [3.2.2 ASCII renderer](#322-ascii-renderer)\n    * [3.2.3 Unicode renderer](#323-unicode-renderer)\n  * [3.3 Options](#33-options)\n  * [3.4 Alignment](#34-alignment)\n  * [3.5 Border](#35-border)\n    * [3.5.1 Parts](#351-parts)\n    * [3.5.2 Custom](#352-custom)\n    * [3.5.3 Separator](#353-separator)\n    * [3.5.4 Style](#354-style)\n  * [3.6 Filter](#36-filter)\n  * [3.7 Multiline](#37-multiline)\n  * [3.8 Padding](#38-padding)\n  * [3.9 Resize](#39-resize)\n  * [3.10 Width](#310-width)\n\n## 1. Usage\n\nFirst, provide **TTY::Table** with data, for example, two headers and two rows:\n\n```ruby\ntable = TTY::Table.new([\"header1\",\"header2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"]])\n```\n\nThen to print to the console, call the `render` method with border type as a first argument:\n\n```ruby\nputs table.render(:ascii)\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |a1     |a2     |\n#  +-------+-------+\n#  |b1     |b2     |\n#  +-------+-------+\n```\n\n## 2. Interface\n\n### 2.1 Initialization\n\n**TTY::Table** can be created in variety of ways. The easiest way is to pass 2-dimensional array:\n\n```ruby\ntable = TTY::Table[[\"a1\", \"a2\"], [\"b1\", \"b2\"]]\ntable = TTY::Table.new([[\"a1\", \"a2\"], [\"b1\", \"b2\"]])\ntable = TTY::Table.new(rows: [[\"a1\", \"a2\"], [\"b1\", \"b2\"]])\n```\n\nAlternatively you can specify the rows one by one inside a block:\n\n```ruby\ntable = TTY::Table.new do |t|\n  t \u003c\u003c [\"a1\", \"a2\"]\n  t \u003c\u003c [\"b1\", \"b2\"]\nend\n```\n\nYou can add rows of data after initialization with `\u003c\u003c` operator:\n\n```ruby\ntable = TTY::Table.new\ntable \u003c\u003c [\"a1\",\"a2\"]\ntable \u003c\u003c [\"b1\",\"b2\"]\n```\n\nIn addition to rows, you can specify table header:\n\n```ruby\ntable = TTY::Table.new([\"h1\", \"h2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"]])\ntable = TTY::Table.new(header: [\"h1\", \"h2\"], rows: [[\"a1\", \"a2\"], [\"b1\", \"b2\"]])\n```\n\nYou can also mix header with rows inside a hash like so:\n\n```ruby\ntable = TTY::Table.new([{\"h1\" =\u003e [\"a1\", \"a2\"], \"h2\" =\u003e [\"b1\", \"b2\"]}])\n```\n\n### 2.2 Iteration\n\nTable behaves like an Array so `\u003c\u003c`, `each` and familiar methods can be used:\n\n```ruby\ntable \u003c\u003c [\"a1\", \"a2\", \"a3\"]\ntable \u003c\u003c [\"b1\", \"b2\", \"b3\"]\ntable \u003c\u003c [\"a1\", \"a2\"] \u003c\u003c [\"b1\", \"b2\"]  # chain rows assignment\n```\n\nIn order to iterate over table rows including headers do:\n\n```ruby\ntable.each { |row| ... }                       # iterate over rows\ntable.each_with_index  { |row, index| ... }    # iterate over rows with an index\n```\n\n### 2.3 Access\n\nIn order to reference the row at `index` do:\n\n```ruby\ntable = TTY::Table.new [[\"a1\",\"a2\"], [\"b1\",\"b2\"]]\ntable[0]                    # =\u003e [\"a1\",\"a2\"]\ntable.row(0)                # =\u003e [\"a1\",\"a2\"]\ntable.row(i) { |row| ... }  # return array for row(i)\n```\n\nNegative indices count backwards from the end of table data (`-1` is the last element):\n\n```ruby\ntable[-1]   # =\u003e [\"b1\",\"b2\"]\n```\n\nTo reference element at given row(i) and column(j) do:\n\n```ruby\ntable[i, j]   # return element at row(i) and column(j)\ntable[0,0]    # =\u003e \"a1\"\n```\n\nTo specifically reference column(j) do:\n\n```ruby\ntable.column(j) { ... }   # return array for column(j)\ntable.column(0)           # =\u003e [\"a1\",\"b1\"]\ntable.column(name)        # return array for column(name), name of header\n```\n\nAn `IndexError` is raised for indexes outside of data range.\n\n### 2.4 Size\n\nIn order to query the number of rows, columns or size do:\n\n```ruby\ntable.rows_size        # return row size\ntable.columns_size     # return column size\ntable.size             # return an array of [row_size, column_size]\n```\n\n### 2.5 Orientation\n\n## 3 Rendering\n\n**TTY-Table** rendering process means you can create tabular data once and then create different renderers to match your needs for formatting the data.\n\n### 3.1 Render\n\nGiven a table:\n\n```ruby\ntable = TTY::Table.new([\"header1\",\"header2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"]])\n```\n\nOnce you have an instance of `TTY::Table` you can decorate the content using the `render` method. In order to display a basic whitespace delimited view do:\n\n```ruby\ntable.render(:basic)\n# =\u003e\n#  header1 header2\n#  a1      a2\n#  b1      b2\n```\n\nThis will use so called `:basic` renderer with default options. The other renderers are `:ascii` and `:unicode`.\n\nThe `render` method can accept as a second argument the [rendering options](#33-options) either as hash value:\n\n```ruby\ntable.render(:basic, alignments: [:left, :center])\n```\n\nor inside a block:\n\n```ruby\ntable.render(:basic) do |renderer|\n  renderer.alignments= [:left, :center]\nend\n```\n\n### 3.2 Renderer\n\n**TTY::Table** has a definition of `TTY::Table::Renderer` which allows you to provide different view for your tabular data. It comes with few initial renderers built in such as `TTY::Table::Renderer::Basic`, `TTY::Table::Renderer::ASCII` and `TTY::Table::Renderer:Unicode`.\n\nGiven a table of data:\n\n```ruby\ntable = TTY::Table.new [\"header1\",\"header2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"]]\n```\n\nYou can create a special renderer for it:\n\n```ruby\nmulti_renderer = TTY::Table::Renderer::Basic.new(table, multiline: true)\n```\n\nand then call `render`\n\n```ruby\nmulti_renderer.render\n```\n\nThis way, you create tabular data once and then create different renderers to match your needs for formatting the data.\n\n#### 3.2.1 Basic Renderer\n\nThe basic render allows for formatting table with whitespace without any border:\n\n```ruby\nrenderer = TTY::Table::Renderer::Basic.new(table)\n```\n\n```ruby\nrenderer.render\n# =\u003e\n#  header1 header2\n#  a1      a2\n#  b1      b2\n```\n\nThis is the same as calling `render` directly on table:\n\n```ruby\ntable.render\n```\n\n#### 3.2.2 ASCII Renderer\n\nThe ascii renderer allows for formatting table with ASCII type border.\n\nCreate an instance of ASCII renderer:\n\n```ruby\nrenderer = TTY::Table::Renderer::ASCII.new(table)\n```\n\nand then call `render` to get the formatted data:\n\n```ruby\nrenderer.render\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |a1     |a2     |\n#  |b1     |b2     |\n#  +-------+-------+\n```\n\nThis is the same as calling `render` directly on table instance with `:ascii` as the first argument:\n\n```ruby\ntable.render(:ascii)\n```\n\n#### 3.2.3 Unicode Renderer\n\nThe uniocde renderer allows for formatting table with Unicode type border.\n\nCreate an instance of Unicode renderer:\n\n```ruby\nrenderer = TTY::Table::Renderer::Unicode.new(table)\n```\n\nand then call `render` to get the formatted data:\n\n```ruby\nrenderer.render\n# =\u003e\n#  ┌───────┬───────┐\n#  │header1│header2│\n#  ├───────┼───────┤\n#  │a1     │a2     │\n#  │b1     │b2     │\n#  └───────┴───────┘\n```\n\nThis is the same as calling `render` directly on table instance with `:unicode` as the first argument:\n\n```ruby\ntable.render(:unicode)\n```\n\n### 3.3 Options\n\nRendering of **TTY-Table** includes numerous customization options:\n\n* `:alignments` - array of cell alignments out of `:left`, `:center` and `:right`. Defaults to `:left`.\n* `:border` - hash of border options out of `:characters`, `:style` and `:separator`\n* `:border_class` - a type of border to use such as `TTY::Table::Border::Null`, `TTY::Table::Border::ASCII` and `TTY::Table::Border::Unicode`\n* `:column_widths` - array of maximum column widths\n* `:filter` - a `proc` object that is applied to every field in a row\n* `:indent` - indentation applied to rendered table, by default 0\n* `:multiline` - when `true` will wrap text at new line or column width, when `false` will escape special characters\n* `:padding` - array of integers to set table fields padding. Defaults to `[0,0,0,0]`.\n* `:resize` - when `true` will expand/shrink table column sizes to match the terminal width, otherwise when `false` will rotate table vertically. Defaults to `false`.\n* `:width` - constrains the table total width. Defaults to value automatically calculated based on the content and terminal size.\n\nThe `render` method can accept as a second argument the above options either as hash value:\n\n```ruby\ntable.render(:basic, alignments: [:left, :center])\n```\n\nOr inside a block as a property:\n\n```ruby\ntable.render(:basic) do |renderer|\n  renderer.alignments = [:left, :center]\nend\n```\n\n### 3.4 Alignment\n\nBy default all columns are `:left` aligned.\n\nYou can align each column individually by passing `:alignments` option to table renderer:\n\n```ruby\ntable.render(:ascii, alignments: [:center, :right])\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |  a1   |     a2|\n#  |  b1   |     b2|\n#  +-------+-------+\n```\n\nAlternatively you can align all columns with `:alignment` option:\n\n```ruby\ntable.render(:ascii, alignment: [:center])\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |  a1   |  a2   |\n#  |  b1   |  b2   |\n#  +-------+-------+\n```\n\nIf you require a more granular alignment you can align individual fields in a row by passing `:alignment` option like so:\n\n```ruby\ntable = TTY::Table.new(header: [\"header1\", \"header2\"])\ntable \u003c\u003c [{value: \"a1\", alignment: :right}, \"a2\"]\ntable \u003c\u003c [\"b1\", {value: \"b2\", alignment: :center}]\n```\n\nand then simply render:\n\n```ruby\ntable.render(:ascii)\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |     a1|a2     |\n#  |b1     |  b2   |\n#  +-------+-------+\n```\n\n### 3.5 Border\n\nTo print border around data table you need to specify `renderer` type out of `basic`, `ascii`, `unicode`. By default `basic` is used. For instance, to output unicode border:\n\n```ruby\ntable = TTY::Table.new [\"header1\", \"header2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"]]\ntable.render :unicode\n# =\u003e\n#  ┌───────┬───────┐\n#  │header1│header2│\n#  ├───────┼───────┤\n#  │a1     │a2     │\n#  │b1     │b2     │\n#  └───────┴───────┘\n```\n\nor by creating unicode renderer:\n\n```ruby\nrenderer = TTY::Table::Renderer::Unicode.new(table)\nrenderer.render\n```\n\n#### 3.5.1 Parts\n\nThe following are available border parts:\n\n| Part          | ASCII | Unicode |\n| ------------- |:-----:|:-------:|\n|  top          | `-`   | `─`     |\n|  top_mid      | `+`   | `┬`     |\n|  top_left     | `+`   | `┌`     |\n|  top_right    | `+`   | `┐`     |\n|  bottom       | `-`   | `─`     |\n|  bottom_mid   | `+`   | `┴`     |\n|  bottom_left  | `+`   | `└`     |\n|  bottom_right | `+`   | `┘`     |\n|  mid          | `-`   | `─`     |\n|  mid_mid      | `+`   | `┼`     |\n|  mid_left     | `+`   | `├`     |\n|  mid_right    | `+`   | `┤`     |\n|  left         | `|`   | `│`     |\n|  center       | `|`   | `│`     |\n|  right        | `|`   | `│`     |\n\nUsing the above border parts you can create your own border with the `border` helper:\n\n```ruby\ntable = TTY::Table.new [\"header1\", \"header2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"]\ntable.render do |renderer|\n  renderer.border do\n    mid          \"=\"\n    mid_mid      \" \"\n  end\nend\n# =\u003e\n#  header1 header2\n#  ======= =======\n#  a1      a2\n#  b1      b2\n```\n\n#### 3.5.2 Custom\n\nYou can also create your own custom border by subclassing `TTY::Table::Border` and implementing the `def_border` method using internal DSL methods like so:\n\n```ruby\nclass MyBorder \u003c TTY::Table::Border\n  def_border do\n    left         \"$\"\n    center       \"$\"\n    right        \"$\"\n    bottom       \" \"\n    bottom_mid   \"*\"\n    bottom_left  \"*\"\n    bottom_right \"*\"\n  end\nend\n```\n\nNext pass the border class to your table instance `render_with` method\n\n```ruby\ntable = TTY::Table.new [\"header1\", \"header2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"]\ntable.render_with MyBorder\n# =\u003e\n#  $header1$header2$\n#  $a1     $a2     $\n#  *       *       *\n```\n\n#### 3.5.3 Separator\n\nIn addition to specifying border characters you can force the table to render a separator line on each row like:\n\n```ruby\ntable = TTY::Table.new [\"header1\", \"header2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"]]\ntable.render do |renderer|\n  renderer.border.separator = :each_row\nend\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |a1     |a2     |\n#  +-------+-------+\n#  |b1     |b2     |\n#  +-------+-------+\n```\n\nIf you want more control you can provide an array of rows *after* which a separator will be added:\n\n```ruby\ntable = TTY::Table.new [\"header1\", \"header2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"], [\"c1\", \"c2\"]]\ntable.render do |renderer|\n  renderer.border.separator = [0, 2]\nend\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |a1     |a2     |\n#  |b1     |b2     |\n#  +-------+-------+\n#  |c1     |c2     |\n#  +-------+-------+\n```\n\n**Note:** if you supply a detailed list of rows to separate, then the separator between the header and the rows\nwill not be automatically added.\n\nYou can also give the separator option a proc to control where the separators are:\n\n```ruby\ntable = TTY::Table.new [\"header1\", \"header2\"],\n                       [[\"a1\", \"a2\"], [\"b1\", \"b2\"], [\"c1\", \"c2\"], [\"d1\", \"d2\"]]\ntable.render do |renderer|\n  renderer.border.separator = -\u003e(row) { row == 0 || (row+1) % 2 == 0} # separate every two rows\nend\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |a1     |a2     |\n#  |b1     |b2     |\n#  +-------+-------+\n#  |c1     |c2     |\n#  |d1     |d2     |\n#  +-------+-------+\n```\n\nFinally you can also position a separator using the `:separator` key word in place of a row:\n\n```ruby\ntable = TTY::Table.new [\"header1\", \"header2\"],\n                       [:separator, [\"a1\", \"a2\"], [\"b1\", \"b2\"]]\ntable \u003c\u003c :separator \u003c\u003c [\"c1\", \"c2\"]  # you can push separators on too!\ntable.render\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |a1     |a2     |\n#  |b1     |b2     |\n#  +-------+-------+\n#  |c1     |c2     |\n#  +-------+-------+\n```\n\n#### 3.5.4 Style\n\nIf you want to change the display color of your border do:\n\n```ruby\ntable.render do |renderer|\n  renderer.border.style = :green\nend\n```\n\nAll [supported colors](https://github.com/piotrmurach/pastel#3-supported-colors) are provided by the **Pastel** dependency.\n\n### 3.6 Filter\n\nYou can define filters that will modify individual table field value before it is rendered. A filter can be a callable such as proc.\n\nHere's an example that formats capitalizes each field in second column skipping the header:\n\n```ruby\ntable = TTY::Table.new([\"header1\", \"header2\"], [[\"a1\", \"a2\"], [\"b1\", \"b2\"]])\ntable.render do |renderer|\n  renderer.filter = -\u003e(val, row_index, col_index) do\n    if col_index == 1 and !(row_index == 0)\n      val.capitalize\n    else\n      val\n    end\n  end\nend\n# =\u003e\n#  +-------+-------+\n#  |header1|header2|\n#  +-------+-------+\n#  |a1     |A2     |\n#  +-------+-------+\n#  |b1     |B2     |\n#  +-------+-------+\n```\n\nTo color even fields red on green background add filter like so:\n\n```ruby\npastel = Pastel.new\n\ntable.render do |renderer|\n  renderer.filter = -\u003e(val, row_index, col_index) do\n    col_index % 2 == 1 ? pastel.red.on_green(val) : val\n  end\nend\n```\n\n### 3.7 Multiline\n\nRenderer options may include `:multiline` parameter. When set to `true`, table fields will wrap at their natural line breaks or the column widths(if provided).\n\n```ruby\ntable = TTY::Table.new([[\"First\", \"1\"], [\"Multi\\nLine\\nContent\", \"2\"], [\"Third\", \"3\"]])\ntable.render(:ascii, multiline: true)\n# =\u003e\n#  +-------+-+\n#  |First  |1|\n#  |Multi  |2|\n#  |Line   | |\n#  |Content| |\n#  |Third  |3|\n#  +-------+-+\n```\n\nWhen `multiline` is set to `false`, all line break characters will be escaped. In cases when the column widths are set, the content will be truncated.\n\n```ruby\ntable = TTY::Table.new [[\"First\", \"1\"], [\"Multiline\\nContent\", \"2\"], [\"Third\", \"3\"]]\ntable.render :ascii, multiline: false\n# =\u003e\n#  +------------------+-+\n#  |First             |1|\n#  |Multiline\\nContent|2|\n#  |Third             |3|\n#  +------------------+-+\n```\n\n### 3.8 Padding\n\nRenderer also accepts `padding` option which accepts array with arguments similar to CSS padding.\n\n```ruby\n[2,2,2,2]  # =\u003e pad left and right with 2 characters, add 2 lines above and below\n[1,2]      # =\u003e pad left and right with 2 characters, add 1 line above and below\n1          # =\u003e pad left and right with 1 character, and 1 lines above and below\n```\n\nTherefore, to apply padding to the example table do:\n\n```ruby\ntable.render(:ascii, padding: [1,2,1,2])\n# =\u003e\n#  +---------+---------+\n#  |         |         |\n#  | header1 | header2 |\n#  |         |         |\n#  +---------+---------+\n#  |         |         |\n#  | a1      | a2      |\n#  |         |         |\n#  |         |         |\n#  | b1      | b2      |\n#  |         |         |\n#  +---------+---------+\n```\n\nHowever, when adding top or bottom padding to content with line breaks, the `multiline` option needs to be set to `true` to allow for rows to span multiple lines. For example:\n\n```ruby\ntable = TTY::Table.new(header: [\"head1\", \"head2\"])\ntable \u003c\u003c [\"Multi\\nLine\", \"Text\\nthat\\nwraps\"]\ntable \u003c\u003c [\"Some\\nother\\ntext\", \"Simple\"]\n```\n\nThis would render as:\n\n```ruby\ntable.render(:ascii, multiline: true, padding: [1,2,1,2])\n# =\u003e\n#  +---------+----------+\n#  |         |          |\n#  |  h1     |  head2   |\n#  |         |          |\n#  +---------+----------+\n#  |         |          |\n#  |  Multi  |  Text    |\n#  |  Line   |  that    |\n#  |         |  wraps   |\n#  |         |          |\n#  |         |          |\n#  |  Some   |  Simple  |\n#  |  other  |          |\n#  |  text   |          |\n#  |         |          |\n#  +---------+----------+\n```\n\n### 3.9 Resize\n\nYou can force table to resize to the terminal full width using the `:resize` option:\n\n```ruby\ntable.render(:ascii, resize: true)\n```\n\n### 3.10 Width\n\nTo control table's column sizes pass `width`, `resize` options. By default table's natural column widths are calculated from the content. If the total table width does not fit in terminal window then the table is rotated vertically to preserve content.\n\nThe `resize` property will force the table to expand/shrink to match the terminal width or custom `width`. On its own the `width` property will not resize table but only enforce table vertical rotation if content overspills.\n\nFor example, given the following table:\n\n```ruby\nheader = [\"h1\", \"h2\", \"h3\"]\nrows   = [[\"aaa1\", \"aa2\", \"aaaaaaa3\"], [\"b1\", \"b2\", \"b3\"]]\ntable = TTY::Table.new(header, rows)\n```\n\nThe result of rending to `80` columns width will produce:\n\n```ruby\ntable.render(width: 80, resize: true)\n# =\u003e\n#  +---------+-------+------------+\n#  |h1       |h2     |h3          |\n#  +---------+-------+------------+\n#  |aaa1     |aa2    |aaaaaaa3    |\n#  |b1       |b2     |b3          |\n#  +---------+-------+------------+\n```\n\n## Contributing\n\n1. Fork it ( https://github.com/piotrmurach/tty-table/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\nThis project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## Copyright\n\nCopyright (c) 2015 Piotr Murach. See LICENSE for further details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Ftty-table","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiotrmurach%2Ftty-table","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotrmurach%2Ftty-table/lists"}