LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
LArFormattingHelper.cc
Go to the documentation of this file.
1 
10 
11 #include <iomanip>
12 
13 using namespace pandora;
14 
15 namespace lar_content
16 {
17 
18 void LArFormattingHelper::SetStyle(const LArFormattingHelper::Style style, std::ostream &stream)
19 {
20  LArFormattingHelper::PrintFormatCharacter(static_cast<unsigned int>(style), stream);
21 }
22 
23 //--------------------------------------------------------------------------------------------------------------------------------------
24 
25 void LArFormattingHelper::SetColor(const LArFormattingHelper::Color color, std::ostream &stream)
26 {
27  LArFormattingHelper::PrintFormatCharacter(static_cast<unsigned int>(color), stream);
28 }
29 
30 //--------------------------------------------------------------------------------------------------------------------------------------
31 
32 void LArFormattingHelper::ResetStyle(std::ostream &stream)
33 {
34  LArFormattingHelper::SetStyle(REGULAR, stream);
35 }
36 
37 //--------------------------------------------------------------------------------------------------------------------------------------
38 
39 void LArFormattingHelper::ResetColor(std::ostream &stream)
40 {
41  LArFormattingHelper::SetColor(DEFAULT, stream);
42 }
43 
44 //--------------------------------------------------------------------------------------------------------------------------------------
45 
46 void LArFormattingHelper::Reset(std::ostream &stream)
47 {
48  LArFormattingHelper::ResetColor(stream);
49  LArFormattingHelper::ResetStyle(stream);
50 }
51 
52 //--------------------------------------------------------------------------------------------------------------------------------------
53 
54 void LArFormattingHelper::PrintFormatCharacter(const unsigned int code, std::ostream &stream)
55 {
56  if (!(stream << LArFormattingHelper::GetFormatCharacter(code)))
57  throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
58 }
59 
60 //--------------------------------------------------------------------------------------------------------------------------------------
61 
62 std::string LArFormattingHelper::GetFormatCharacter(const unsigned int code)
63 {
64  const std::string startFormattingCode("\x1B[");
65  const std::string endFormattingCode("m");
66  return (startFormattingCode + std::to_string(code) + endFormattingCode);
67 }
68 
69 //--------------------------------------------------------------------------------------------------------------------------------------
70 
71 void LArFormattingHelper::PrintHeader(const std::string &title, const unsigned int width)
72 {
74  std::cout << std::endl << std::string(width, '-') << "\r-" << title << std::endl;
76 }
77 
78 //--------------------------------------------------------------------------------------------------------------------------------------
79 
80 void LArFormattingHelper::PrintRule(const unsigned int width)
81 {
83 }
84 
85 //--------------------------------------------------------------------------------------------------------------------------------------
86 //--------------------------------------------------------------------------------------------------------------------------------------
87 
88 LArFormattingHelper::Table::Table(const StringVector &columnTitles, const unsigned int precision) :
89  m_columnTitles(columnTitles),
90  m_precision(precision)
91 {
92  m_stringstream.precision(m_precision);
93 
94  for (const std::string &title : m_columnTitles)
95  m_widths.push_back(title.length());
96 }
97 
98 //--------------------------------------------------------------------------------------------------------------------------------------
99 
101 {
102  if (m_columnTitles[m_elements.size() % m_columnTitles.size()] == "")
103  {
104  m_format.push_back("");
105  m_elements.push_back("");
106  }
107 }
108 
109 //--------------------------------------------------------------------------------------------------------------------------------------
110 
111 bool LArFormattingHelper::Table::IsSeparatorColumn(const unsigned int column) const
112 {
113  if (column >= m_columnTitles.size())
114  throw StatusCodeException(STATUS_CODE_OUT_OF_RANGE);
115 
116  return (m_columnTitles[column] == "");
117 }
118 
119 //--------------------------------------------------------------------------------------------------------------------------------------
120 
122 {
123  if (m_widths.empty() || m_elements.empty())
124  throw StatusCodeException(STATUS_CODE_OUT_OF_RANGE);
125 
126  const unsigned int currentElementIndex(m_elements.size() - 1);
127  const unsigned int column(currentElementIndex % m_widths.size());
128  m_widths[column] = std::max(static_cast<unsigned int>(m_elements[currentElementIndex].length()), m_widths[column]);
129 }
130 
131 //--------------------------------------------------------------------------------------------------------------------------------------
132 
134 {
135  if (m_columnTitles.empty())
136  return;
137 
138  this->PrintColumnTitles();
139  this->PrintHorizontalLine();
140  this->PrintTableElements();
141 }
142 
143 //--------------------------------------------------------------------------------------------------------------------------------------
144 
146 {
147  for (unsigned int i = 0, nColumns = m_columnTitles.size(); i < nColumns; ++i)
149 }
150 
151 //--------------------------------------------------------------------------------------------------------------------------------------
152 
154 {
155  for (unsigned int i = 0, nColumns = m_columnTitles.size(); i < nColumns; ++i)
157 }
158 
159 //--------------------------------------------------------------------------------------------------------------------------------------
160 
162 {
163  if (m_columnTitles.empty())
164  return;
165 
166  const unsigned int nRows((m_elements.size() + m_columnTitles.size() - 1)/m_columnTitles.size());
167  const unsigned int nColumns(m_columnTitles.size());
168  if (nRows * nColumns != m_elements.size())
169  {
170  std::cout << "LArFormattingHelper::Table::PrintTableElements - Error: Number of table elements added doesn't fill a whole row" << std::endl;
171  throw StatusCodeException(STATUS_CODE_OUT_OF_RANGE);
172  }
173 
174  for (unsigned int i = 0; i < nRows * nColumns; i++)
175  this->PrintTableCell(m_elements[i], m_format[i], i);
176 }
177 
178 //--------------------------------------------------------------------------------------------------------------------------------------
179 
180 void LArFormattingHelper::Table::PrintTableCell(const std::string &value, const std::string &format, const unsigned int index) const
181 {
182  if (m_columnTitles.empty())
183  return;
184 
185  const unsigned int column(index % m_widths.size());
186 
187  const std::string separator(this->IsSeparatorColumn(column) ? "" : " ");
188  std::cout << "|" << format << separator << std::setw(m_widths[column]) << std::internal << value << separator << std::resetiosflags(std::ios::adjustfield);
190 
191  if (column == m_columnTitles.size() - 1)
192  std::cout << "|" << std::endl;
193 }
194 
195 } // namespace lar_content
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.
void PrintTableElements() const
Print the table elements.
static std::string GetFormatCharacter(const unsigned int code)
Get a formatting character.
void PrintHorizontalLine() const
Print a horizontal line.
Int_t max
Definition: plot.C:27
std::stringstream m_stringstream
The stringstream to print objects to.
ntupleExperimental Reset()
const unsigned int m_precision
The number of significant figures to use when displaying number types.
pandora::StringVector m_elements
The vector of flattened table elements.
const pandora::StringVector m_columnTitles
The vector of columns titles in the table.
void CheckAndSetSeparatorColumn()
Check if the next table cell is in a separator column, if so add a blank element. ...
std::size_t color(std::string const &procname)
std::string value(boost::any const &)
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
Definition: BitMask.h:187
std::vector< unsigned int > m_widths
The widths of each column (in units of number of characters)
gROOT SetStyle("clearRetro")
void Print() const
Print the table.
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 PrintHeader(std::string someText)
Definition: Utils.cxx:4568
void PrintColumnTitles() const
Print the column titles.