ROSE 0.11.145.250
FormattedTable.h
1#ifndef ROSE_FormattedTable_H
2#define ROSE_FormattedTable_H
3
4#include <Rose/Color.h>
5#include <boost/lexical_cast.hpp>
6#include <string>
7#include <vector>
8
9namespace Rose {
10
16public:
18 // Public types
20
22 enum class Format {
23 PLAIN,
24 HTML,
25 CSV,
26 SHELL
31 };
32
34 enum class Alignment {
35 LEFT,
36 RIGHT,
37 CENTER
38 };
39
85
86private:
87 std::vector<std::vector<std::string>> cells_; // data
88 std::vector<std::vector<CellProperties>> props_; // data properties
89 std::vector<std::vector<std::string>> columnHeaders_; // titles for columns
90 std::vector<std::vector<CellProperties>> columnHeaderProps_;
91 std::string indentation_; // to be printed before each line of output
92 Format format_ = Format::PLAIN;
93
94public:
96 // Constructors, etc.
98
101
104
106 // Table-wide properties
108
114 Format format() const;
124 const std::string& indentation() const;
125 void indentation(const std::string&);
129 // FormattedTable data
131
138 void resize(size_t nRows, size_t nColumns);
139 void resize(size_t nRows);
147 size_t nRows() const;
148 size_t nColumns() const;
157 void insert(size_t rowIdx, size_t columnIdx, const std::string &repr);
158 void insert(size_t rowIdx, size_t columnIdx, const std::string &repr, const CellProperties&);
159 template<typename T> void insert(size_t rowIdx, size_t columnIdx, const T &value) {
160 insert(rowIdx, columnIdx, boost::lexical_cast<std::string>(value));
161 }
162 template<typename T> void insert(size_t rowIdx, size_t columnIdx, const T &value, const CellProperties &prop) {
163 insert(rowIdx, columnIdx, boost::lexical_cast<std::string>(value), prop);
164 }
168 const std::string& get(size_t rowIdx, size_t colIdx) const;
169
175 const CellProperties& cellProperties(size_t rowIdx, size_t columnIdx) const;
176 void cellProperties(size_t rowIdx, size_t columnIdx, const CellProperties&);
180 // FormattedTable columns
182
190 size_t nColumnHeaderRows() const;
191 void nColumnHeaderRows(size_t);
200 const std::string& columnHeader(size_t rowIdx, size_t columnIdx) const;
201 void columnHeader(size_t rowIdx, size_t columnIdx, const std::string &title);
210 const CellProperties& columnHeaderProperties(size_t rowIdx, size_t columnIdx) const;
211 void columnHeaderProperties(size_t rowIdx, size_t columnIdx, const CellProperties&);
215 // Printing
217
219 void print(std::ostream&) const;
220
221private:
222 std::string cellPropertiesBegin(const CellProperties&) const;
223 std::string cellPropertiesEnd(const CellProperties&) const;
224 void printHorizontalRule(std::ostream&, const std::vector<size_t> &widths) const;
225 void printRow(std::ostream&, const std::vector<size_t> &widths, const std::vector<CellProperties> &props,
226 const std::vector<std::string> &row) const;
227 std::vector<size_t> computeColumnWidths() const;
228
229};
230
231} // namespace
232
233std::ostream& operator<<(std::ostream &out, const Rose::FormattedTable &table);
234
235#endif
Properties for a particular cell.
CellProperties & alignment(const Sawyer::Optional< Alignment > &)
Property: Horizontal alignment.
const Sawyer::Optional< Color::HSV > & foreground() const
Property: Foreground color.
const Sawyer::Optional< Alignment > & alignment() const
Property: Horizontal alignment.
CellProperties & background(const Sawyer::Optional< Color::HSV > &)
Property: Background color.
CellProperties & foreground(const Sawyer::Optional< Color::HSV > &)
Property: Foreground color.
CellProperties()
Default constructor.
const Sawyer::Optional< Color::HSV > & background() const
Property: Background color.
CellProperties(const Sawyer::Optional< Color::HSV > &foreground, const Sawyer::Optional< Color::HSV > &background, const Sawyer::Optional< Alignment > &alignment)
Constructor.
static CellProperties merge(const CellProperties &a, const CellProperties &b)
Create new properties by merging two properties.
Class for printing nicely formattated tables.
const std::string & get(size_t rowIdx, size_t colIdx) const
Get the data for a table cell.
size_t nRows() const
Number of rows or columns of data.
size_t nColumns() const
Number of rows or columns of data.
void nColumnHeaderRows(size_t)
Property: Number of column header rows.
const std::string & columnHeader(size_t rowIdx, size_t columnIdx) const
Property: Name for column headers.
void insert(size_t rowIdx, size_t columnIdx, const std::string &repr)
Insert data into a table.
const CellProperties & columnHeaderProperties(size_t rowIdx, size_t columnIdx) const
Properties for column headers.
Alignment
How text is aligned in a table cell.
@ RIGHT
Text is aligned to the right edge of the cell.
@ LEFT
Text is aligned to the left edge of the cell.
@ CENTER
Text is centered in the cell.
void indentation(const std::string &)
Property: Indentation.
Format format() const
Property: Output format.
void print(std::ostream &) const
Print a table.
void cellProperties(size_t rowIdx, size_t columnIdx, const CellProperties &)
Properties for a data cell.
FormattedTable(size_t nRows, size_t nColumns)
Construct a table with a certain number of cells.
void columnHeader(size_t rowIdx, size_t columnIdx, const std::string &title)
Property: Name for column headers.
const std::string & indentation() const
Property: Indentation.
size_t nColumnHeaderRows() const
Property: Number of column header rows.
void resize(size_t nRows, size_t nColumns)
Resize the table data.
FormattedTable()
Construct an empty table.
void insert(size_t rowIdx, size_t columnIdx, const T &value)
Insert data into a table.
void insert(size_t rowIdx, size_t columnIdx, const T &value, const CellProperties &prop)
Insert data into a table.
void columnHeaderProperties(size_t rowIdx, size_t columnIdx, const CellProperties &)
Properties for column headers.
void format(Format)
Property: Output format.
void insert(size_t rowIdx, size_t columnIdx, const std::string &repr, const CellProperties &)
Insert data into a table.
const CellProperties & cellProperties(size_t rowIdx, size_t columnIdx) const
Properties for a data cell.
Format
Format when generating output.
@ SHELL
A format friendly for shell scripts.
@ HTML
Generate HTML output.
@ CSV
Comma separated values, RFC 4180.
@ PLAIN
Use ASCII-art to draw the table borders.
void resize(size_t nRows)
Resize the table data.
Holds a value or nothing.
Definition Optional.h:56
The ROSE library.