Pretty print a table in C++
Asked Answered
O

8

18

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);
Openhearted answered 17/12, 2010 at 15:1 Comment(1)
Related: https://mcmap.net/q/668884/-printing-2d-table-headers/…Songsongbird
O
25

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

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

Openhearted answered 17/4, 2011 at 22:8 Comment(0)
C
15

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

Corinthian answered 29/5, 2018 at 16:44 Comment(3)
The repository once cloned have 20+ errors just when I run make.Kipkipling
it's also header only!Spense
Nice that it's header only, but the GNU licence is a bummer for non-private use :(Intonation
R
9

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.

Raneeraney answered 17/12, 2010 at 15:6 Comment(0)
G
5

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.

Gooseberry answered 17/12, 2010 at 15:7 Comment(0)
M
4

LibFort

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

https://github.com/seleznevae/libfort

Mediation answered 30/1, 2020 at 8:0 Comment(1)
Could not get this to work with Visual Studio / Windows. Yes, I ran CMAKE, but encountered lots of compile errors.Mohican
E
4

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++

Elke answered 18/11, 2021 at 15:53 Comment(1)
Thank you very much for your answer. I just wanted a simple library and libfort provided everything I needed, excellent library.Drabbet
D
0

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

Decemvir answered 17/12, 2010 at 15:26 Comment(0)
B
-3

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

Borisborja answered 17/12, 2010 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.