Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

tabulate

> 数据库
Open source

Table Maker for Modern C++

2.2K stars0 likes1 views
WebsiteGitHub

About

Table Maker for Modern C++

Source for the above image can be found here

Table of Contents

  • Quick Start
  • Formatting Options
    • Style Inheritance Model
    • Word Wrapping
    • Font Alignment
    • Font Styles
    • Cell Colors
    • Borders and Corners
    • Range-based Iteration
    • Nested Tables
    • UTF-8 Support
  • Exporters
    • Markdown
    • AsciiDoc
  • Building Samples
  • Generating Single Header
  • Contributing
  • License

Quick Start

tabulate is a header-only library. Just add include/ to your include_directories and you should be good to go. A single header file version is also available in single_include/.

NOTE Tabulate supports >=C++11. The rest of this README, however, assumes C++17 support.

Create a Table object and call Table.add_rows to add rows to your table.

…

Use RowStream to format table data with stream insertion.

cpp
  employees.add_row({"Emp. ID", "First Name", "Last Name", "Department / Business Unit", "Pay Rate"});
  employees.add_row(RowStream{} << 101 << "Donald" << "Patrick" << "Finance" << 59.6154);
  employees.add_row(RowStream{} << 102 << "Rachel" << "Williams" << "Marketing and Operational\nLogistics Planning" << 34.9707);
  employees.add_row(RowStream{} << 103 << "Ian" << "Jacob" << department << 57.0048);

You can format this table using Table.format() which returns a Format object. Using a fluent interface, format properties of the table, e.g., borders, font styles, colors etc.

cpp
  universal_constants.format()
    .font_style({FontStyle::bold})
    .border_top(" ")
    .border_bottom(" ")
    .border_left(" ")
    .border_right(" ")
    .corner(" ");

You can access rows in the table using Table[row_index]. This will return a Row object on which you can similarly call Row.format() to format properties of all the cells in that row.

Now, let's format the header of the table. The following code changes the font background of the header row to red, aligns the cell contents to center and applies a padding to the top and bottom of the row.

cpp
  universal_constants[0].format()
    .padding_top(1)
    .padding_bottom(1)
    .font_align(FontAlign::center)
    .font_style({FontStyle::underline})
    .font_background_color(Color::red);

Calling Table.column(index) will return a Column object. Similar to rows, you can use Column.format() to format all the cells in that column.

Now, let's change the font color of the second column to yellow:

cpp
  universal_constants.column(1).format()
    .font_color(Color::yellow);

You can access cells by indexing twice from a table: From a row using Table[row_index][col_index] or from a column using Table.column(col_index)[cell_index]. Just like rows, columns, and tables, you can use Cell.format() to format individual cells

cpp
  universal_constants[0][1].format()
    .font_background_color(Color::blue)
    .font_color(Color::white);
}

Print the table using the stream operator<< like so:

cpp
  std::cout << universal_constants << std::endl;

You could also use Table.print(stream) to print the table, e.g., universal_constants.print(std::cout).

Formatting Options

Style Inheritance Model

Formatting in tabulate follows a simple style-inheritance model. When rendering each cell:

  1. Apply cell formatting if specified
  2. If no cell formatting is specified, apply its parent row formatting
  3. If no row formatting is specified, apply its parent table formatting
  4. If no table formatting is specified, apply the default table formatting

This enables overriding the formatting for a particular cell even though row or table formatting is specified, e.g., when an entire row is colored yellow but you want a specific cell to be colored red.

Word Wrapping

tabulate supports automatic word-wrapping when printing cells.

Although word-wrapping is automatic, there is a simple override. Automatic word-wrapping is used only if the cell contents do not have any embedded newline \n characters. So, you can embed newline characters in the cell contents and enforce the word-wrapping manually.

cpp
#include <tabulate/table.hpp>
using namespace tabulate;

int main() {
  Table table;

  table.add_row({"This paragraph contains a veryveryveryveryveryverylong word. The long word will "
                 "break and word wrap to the next line.",
                 "This paragraph \nhas embedded '\\n' \ncharacters and\n will break\n exactly "
                 "where\n you want it\n to\n break."});

  table[0][0].format().width(20);
  table[0][1].format().width(50);

  std::cout << table << std::endl;
}
  • The above table has 1 row and 2 columns.
  • The first cell has automatic word-wrapping.
  • The second cell uses the embedded newline characters in the cell contents - even though the second column has plenty of space (50 characters width), it uses user-provided newline characters to break into new lines and enforce the cell style.
  • NOTE: Whether word-wrapping is automatic or not, tabulate performs a trim operation on each line of each cell to remove whitespace characters from either side of line.

