Pretty Print Tables

A while ago I wrote a chunk of code to dump data to a console window in a formatted table. Something like you’d see in a SQL console when doing queries. I’ve finally gotten around to open sourcing it so here’s a description of how to use it and where to find it.

To format data into the table structure, a table must first be initialized using table_init(). The data will be output in either CSV format or a pretty printed table depending on the value of the verbose parameter. Rows are added to the table with the table_row() function. Each column in the row is specified by a position in the format string. For example the first format specifier is the first column, second column two, etc. The total number of columns for a table is determined by the call to table_row() with the highest number of format specifiers. The format parameter for table_row() takes a C style format string. The acceptable format specifiers are a (mostly) subset of the printf() format specifiers with the addition of 't' for a timestamp. Format specifiers:

specifierOutput
s or SString of characters
c or CCharacter
d or iSigned decimal integer
uUnsigned decimal integer
oUnsigned octal integer
tTimestamp specified by a time_t value
x or XUnsigned hexadecimal integer
f, F, g or GDecimal floating point

[width].[precision] are also supported. Note that precision defaults to 0 so with floating point values it must always be specified or the value will be truncated to a whole number.


Finally, when data is done being added to the table, it is printed using the table_commit() function.

int main(int argc, char* argv[])
{
	struct table_t* table = table_init(stdout, "Test Table", 1);
	table_row(table, "%s%s", "Column 1", "Column 2");
	table_separator(table);
	table_row(table, "%s%s", "String value", "test string");
	table_row(table, "%s%c", "Character value", 'c');
	table_row(table, "%s%d", "Signed integer", -1);
	table_row(table, "%s%u", "Unsigned integer", 42);
	table_row(table, "%s%o", "Octal integer", 511);
	table_row(table, "%s%8x", "Hexadecimal integer", 255);
	table_row(table, "%s%.5f", "Floating point", 3.14159);
	table_row(table, "%s%t", "Timestamp", 0);
	table_commit(table);
	return 0;
}

Produces the following output:

$ ./table.exe
+---------------------------------------------------------------+
| Test Table                                                    |
+---------------------+-----------------------------------------+
| Column 1            | Column 2                                |
+---------------------+-----------------------------------------+
| String value        | test string                             |
| Character value     | c                                       |
| Signed integer      | -1                                      |
| Unsigned integer    | 42                                      |
| Octal integer       | 0777                                    |
| Hexadecimal integer | 0x000000ff                              |
| Floating point      | 3.14159                                 |
| Timestamp           | Thursday, January 01, 1970, 12:00:00 AM |
+---------------------+-----------------------------------------+

The code is free under the MIT license and can be downloaded here.

Related Posts

Leave a Reply