LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
LArFormattingHelper.h
Go to the documentation of this file.
1 
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 
26 {
27 public:
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  {
94  REGULAR = 0,
95  BOLD = 1,
96  DIM = 2,
99  };
100 
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 
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 
205  const pandora::StringVector m_columnTitles;
206  const unsigned int m_precision;
207  pandora::StringVector m_elements;
208  pandora::StringVector m_format;
209  std::vector<unsigned int> m_widths;
210  std::stringstream m_stringstream;
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.
bool IsSeparatorColumn(const unsigned int column) const
If the supplied column is a separator (vertical rule)
pandora::StringVector m_format
The formatting of each table element.
LArFormattingHelper class.
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.
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::string value(boost::any const &)
std::vector< unsigned int > m_widths
The widths of each column (in units of number of characters)
void Print() const
Print the table.
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.