This C++ template prints string and numeric data as an aligned, sortable
table.
It calculates column widths after all fields are known.
The template requires only the standard library.
Copy PrettyTable.h and AlphaNum.h into a project to use it.
The project also has a website.
Column sorting uses Dirk Jagdmann’s implementation of the
alphanum algorithm for human-friendly ordering.
The following snippet shows typical usage:
CPrettyTable<int, const char*, double> tbl("num", "name", "score");
for (int i = 0; i < 5; ++i) {
tbl.Add(i + 1, "FooBar", 2.5 * (i + 1));
}
tbl.SetCaption("FooBar");
tbl.Dump(std::cerr);The snippet prints the following output to the standard error stream:
+-----+[ FooBar ]------+
| num | name | score |
+-----+--------+-------+
| 1 | FooBar | 2.5 |
| 2 | FooBar | 5 |
| 3 | FooBar | 7.5 |
| 4 | FooBar | 10 |
| 5 | FooBar | 12.5 |
+-----+--------+-------+Each template parameter sets the type of one column, up to 20 columns.
A column accepts any type that supports operator<<.
Numeric columns align right and other columns align left.
| Method | Description |
|---|---|
|
Creates a table without a header row. |
|
Appends a row with one value per column. Omitted trailing values leave their cells empty. |
|
Embeds a caption into the top border.
A |
|
Sorts data rows by the zero-based column with the alphanum algorithm.
The order is |
|
Prints the table to an output stream. Each line, including the first one, starts with a newline. |
|
Returns the printed table as a string. |
|
Returns the number of data rows without the header row. |
|
Reports whether the table has no data rows. |
|
Removes all data rows and keeps the header row. |
redo test builds the debug application and compares its output with the
recorded golden file.
redo lint applies the following linters to the source files:
actionlint,
checkmake,
reuse,
shellcheck,
shfmt,
typos,
yamllint,
zizmor.
ptable is copyrighted by David Rabkin and available under the
Zero-Clause BSD license.