ocean.io.console.StructTable

Helper template to display tables to the console where the headings of the columns are the names of the fields of a struct, and where the contents of each row of the table are the values of the fields of an instance of the struct type.

Usage example:

import ocean.io.console.StructTable;

struct Test
{
    int number;
    float fraction;
    char[] name;
}

scope table = new StructTable!(Test);

table.addRow(Test(23, 12.12, "gavin"));
table.addRow(Test(17, 99.9, "david"));
table.addRow(Test(11, 10.0, "luca"));

table.display();

The output of the usage example would be:

number | fraction | name |

23 |    12.12 | gavin |
17 |    99.90 | david |
11 |    10.00 |  luca |
The StructTable template class internally generates one protected method for
each field of the struct it is based on (the template parameter). These
methods are named <field name>_string, and return a char[] which is to be
displayed in the approrpiate column of the table. By default these methods
simply format the struct fields using Layout. However, due to the way this
is implemented, it is possible to derive a class which overrides the
stringifying method of one or more struct fields, enabling special output
behaviour to be implemented.

Class overriding usage example (extends above example):

import ocean.text.convert.Formatter;

class TestTable : StructTable!(Test) { override protected char[] fraction_string ( float* field ) { this.format_buffer.length = 0; sformat(this.format_buffer, "{}%", *field * 100.0); return this.format_buffer; } }

scope table2 = new TestTable;

table2.addRow(Test(23, 0.12, "gavin")); table2.addRow(Test(17, 0.999, "david")); table2.addRow(Test(11, 0.1, "luca"));

table2.display();

The output of the usage example would be:
number |  fraction |  name |

23 | 12.00% | gavin | 17 | 0.999% | david | 11 | 0.10% | luca |

Members

Classes

StructTable
class StructTable(S)

Struct table template class.

Meta

License

Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. Alternatively, this file may be distributed under the terms of the Tango 3-Clause BSD License (see LICENSE_BSD.txt for details).