Skip to content
dattanchu edited this page Apr 23, 2011 · 12 revisions

BPrinter is a pretty printer for C++ to be used with command line application (or for outputting to string/log). It only uses standard C++ (and Boost Karma, see FAQ) and is meant to be easy-to-use.

As long as your object can be serialized with std::ostream, you are good to go. The first class available in BPrinter is the TablePrinter class. The TablePrinter class is a class for pretty printing a two-dimensional table.

The usage is straight forward.

#include <bprinter/table_printer.h>

TablePrinter tp(&std::cout);
tp.AddColumn("Name", 25);
tp.AddColumn("Age", 5);
tp.AddColumn("Position", 30);
tp.AddColumn("Allowance", 9);

tp.PrintHeader();
tp << "Dat Chu" << 25 << "Research Assistant" << -0.00000000001337;
tp << "John Doe" << 26 << "Too much float" << 125456789.123456789;
tp << "John Doe" << 26 << "Typical Int" << 1254;
tp << "John Doe" << 26 << "Typical float" << 1254.36;
tp << "John Doe" << 26 << "Too much negative" << -125456789.123456789;
tp << "John Doe" << 26 << "Exact size int" << 125456789;
tp << "John Doe" << 26 << "Exact size int" << -12545678;
tp << "John Doe" << 26 << "Exact size float" << -1254567.8;
tp << "John Doe" << 26 << "Negative Int" << -1254;
tp << "Jane Doe" << bprinter::endl();
tp << "Tom Doe" << 7 << "Student" << -M_PI;
tp.PrintFooter();
+------------------------------------------------------------------------+
|                     Name|  Age|                      Position|Allowance|
+------------------------------------------------------------------------+
|                  Dat Chu|   25|            Research Assistant|-0.00000*|
|                 John Doe|   26|                Too much float|12545678*|
|                 John Doe|   26|                   Typical Int|     1254|
|                 John Doe|   26|                 Typical float|1254.360*|
|                 John Doe|   26|             Too much negative|-1254567*|
|                 John Doe|   26|                Exact size int|125456789|
|                 John Doe|   26|                Exact size int|-12545678|
|                 John Doe|   26|              Exact size float|-1254567*|
|                 John Doe|   26|                  Negative Int|    -1254|
|                 Jane Doe|     |                              |         |
|                  Tom Doe|    7|                       Student|-3.14159*|
+------------------------------------------------------------------------+

FAQ

What does B stand for?

BPrinter supposes to mean Betty Printer, intended as a pun to Pretty Printer classes found in other languages.

Will you add support for other types of printing?

Sure. If you want something pretty printed (tree, annotation of equation, ...), let me know.

Can I use this in my project?

Yup. It is BSD. Do with it whatever you like. Drop me a line if you find it useful.

I don't use Boost and I don't intend to. How can I still use bprinter?

When you compile BPrinter, there is a CMake option: USE_BOOST_KARMA, uncheck this option (on by default) will remove the usage of Boost.Karma in BPrinter. However, you should consider using some parts of Boost. It is a good package.

Clone this wiki locally