Pandora
Pandora source code navigator
Loading...
Searching...
No Matches
LArFormattingHelper.h
Go to the documentation of this file.
1
8#ifndef LAR_FORMATTING_HELPER_H
9#define LAR_FORMATTING_HELPER_H 1
10
12#include "Pandora/StatusCodes.h"
13
14#include <iostream>
15#include <sstream>
16#include <string>
17#include <vector>
18
19namespace lar_content
20{
21
26{
27public:
28 enum Color : unsigned int;
29 enum Style : unsigned int;
30
36 static void SetStyle(const Style style, std::ostream &stream = std::cout);
37
43 static void SetColor(const Color color, std::ostream &stream = std::cout);
44
48 static void ResetStyle(std::ostream &stream = std::cout);
49
53 static void ResetColor(std::ostream &stream = std::cout);
54
58 static void Reset(std::ostream &stream = std::cout);
59
65 static void PrintFormatCharacter(const unsigned int code, std::ostream &stream = std::cout);
66
72 static std::string GetFormatCharacter(const unsigned int code);
73
80 static void PrintHeader(const std::string &title = "", const unsigned int width = 140);
81
87 static void PrintRule(const unsigned int width = 140);
88
92 enum Style : unsigned int
93 {
95 BOLD = 1,
96 DIM = 2,
98 INVERTED = 7
99 };
100
104 enum Color : unsigned int
105 {
107 BLACK = 30,
108 RED = 31,
109 GREEN = 32,
110 YELLOW = 33,
111 BLUE = 34,
113 CYAN = 36,
122 WHITE = 97
123 };
124
128 class Table
129 {
130 public:
137 Table(const pandora::StringVector &columnTitles, const unsigned int precision = 3);
138
142 void Print() const;
143
151 template <typename T>
152 void AddElement(const T &value, const Style style = REGULAR, const Color color = DEFAULT);
153
154 private:
162 bool IsSeparatorColumn(const unsigned int column) const;
163
169 void PrintColumnTitles() const;
170
176 void PrintHorizontalLine() const;
177
183 void PrintTableElements() const;
184
193 void PrintTableCell(const std::string &value, const std::string &format, const unsigned int index) const;
194
199
203 void UpdateColumnWidth();
204
206 const unsigned int m_precision;
209 std::vector<unsigned int> m_widths;
210 std::stringstream m_stringstream;
211 };
212};
213
214//------------------------------------------------------------------------------------------------------------------------------------------
215
216template <typename T>
217inline void LArFormattingHelper::Table::AddElement(const T &value, const Style style, const Color color)
218{
220
221 if (!(m_stringstream << value))
222 throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
223
224 m_elements.push_back(static_cast<std::string>(m_stringstream.str()));
226 m_stringstream.str("");
227
228 this->UpdateColumnWidth();
230}
231
232} // namespace lar_content
233
234#endif // #ifndef LAR_FORMATTING_HELPER_H
Header file defining relevant internal typedefs, sort and string conversion functions.
Header file defining status codes and relevant preprocessor macros.
void PrintHorizontalLine() const
Print a horizontal line.
void CheckAndSetSeparatorColumn()
Check if the next table cell is in a separator column, if so add a blank element.
void AddElement(const T &value, const Style style=REGULAR, const Color color=DEFAULT)
Add an element to the table into the next (non-separator) column.
pandora::StringVector m_format
The formatting of each table element.
std::stringstream m_stringstream
The stringstream to print objects to.
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
std::vector< unsigned int > m_widths
The widths of each column (in units of number of characters)
void PrintTableCell(const std::string &value, const std::string &format, const unsigned int index) const
Print a table cell.
const unsigned int m_precision
The number of significant figures to use when displaying number types.
void UpdateColumnWidth()
Update the width of the last column in which an element was added.
pandora::StringVector m_elements
The vector of flattened table elements.
bool IsSeparatorColumn(const unsigned int column) const
If the supplied column is a separator (vertical rule)
void PrintColumnTitles() const
Print the column titles.
void PrintTableElements() const
Print the table elements.
static void Reset(std::ostream &stream=std::cout)
Reset the formatting and text color of the standard output stream.
static void PrintRule(const unsigned int width=140)
Print a horizontal rule.
static void SetStyle(const Style style, std::ostream &stream=std::cout)
Set the format style (to standard output stream)
static void PrintHeader(const std::string &title="", const unsigned int width=140)
Print a header line of a given width.
static std::string GetFormatCharacter(const unsigned int code)
Get a formatting character.
static void ResetStyle(std::ostream &stream=std::cout)
Reset the style of the standard output stream.
static void SetColor(const Color color, std::ostream &stream=std::cout)
Set the text color (of standard output stream)
static void ResetColor(std::ostream &stream=std::cout)
Reset the text color of the standard output stream.
static void PrintFormatCharacter(const unsigned int code, std::ostream &stream=std::cout)
Print a formatting character to the standard output stream.
StatusCodeException class.
std::vector< std::string > StringVector