Easily Create Tables in Terminal with C++

A C++ library that facilitates the development of your TUI applications.


Easily Create Tables in Terminal with C++


TUI (Terminal User Interface) applications are growing day by day. And for you to display table data in the terminal with colors and other features, for C++ developers there is one more library that is very simple to use: Tabulate .

Installation

To use Tabulate in your applications, just install it on your system and for that you will need the following dependencies:

Usually C++ programmers already have them all installed, but just in case… 😃

After that, just clone and install with the following commands:

git clone https://github.com/p-ranav/tabulate
cd tabulate
cmake .
sudo make install


Usage

There are several ways to apply Tabulate in your code, however, the most basic way would be:

  • Include library: #include <tabulate/table.hpp>
  • Use namespace directly or with using: using namespace tabulate;
  • Instantiate the class: Table table;
  • And use the code as needed.

In this basic example we print two cells with predefined widths as desired:

vim main.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;
}

To compile you don’t need any additional flag, example:

g++ main.cpp
./a.out

Possible output:

TABLE

Note that to print the table you must use std::cout << table_name << '\n'; . And to add features, formatting, colors and others you can do union in your class instance, for example adding color would be: table[0][0].format().font_color(Color::yellow);, in this case according to the example above the [0][0] indicates that the output of the cell on the left will now be colored yellow .


More examples

For a more detailed example you can use the samples/ directory, example:

g++ tabulate/samples/summary.cpp
./a.out

The possible output will be:

Summary Tabulate C++

If you want to uninstall Tabulate, run:

sudo rm -rf /usr/local/lib64/cmake/tabulate /usr/local/include/tabulate

For more examples and information visit the official repository .


cpp cppdaily tui


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles