ROSE  0.11.96.11
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 
9 namespace Rose {
10 
16 public:
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 
43  Sawyer::Optional<Color::HSV> foreground_;
44  Sawyer::Optional<Color::HSV> background_;
45  Sawyer::Optional<Alignment> alignment_;
46 
47  public:
52 
59 
84  static CellProperties merge(const CellProperties &a, const CellProperties &b);
85  };
86 
87 private:
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 
95 public:
97  // Constructors, etc.
99 
101  FormattedTable();
102 
104  FormattedTable(size_t nRows, size_t nColumns);
105 
107  // Table-wide properties
109 
115  Format format() const;
116  void format(Format);
125  const std::string& indentation() const;
126  void indentation(const std::string&);
129  // 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&);
180  // 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&);
215  // Printing
218 
220  void print(std::ostream&) const;
221 
222 private:
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 
234 std::ostream& operator<<(std::ostream &out, const Rose::FormattedTable &table);
235 
236 #endif
Rose::FormattedTable::CellProperties::alignment
const Sawyer::Optional< Alignment > & alignment() const
Property: Horizontal alignment.
Rose::FormattedTable::nColumns
size_t nColumns() const
Number of rows or columns of data.
Rose::FormattedTable::Alignment
Alignment
How text is aligned in a table cell.
Definition: FormattedTable.h:35
Sawyer::Optional
Holds a value or nothing.
Definition: Optional.h:49
Rose::FormattedTable::nRows
size_t nRows() const
Number of rows or columns of data.
Rose::FormattedTable
Class for printing nicely formattated tables.
Definition: FormattedTable.h:15
Rose::FormattedTable::format
Format format() const
Property: Output format.
Rose::FormattedTable::resize
void resize(size_t nRows, size_t nColumns)
Resize the table data.
Rose::FormattedTable::nColumnHeaderRows
size_t nColumnHeaderRows() const
Property: Number of column header rows.
Rose::FormattedTable::insert
void insert(size_t rowIdx, size_t columnIdx, const std::string &repr)
Insert data into a table.
Rose::FormattedTable::insert
void insert(size_t rowIdx, size_t columnIdx, const T &value, const CellProperties &prop)
Insert data into a table.
Definition: FormattedTable.h:163
Rose::FormattedTable::Format
Format
Format when generating output.
Definition: FormattedTable.h:22
Rose::FormattedTable::get
const std::string & get(size_t rowIdx, size_t colIdx) const
Get the data for a table cell.
Rose::FormattedTable::columnHeaderProperties
const CellProperties & columnHeaderProperties(size_t rowIdx, size_t columnIdx) const
Properties for column headers.
Rose::FormattedTable::insert
void insert(size_t rowIdx, size_t columnIdx, const T &value)
Insert data into a table.
Definition: FormattedTable.h:160
Rose::FormattedTable::indentation
const std::string & indentation() const
Property: Indentation.
Rose::FormattedTable::Format::SHELL
@ SHELL
A format friendly for shell scripts.
Rose::FormattedTable::CellProperties::foreground
const Sawyer::Optional< Color::HSV > & foreground() const
Property: Foreground color.
Rose::FormattedTable::Format::PLAIN
@ PLAIN
Use ASCII-art to draw the table borders.
Rose::FormattedTable::CellProperties::merge
static CellProperties merge(const CellProperties &a, const CellProperties &b)
Create new properties by merging two properties.
Rose::FormattedTable::CellProperties::CellProperties
CellProperties()
Default constructor.
Rose::FormattedTable::columnHeader
const std::string & columnHeader(size_t rowIdx, size_t columnIdx) const
Property: Name for column headers.
Rose::FormattedTable::CellProperties::background
const Sawyer::Optional< Color::HSV > & background() const
Property: Background color.
Rose::FormattedTable::Alignment::CENTER
@ CENTER
Text is centered in the cell.
Rose
Main namespace for the ROSE library.
Definition: BinaryTutorial.dox:3
Rose::FormattedTable::Format::CSV
@ CSV
Comma separated values, RFC 4180.
Rose::FormattedTable::CellProperties
Properties for a particular cell.
Definition: FormattedTable.h:42
Rose::FormattedTable::print
void print(std::ostream &) const
Print a table.
Rose::FormattedTable::Alignment::LEFT
@ LEFT
Text is aligned to the left edge of the cell.
Rose::FormattedTable::Format::HTML
@ HTML
Generate HTML output.
Rose::FormattedTable::FormattedTable
FormattedTable()
Construct an empty table.
Rose::FormattedTable::Alignment::RIGHT
@ RIGHT
Text is aligned to the right edge of the cell.
Rose::FormattedTable::cellProperties
const CellProperties & cellProperties(size_t rowIdx, size_t columnIdx) const
Properties for a data cell.