Dat Chu
Dat Chu

Reputation: 11150

Pretty print a table in C++

I am looking for a library similar to prettytable but in C++

http://code.google.com/p/prettytable/

I know how to generate one myself using either printf or iostream. However, I would like to know if there is a library for this.

I am interested only in writing this ASCII table to the console.

Preferably something like:

std::vector<std::string> headers;
headers.push_back("My Awesome Header 1");
headers.push_back("My Awesome Header 2");
headers.push_back("My Awesome Header 3");


PrettyTablePrinter ptp;
ptp.SetHeaders(headers);
// Set some other options here
ptp.AddRow(data[0]);
ptp.AddRow(data[1]);
ptp.AddRow(data[2]);

ptp.Print(&std::cout);

Upvotes: 18

Views: 45023

Answers (8)

frakod
frakod

Reputation: 1901

In my opinion, these 2 libraries are the best options today :-

  1. tabulate
  2. libfort

libfort is comparatively simpler and smaller since it's written in C, whereas tabulate is highly customisable and is written in C++

Upvotes: 4

Attaullah Khan
Attaullah Khan

Reputation: 370

LibFort

It's Fantastic, they have Several cool table styles too.

https://github.com/seleznevae/libfort

Upvotes: 4

friedmud
friedmud

Reputation: 673

I wasn't satisfied with any of the ones I found online so I wrote my own: https://github.com/friedmud/variadic_table

It uses variadic templates to allow each column to hold a different type. It also only requires C++11.

VariadicTable<std::string, double, int, std::string> vt({"Name", "Weight", "Age", "Brother"});

vt.addRow({"Cody", 180.2, 40, "John"});
vt.addRow({"David", 175.3, 38, "Andrew"});
vt.addRow({"Robert", 140.3, 27, "Fande"});

vt.print();

This will output:

--------------------------------------
| Name |  Weight  |    Age   |Brother|
--------------------------------------
|Cody  |     180.2|        40|John   |
|David |     175.3|        38|Andrew |
|Robert|     140.3|        27|Fande  |
--------------------------------------

This is actively being used in a large software project - so it will be maintained and developed over time. Feel free to submit issues / PRs

Upvotes: 15

Dat Chu
Dat Chu

Reputation: 11150

Since I have not found a good C++ solution, I have written one for you all

https://github.com/dattanchu/bprinter/wiki

Upvotes: 27

yasouser
yasouser

Reputation: 5187

You may achieve it using ncurses library. Its C library.

Upvotes: 0

Billy O&#39;Connor
Billy O&#39;Connor

Reputation: 179

The most generic way to format any output of all, in fact the only way to do so within the C++ language is with I/O Manipulators.
http://www.fredosaurus.com/notes-cpp/io/omanipulators.html

Upvotes: -3

hkaiser
hkaiser

Reputation: 11521

While not exactly what you're looking for, Boost.Spirit contains a library (named Karma) usable to generate this kind of output fairly easily. The docs are here.

Upvotes: 5

icecrime
icecrime

Reputation: 76845

To my knowledge, you have three major options here :

I'm not aware of any library which could help you in the "table design" more than this.

Upvotes: 9

Related Questions