Table Formatter is a small utility for pretty printing tabulated data in a table format. Data is added to the table programmatically when complete is printed to an output file in either CSV or pretty printed table format.
Rows are added to the table using a (mostly) subset of C printf style format specifiers:
specifier | Output |
---|---|
s or S | String of characters |
c or C | Character |
d or i | Signed decimal integer |
u | Unsigned decimal integer |
o | Unsigned octal integer |
t | Timestamp specified by a time_t value |
x or X | Unsigned hexadecimal integer |
f, F, g or G | Decimal 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.
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 | +---------------------+-----------------------------------------+