ROSE  0.11.58.0
FormattedTable.h
1 #ifndef ROSE_StringUtility_FormattedTable_H
2 #define ROSE_StringUtility_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
size_t nRows() const
Number of rows or columns of data.
static CellProperties merge(const CellProperties &a, const CellProperties &b)
Create new properties by merging two properties.
CellProperties()
Default constructor.
const Sawyer::Optional< Color::HSV > & background() const
Property: Background color.
Text is aligned to the left edge of the cell.
Main namespace for the ROSE library.
const CellProperties & cellProperties(size_t rowIdx, size_t columnIdx) const
Properties for a data cell.
const std::string & indentation() const
Property: Indentation.
const std::string & columnHeader(size_t rowIdx, size_t columnIdx) const
Property: Name for column headers.
const CellProperties & columnHeaderProperties(size_t rowIdx, size_t columnIdx) const
Properties for column headers.
FormattedTable()
Construct an empty table.
size_t nColumnHeaderRows() const
Property: Number of column header rows.
Alignment
How text is aligned in a table cell.
const Sawyer::Optional< Color::HSV > & foreground() const
Property: Foreground color.
void insert(size_t rowIdx, size_t columnIdx, const std::string &repr)
Insert data into a table.
void print(std::ostream &) const
Print a table.
const Sawyer::Optional< Alignment > & alignment() const
Property: Horizontal alignment.
A format friendly for shell scripts.
size_t nColumns() const
Number of rows or columns of data.
void resize(size_t nRows, size_t nColumns)
Resize the table data.
Format
Format when generating output.
void insert(size_t rowIdx, size_t columnIdx, const T &value, const CellProperties &prop)
Insert data into a table.
Text is centered in the cell.
Properties for a particular cell.
Format format() const
Property: Output format.
Comma separated values, RFC 4180.
void insert(size_t rowIdx, size_t columnIdx, const T &value)
Insert data into a table.
Text is aligned to the right edge of the cell.
Use ASCII-art to draw the table borders.
Class for printing nicely formattated tables.