ROSE 0.11.145.147
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
32 };
33
35 enum class Alignment {
36 LEFT,
37 RIGHT,
38 CENTER
39 };
40
86
87private:
88 std::vector<std::vector<std::string>> cells_; // data
89 std::vector<std::vector<CellProperties>> props_; // data properties
90 std::vector<std::vector<std::string>> columnHeaders_; // titles for columns
91 std::vector<std::vector<CellProperties>> columnHeaderProps_;
92 std::string indentation_; // to be printed before each line of output
93 Format format_ = Format::PLAIN;
94
95public:
97 // Constructors, etc.
99
102
105
107 // Table-wide properties
109
115 Format format() const;
125 const std::string& indentation() const;
126 void indentation(const std::string&);
130 // FormattedTable data
132
139 void resize(size_t nRows, size_t nColumns);
140 void resize(size_t nRows);
148 size_t nRows() const;
149 size_t nColumns() const;
158 void insert(size_t rowIdx, size_t columnIdx, const std::string &repr);
159 void insert(size_t rowIdx, size_t columnIdx, const std::string &repr, const CellProperties&);
160 template<typename T> void insert(size_t rowIdx, size_t columnIdx, const T &value) {
161 insert(rowIdx, columnIdx, boost::lexical_cast<std::string>(value));
162 }
163 template<typename T> void insert(size_t rowIdx, size_t columnIdx, const T &value, const CellProperties &prop) {
164 insert(rowIdx, columnIdx, boost::lexical_cast<std::string>(value), prop);
165 }
169 const std::string& get(size_t rowIdx, size_t colIdx) const;
170
176 const CellProperties& cellProperties(size_t rowIdx, size_t columnIdx) const;
177 void cellProperties(size_t rowIdx, size_t columnIdx, const CellProperties&);
181 // FormattedTable columns
183
191 size_t nColumnHeaderRows() const;
192 void nColumnHeaderRows(size_t);
201 const std::string& columnHeader(size_t rowIdx, size_t columnIdx) const;
202 void columnHeader(size_t rowIdx, size_t columnIdx, const std::string &title);
211 const CellProperties& columnHeaderProperties(size_t rowIdx, size_t columnIdx) const;
212 void columnHeaderProperties(size_t rowIdx, size_t columnIdx, const CellProperties&);
216 // Printing
218
220 void print(std::ostream&) const;
221
222private:
223 std::string cellPropertiesBegin(const CellProperties&) const;
224 std::string cellPropertiesEnd(const CellProperties&) const;
225 void printHorizontalRule(std::ostream&, const std::vector<size_t> &widths) const;
226 void printRow(std::ostream&, const std::vector<size_t> &widths, const std::vector<CellProperties> &props,
227 const std::vector<std::string> &row) const;
228 std::vector<size_t> computeColumnWidths() const;
229
230};
231
232} // namespace
233
234std::ostream& operator<<(std::ostream &out, const Rose::FormattedTable &table);
235
236#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.