{"id":13411952,"url":"https://github.com/tj/terminal-table","last_synced_at":"2025-05-14T21:00:20.215Z","repository":{"id":477592,"uuid":"102989","full_name":"tj/terminal-table","owner":"tj","description":"Ruby ASCII Table Generator, simple and feature rich.","archived":false,"fork":false,"pushed_at":"2025-01-27T21:53:18.000Z","size":294,"stargazers_count":1555,"open_issues_count":1,"forks_count":125,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-05-07T20:28:51.085Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/tj.png","metadata":{"files":{"readme":"README.md","changelog":"History.rdoc","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"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}},"created_at":"2009-01-07T23:43:22.000Z","updated_at":"2025-04-29T13:37:15.000Z","dependencies_parsed_at":"2024-06-18T10:56:43.940Z","dependency_job_id":"8a0e75e1-4247-4348-92ea-74b69ea3796b","html_url":"https://github.com/tj/terminal-table","commit_stats":{"total_commits":227,"total_committers":42,"mean_commits":5.404761904761905,"dds":0.6740088105726872,"last_synced_commit":"5a834ab3011b78d9d42c95c382000bb10826af59"},"previous_names":["visionmedia/terminal-table"],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fterminal-table","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fterminal-table/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fterminal-table/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tj%2Fterminal-table/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tj","download_url":"https://codeload.github.com/tj/terminal-table/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253411524,"owners_count":21904147,"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":[],"created_at":"2024-07-30T20:01:19.137Z","updated_at":"2025-05-14T21:00:20.196Z","avatar_url":"https://github.com/tj.png","language":"Ruby","readme":"[![CI status](https://github.com/tj/terminal-table/workflows/CI/badge.svg)](https://github.com/tj/terminal-table/actions)\n[![Gem Version](https://badge.fury.io/rb/terminal-table.svg)](https://badge.fury.io/rb/terminal-table)\n\n# Terminal Table\n\n## Description\n\nTerminal Table is a fast and simple, yet feature rich table generator\nwritten in Ruby.  It supports ASCII and Unicode formatted tables.\n\n## Installation\n\n```\n$ gem install terminal-table\n```\n## Usage\n\n### Basics\n\nTo use Terminal Table:\n\n```ruby\nrequire 'terminal-table'\n```\nTo generate a table, provide an array of arrays (which are interpreted as\nrows):\n\n```ruby\nrows = []\nrows \u003c\u003c ['One', 1]\nrows \u003c\u003c ['Two', 2]\nrows \u003c\u003c ['Three', 3]\ntable = Terminal::Table.new :rows =\u003e rows\n\n# \u003e puts table\n#\n# +-------+---+\n# | One   | 1 |\n# | Two   | 2 |\n# | Three | 3 |\n# +-------+---+\n```\nThe constructor can also be given a block which is either yielded the Table\nobject or instance evaluated:\n\n```ruby\ntable = Terminal::Table.new do |t|\n  t.rows = rows\nend\n\ntable = Terminal::Table.new do\n  self.rows = rows\nend\n```\nAdding rows one by one:\n\n```ruby\ntable = Terminal::Table.new do |t|\n  t \u003c\u003c ['One', 1]\n  t.add_row ['Two', 2]\nend\n```\nTo add separators between rows:\n\n```ruby\ntable = Terminal::Table.new do |t|\n  t \u003c\u003c ['One', 1]          # Using \u003c\u003c (push) as an alias for add_row\n  t \u003c\u003c :separator          # Using \u003c\u003c with :separator as an alias for add_separator\n  t.add_row ['Two', 2]\n  t.add_separator          # Note - this version allows setting the separator's border_type\n  t.add_row ['Three', 3]\nend\n\n# \u003e puts table\n#\n# +-------+---+\n# | One   | 1 |\n# +-------+---+\n# | Two   | 2 |\n# +-------+---+\n# | Three | 3 |\n# +-------+---+\n```\nCells can handle multiline content:\n\n```ruby\ntable = Terminal::Table.new do |t|\n  t \u003c\u003c ['One', 1]\n  t \u003c\u003c :separator\n  t.add_row [\"Two\\nDouble\", 2]\n  t.add_separator\n  t.add_row ['Three', 3]\nend\n\n# \u003e puts table\n#\n# +--------+---+\n# | One    | 1 |\n# +--------+---+\n# | Two    | 2 |\n# | Double |   |\n# +--------+---+\n# | Three  | 3 |\n# +--------+---+\n```\n### Head\n\nTo add a head to the table:\n\n```ruby\ntable = Terminal::Table.new :headings =\u003e ['Word', 'Number'], :rows =\u003e rows\n\n# \u003e puts table\n#\n# +-------+--------+\n# | Word  | Number |\n# +-------+--------+\n# | One   | 1      |\n# | Two   | 2      |\n# | Three | 3      |\n# +-------+--------+\n```\n### Title\n\nTo add a title to the table:\n\n```ruby\ntable = Terminal::Table.new :title =\u003e \"Cheatsheet\", :headings =\u003e ['Word', 'Number'], :rows =\u003e rows\n\n# \u003e puts table\n#\n# +---------------------+\n# |     Cheatsheet      |\n# +------------+--------+\n# | Word       | Number |\n# +------------+--------+\n# | One        | 1      |\n# | Two        | 2      |\n# | Three      | 3      |\n# +------------+--------+\n```\n### Alignment\n\nTo align the second column to the right:\n\n```ruby\ntable.align_column(1, :right)\n\n# \u003e puts table\n#\n# +-------+--------+\n# | Word  | Number |\n# +-------+--------+\n# | One   |      1 |\n# | Two   |      2 |\n# | Three |      3 |\n# +-------+--------+\n```\nTo align an individual cell, you specify the cell value in a hash along the\nalignment:\n\n```ruby\ntable \u003c\u003c [\"Four\", {:value =\u003e 4.0, :alignment =\u003e :center}]\n\n# \u003e puts table\n#\n# +-------+--------+\n# | Word  | Number |\n# +-------+--------+\n# | One   |      1 |\n# | Two   |      2 |\n# | Three |      3 |\n# | Four  |  4.0   |\n# +-------+--------+\n```\n### Style\n\nTo specify style options:\n\n```ruby\ntable = Terminal::Table.new :headings =\u003e ['Word', 'Number'], :rows =\u003e rows, :style =\u003e {:width =\u003e 80}\n\n# \u003e puts table\n#\n# +--------------------------------------+---------------------------------------+\n# | Word                                 | Number                                |\n# +--------------------------------------+---------------------------------------+\n# | One                                  | 1                                     |\n# | Two                                  | 2                                     |\n# | Three                                | 3                                     |\n# +--------------------------------------+---------------------------------------+\n```\nAnd change styles on the fly:\n\n```ruby\ntable.style = {:width =\u003e 40, :padding_left =\u003e 3, :border_x =\u003e \"=\", :border_i =\u003e \"x\"}\n\n# \u003e puts table\n#\n# x======================================x\n# |               Cheatsheet             |\n# x====================x=================x\n# |   Word             |   Number        |\n# x====================x=================x\n# |   One              |   1             |\n# |   Two              |   2             |\n# |   Three            |   3             |\n# x====================x=================x\n```\nYou can also use styles to add a separator after every row:\n\n```ruby\ntable = Terminal::Table.new do |t|\n  t.add_row [1, 'One']\n  t.add_row [2, 'Two']\n  t.add_row [3, 'Three']\n  t.style = {:all_separators =\u003e true}\nend\n\n# \u003e puts table\n#\n# +---+-------+\n# | 1 | One   |\n# +---+-------+\n# | 2 | Two   |\n# +---+-------+\n# | 3 | Three |\n# +---+-------+\n```\nYou can also use styles to disable top and bottom borders of the table.\n\n```ruby\ntable = Terminal::Table.new do |t|\n  t.headings = ['id', 'name']\n  t.rows = [[1, 'One'], [2, 'Two'], [3, 'Three']]\n  t.style = { :border_top =\u003e false, :border_bottom =\u003e false }\nend\n\n# \u003e puts table\n# | id | name  |\n# +----+-------+\n# | 1  | One   |\n# | 2  | Two   |\n# | 3  | Three |\n```\n\nAnd also to disable left and right borders of the table.\n\n```ruby\ntable = Terminal::Table.new do |t|\n  t.headings = ['id', 'name']\n  t.rows = [[1, 'One'], [2, 'Two'], [3, 'Three']]\n  t.style = { :border_left =\u003e false, :border_right =\u003e false }\nend\n\n# \u003e puts table\n# ----+-------\n#  id | name\n# ----+-------\n#  1  | One\n#  2  | Two\n#  3  | Three\n# ----+-------\n```\n\nTo change the default style options:\n\n```ruby\nTerminal::Table::Style.defaults = {:width =\u003e 80}\n```\nAll Table objects created afterwards will inherit these defaults.\n\n### Constructor options and setter methods\n\nValid options for the constructor are `:rows`, `:headings`, `:style` and `:title` -\nand all options can also be set on the created table object by their setter\nmethod:\n\n```ruby\ntable = Terminal::Table.new\ntable.title = \"Cheatsheet\"\ntable.headings = ['Word', 'Number']\ntable.rows = rows\ntable.style = {:width =\u003e 40}\n```\n\n## New Formatting\n\n### Unicode Table Borders\nSupport for Unicode 'box art' borders presented a challenge, as the original terminal-table only handled three border types:  horizontal (x), vertical (y), and intersection (i).  For proper box-art, it became necessary to enable different types of corners/edges for multiple intersection types.\n\nFor the sake of backward compatiblity, the previous interface is still supported, as this gem has been around a long time and making breaking changes would have been inconvenient.  The new interface is required for any complex and/or Unicode style bordering. A few variations on border style are supported via some new classes and creation of additional classes (or modification of characters used in existing ones) will allow for customized border types.\n\nThe simplest way to use an alternate border is one of the following:\n```\ntable.style = { :border =\u003e :unicode }\ntable.style = { :border =\u003e :unicode_round }\ntable.style = { :border =\u003e :unicode_thick_edge }\n```\n\nThese are a convenience wrapper around setting border using an instance of a class that inherits from Table::Terminal::Border\n```\ntable.style = { :border =\u003e Terminal::Table::UnicodeBorder.new() }\ntable.style = { :border =\u003e Terminal::Table::UnicodeRoundBorder.new() }\ntable.style = { :border =\u003e Terminal::Table::UnicodeThickEdgeBorder.new() }\n```\n\nIf you define a custom class and wish to use the symbol shortcut, you must namespace within `Terminal::Table` and end your class name with `Border`.\n\n### Markdown Compatiblity\nPer popular request, Markdown formatted tables can be generated by using the following border style:\n\n```\ntable.style = { :border =\u003e :markdown }\n```\n\n### Ascii Borders\nAscii borders are default, but can be explicitly set with:\n```\ntable.style = { :border =\u003e :ascii }\n```\n\n### Customizing Borders\nInside the `UnicodeBorder` class, there are definitions for a variety of corner/intersection and divider types.\n\n```ruby\n@data = {\n  nil =\u003e nil,\n  nw: \"┌\", nx: \"─\", n:  \"┬\", ne: \"┐\",\n  yw: \"│\",          y:  \"│\", ye: \"│\", \n  aw: \"╞\", ax: \"═\", ai: \"╪\", ae: \"╡\", ad: '╤', au: \"╧\", # double\n  bw: \"┝\", bx: \"━\", bi: \"┿\", be: \"┥\", bd: '┯', bu: \"┷\", # heavy/bold/thick\n  w:  \"├\", x:  \"─\", i:  \"┼\", e:  \"┤\", dn: \"┬\", up: \"┴\", # normal div\n  sw: \"└\", sx: \"─\", s:  \"┴\", se: \"┘\",\n  # alternative dots/dashes\n  x_dot4:  '┈', x_dot3:  '┄', x_dash:  '╌',\n  bx_dot4: '┉', bx_dot3: '┅', bx_dash: '╍',\n}\n```\n\nNote that many are defined as directional (:nw == north-west), others defined in terms of 'x' or 'y'.\nThe border that separates headings (below each heading) is of type `:double` and is defined with `a*` entries.\nAlternate `:heavy` types that can be applied to separators can be defined with `b*` entries.\n\nWhen defining a new set of borders, it's probably easiest to define a new class that inherits from UnicodeBorder and replaces the `@data` Hash.\nHowever, these elements can be these can be overridden by poking setting the Hash, should the need arise:\n\n```\ntable.style = {border: :unicode}\ntable.style.border[:nw] = '*'  # Override the north-west corner of the table\n```\n\n### Customizing row separators\n\nRow-separators can now be customized in a variety of ways.  The default separator's border_type is referred to as `:div`.  Additional separator border types (e.g. `:double`, `:heavy`, `:dash` - see full list below) can be applied to separate the sections (e.g. header/footer/title).\n\nThe separator's `border_type`  may be specified when a user-defined separator is added.  Alternatively, borders may be adjusted after the table's rows are elaborated, but before the table is rendered.\n\nSeparator `border_type`s can be adjusted to be heavy, use double-lines, and different dash/dot styles.  The border type should be one of:\n\n    div dash dot3 dot4 \n    thick thick_dash thick_dot3 thick_dot4\n    heavy heavy_dash heavy_dot3 heavy_dot4\n    bold bold_dash bold_dot3 bold_dot4\n    double\n\nTo manually set the separator border_type, the `add_separator` method may be called.\n```ruby\nadd_separator(border_type: :heavy_dash)\n```\n\nAlternatively, if `style: :all_separators` is used at the table level, it may be necessary to elaborate the implicit Separator rows prior to rendering.\n```ruby\ntable = Terminal::Table.new do |t|\n  t.add_row [1, 'One']\n  t.add_row [2, 'Two']\n  t.add_row [3, 'Three']\n  t.style = {:all_separators =\u003e true}\nend\nrows = table.elaborate_rows\nrows[2].border_type = :heavy # modify separator row: emphasize below title\nputs table.render\n```\n\n## Example: Displaying a small CSV spreadsheet\n\nThis example code demonstrates using Terminal-table and CSV to display a small spreadsheet.\n\n```ruby\n#!/usr/bin/env ruby\nrequire \"csv\"\nrequire \"terminal-table\"\nuse_stdin = ARGV[0].nil? || (ARGV[0] == '-')\nio_object = use_stdin ? $stdin : File.open(ARGV[0], 'r')\ncsv = CSV.new(io_object)\ncsv_array = csv.to_a\nuser_table = Terminal::Table.new do |v|\n  v.style = { :border =\u003e :unicode_round } # \u003e= v3.0.0\n  v.title = \"Some Title\"\n  v.headings = csv_array[0]\n  v.rows = csv_array[1..-1]\nend\nputs user_table\n```\n\nSee also `examples/show_csv_table.rb` in the source distribution.\n\n## More examples\n\nFor more examples, please see the `examples` directory included in the\nsource distribution.\n\n## Author\n\nTJ Holowaychuk \u003ctj@vision-media.ca\u003e\n\nUnicode table support by Ben Bowers https://github.com/nanobowers\n","funding_links":[],"categories":["Ruby","Happy Exploring 🤘","CLI Utilities","Command Line","命令行"],"sub_categories":["高级控制台界面","高級控制台界面"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftj%2Fterminal-table","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftj%2Fterminal-table","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftj%2Fterminal-table/lists"}