NOTE: Both columns in the above table are left-aligned by default. This, however, can be easily changed.

Font Alignment

tabulate supports three font alignment settings: left, center, and right. By default, all table content is left-aligned. To align cells, use .format().font_align(alignment).

…

Font Styles

tabulate supports 8 font styles: bold, dark, italic, underline, blink, reverse, concealed, crossed. Depending on the terminal (or terminal settings), some of these might not work.

To apply a font style, simply call .format().font_style({...}). The font_style method takes a vector of font styles. This allows to apply multiple font styles to a cell, e.g., bold and italic.

…

NOTE: Font styles are applied to the entire cell. Unlike HTML, you cannot currently apply styles to specific words in a cell.

Cell Colors

There are a number of methods in the Format object to color cells - foreground and background - for font, borders, corners, and column separators. Thanks to termcolor, tabulate supports 8 colors: grey, red, green, yellow, blue, magenta, cyan, and white. The look of these colors vary depending on your terminal.

For font, border, and corners, you can call .format().<element>_color(value) to set the foreground color and .format().<element>_background_color(value) to set the background color. Here's an example:

…

Borders and Corners

tabulate allows for fine control over borders and corners. For each border and corner, you can set the text, color, and background color.

NOTE: You can use .corner(..), .corner_color(..), and .corner_background_color(..) to set a common style for all corners. Similarly, you can use .border(..), .border_color(..) and .border_background_color(..) to set a common style for all borders.

NOTE: Note the use of .format().multi_byte_characters(true). Use this when you know your table has multi-byte characters. This is an opt-in because the calculation of column width when dealing with multi-byte characters is more involved and you don't want to pay the performance penalty unless you need it. Just like any other format setting, you can set this at the table-level, row-level, or on a per-cell basis.

Here's an example where each border and corner is individually styled:

…

Range-based Iteration

Hand-picking and formatting cells using operator[] gets tedious very quickly. To ease this, tabulate supports range-based iteration on tables, rows, and columns. Quickly iterate over rows and columns to format cells.

…

Nested Tables

Table.add_row(...) takes either a std::string or a tabulate::Table. This can be used to nest tables within tables. Here's an example program that prints a UML class diagram using tabulate. Note the use of font alignment, style, and width settings to generate a diagram that looks centered and great.

…

UTF-8 Support

In *nix, wcswidth is used to compute the display width of multi-byte characters. Column alignment works well when your system supports the necessary locale, e.g., I've noticed on MacOS 10 there is no Arabic locale (searched with locale -a) and this ends up causing alignment issues when using Arabic text, e.g., "ٲنَا بحِبَّك (Ana bahebak)" in tables.

The following table prints the phrase I love you in different languages. Note the use of .format().multi_byte_characters(true) for the second column. Remember to do this when dealing with multi-byte characters.

…

You can explicitly set the locale for a cell using .format().locale(value). Note that the locale string is system-specific. So, the following code might throw std::runtime_error locale::facet::_S_create_c_locale name not valid on your system.

…

Exporters

Markdown

Tables can be exported to GitHub-flavored markdown using a MarkdownExporter. Simply create an exporter object and call exporter.dump(table) to generate a Markdown-formatted std::string.

…

The above table renders in Markdown like below.

NOTE: Unlike tabulate, you cannot align individual cells in Markdown. Alignment is on a per-column basis. Markdown allows a second header row where such column-wise alignment can be specified. The MarkdownExporter uses the formatting of the header cells in the original tabulate::Table to decide how to align each column. As per the Markdown spec, columns are left-aligned by default.

S/N Movie Name Director Estimated Budget Release Date
tt1979376 Toy Story 4 Josh Cooley $200,000,000 21 June 2019
tt3263904 Sully Clint Eastwood $60,000,000 9 September 2016
tt1535109 Captain Phillips Paul Greengrass $55,000,000 11 October 2013

AsciiDoc

Tabulate can export tables as AsciiDoc using an AsciiDocExporter.

…

Below is the export of the example above:

[cols="<,<,^,>,>"]
|===
|*S/N*|*Movie Name*|*Director*|*Estimated Budget*|*Release Date*

|tt1979376|Toy Story 4|*_Josh Cooley_*|$200,000,000|21 June 2019
|tt3263904|_Sully_|Clint Eastwood|$60,000,000|9 September 2016
|tt1535109|Captain Phillips|Paul Greengrass|$55,000,000| 11 October 2013
|===

The rendered output you can see here: http://tpcg.io/pbbfU3ks

NOTE Alignment is only supported per column. The font styles FontStyle::bold and FontStyle::italic can be used for each cell, also in combination.

Building Samples

There are

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++alignedalignmentborderscli

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库