LArFormattingHelper.h
Go to the documentation of this file.
1 /**
2  * @file larpandoracontent/LArHelpers/LArFormatting.h
3  *
4  * @brief Header file for the formatting helper class
5  *
6  * $Log: $
7  */
8 #ifndef LAR_FORMATTING_HELPER_H
9 #define LAR_FORMATTING_HELPER_H 1
10 
11 #include "Pandora/PandoraInternal.h"
12 #include "Pandora/StatusCodes.h"
13 
14 #include <iostream>
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 
19 namespace lar_content
20 {
21 
22 /**
23  * @brief LArFormattingHelper class
24  */
26 {
27 public:
28  enum Color : unsigned int;
29  enum Style : unsigned int;
30 
31  /**
32  * @brief Set the format style (to standard output stream)
33  *
34  * @param style the style of choice
35  */
36  static void SetStyle(const Style style, std::ostream &stream = std::cout);
37 
38  /**
39  * @brief Set the text color (of standard output stream)
40  *
41  * @param color the color of choice
42  */
43  static void SetColor(const Color color, std::ostream &stream = std::cout);
44 
45  /**
46  * @brief Reset the style of the standard output stream
47  */
48  static void ResetStyle(std::ostream &stream = std::cout);
49 
50  /**
51  * @brief Reset the text color of the standard output stream
52  */
53  static void ResetColor(std::ostream &stream = std::cout);
54 
55  /**
56  * @brief Reset the formatting and text color of the standard output stream
57  */
58  static void Reset(std::ostream &stream = std::cout);
59 
60  /**
61  * @brief Print a formatting character to the standard output stream
62  *
63  * @param code the formatting code to output
64  */
65  static void PrintFormatCharacter(const unsigned int code, std::ostream &stream = std::cout);
66 
67  /**
68  * @brief Get a formatting character
69  *
70  * @param code the formatting code to output
71  */
72  static std::string GetFormatCharacter(const unsigned int code);
73 
74  /**
75  * @brief Print a header line of a given width
76  *
77  * @param title the title of the header
78  * @param width the width of the header line
79  */
80  static void PrintHeader(const std::string &title = "", const unsigned int width = 140);
81 
82  /**
83  * @brief Print a horizontal rule
84  *
85  * @param width the width of the rule line
86  */
87  static void PrintRule(const unsigned int width = 140);
88 
89  /**
90  * @brief Style code enumeration
91  */
92  enum Style : unsigned int
93  {
94  REGULAR = 0,
95  BOLD = 1,
96  DIM = 2,
99  };
100 
101  /**
102  * @brief Style code enumeration
103  */
104  enum Color : unsigned int
105  {
106  DEFAULT = 39,
107  BLACK = 30,
108  RED = 31,
109  GREEN = 32,
110  YELLOW = 33,
111  BLUE = 34,
112  MAGENTA = 35,
113  CYAN = 36,
115  DARK_GRAY = 90,
116  LIGHT_RED = 91,
122  WHITE = 97
123  };
124 
125  /**
126  * @brief Table class
127  */
128  class Table
129  {
130  public:
131  /**
132  * @brief Table constructor
133  *
134  * @param columnTitles the string columns titles use empty string for a separator column
135  * @param precision the number of significant figures to display for number type table elements
136  */
137  Table(const pandora::StringVector &columnTitles, const unsigned int precision = 3);
138 
139  /**
140  * @brief Print the table
141  */
142  void Print() const;
143 
144  /**
145  * @brief Add an element to the table into the next (non-separator) column
146  *
147  * @param value the element to add to the table
148  * @param style the style of the element
149  * @param color the color of the element
150  */
151  template <typename T>
152  void AddElement(const T &value, const Style style = REGULAR, const Color color = DEFAULT);
153 
154  private:
155  /**
156  * @brief If the supplied column is a separator (vertical rule)
157  *
158  * @param column the column index to check
159  *
160  * @return true/false
161  */
162  bool IsSeparatorColumn(const unsigned int column) const;
163 
164  /**
165  * @brief Print the column titles
166  *
167  * @param widths a vector of the number of characters to include in each column
168  */
169  void PrintColumnTitles() const;
170 
171  /**
172  * @brief Print a horizontal line
173  *
174  * @param widths a vector of the number of characters to include in each column
175  */
176  void PrintHorizontalLine() const;
177 
178  /**
179  * @brief Print the table elements
180  *
181  * @param widths a vector of the number of characters to include in each column
182  */
183  void PrintTableElements() const;
184 
185  /**
186  * @brief Print a table cell
187  *
188  * @param value the string to print in the cell
189  * @param format a formatting string (see GetFormatCharacter(...))
190  * @param index the index of the table cell (0 --> number of elements) used to find the column
191  * @param widths a vector of the number of characters to include in each column
192  */
193  void PrintTableCell(const std::string &value, const std::string &format, const unsigned int index) const;
194 
195  /**
196  * @brief Check if the next table cell is in a separator column, if so add a blank element
197  */
199 
200  /**
201  * @brief Update the width of the last column in which an element was added
202  */
203  void UpdateColumnWidth();
204 
205  const pandora::StringVector m_columnTitles; ///< The vector of columns titles in the table
206  const unsigned int m_precision; ///< The number of significant figures to use when displaying number types
207  pandora::StringVector m_elements; ///< The vector of flattened table elements
208  pandora::StringVector m_format; ///< The formatting of each table element
209  std::vector<unsigned int> m_widths; ///< The widths of each column (in units of number of characters)
210  std::stringstream m_stringstream; ///< The stringstream to print objects to
211  };
212 };
213 
214 //------------------------------------------------------------------------------------------------------------------------------------------
215 
216 template <typename T>
217 inline 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
void PrintTableCell(const std::string &value, const std::string &format, const unsigned int index) const
Print a table cell.
float precision
Definition: makePolycone.py:48
bool IsSeparatorColumn(const unsigned int column) const
If the supplied column is a separator (vertical rule)
std::string string
Definition: nybbler.cc:12
pandora::StringVector m_format
The formatting of each table element.
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
Definition: qstring.cpp:11496
LArFormattingHelper class.
Definition: image.cpp:28
void PrintTableElements() const
Print the table elements.
static void SetColor(const Color color, std::ostream &stream=std::cout)
Set the text color (of standard output stream)
static void PrintFormatCharacter(const unsigned int code, std::ostream &stream=std::cout)
Print a formatting character to the standard output stream.
static std::string GetFormatCharacter(const unsigned int code)
Get a formatting character.
void PrintHorizontalLine() const
Print a horizontal line.
std::stringstream m_stringstream
The stringstream to print objects to.
const unsigned int m_precision
The number of significant figures to use when displaying number types.
static void PrintHeader(const std::string &title="", const unsigned int width=140)
Print a header line of a given width.
pandora::StringVector m_elements
The vector of flattened table elements.
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
CodeOutputInterface * code
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.
void CheckAndSetSeparatorColumn()
Check if the next table cell is in a separator column, if so add a blank element. ...
Table(const pandora::StringVector &columnTitles, const unsigned int precision=3)
Table constructor.
static void ResetColor(std::ostream &stream=std::cout)
Reset the text color of the standard output stream.
std::size_t color(std::string const &procname)
std::vector< unsigned int > m_widths
The widths of each column (in units of number of characters)
std::vector< string > StringVector
Definition: fcldump.cxx:29
void Print() const
Print the table.
std::vector< std::string > column
static void Reset(std::ostream &stream=std::cout)
Reset the formatting and text color of the standard output stream.
static void ResetStyle(std::ostream &stream=std::cout)
Reset the style of the standard output stream.
void UpdateColumnWidth()
Update the width of the last column in which an element was added.
void PrintColumnTitles() const
Print the column titles.
enum Color unsigned int enum static Style unsigned int void SetStyle(const Style style, std::ostream &stream=std::cout)
Set the format style (to standard output stream)
static void PrintRule(const unsigned int width=140)
Print a horizontal rule.