25 RDisplayElement::RDisplayElement(
const std::string &representation) : fRepresentation(representation)
32 RDisplayElement::RDisplayElement()
39 void RDisplayElement::SetPrint()
41 fPrintingAction = PrintingAction::ToBePrinted;
46 void RDisplayElement::SetIgnore()
48 fPrintingAction = PrintingAction::ToBeIgnored;
53 void RDisplayElement::SetDots()
55 fPrintingAction = PrintingAction::ToBeDotted;
60 bool RDisplayElement::IsPrint()
const
62 return fPrintingAction == PrintingAction::ToBePrinted;
67 bool RDisplayElement::IsIgnore()
const
69 return fPrintingAction == PrintingAction::ToBeIgnored;
74 bool RDisplayElement::IsDot()
const
76 return fPrintingAction == PrintingAction::ToBeDotted;
79 const std::string &RDisplayElement::GetRepresentation()
const
81 return fRepresentation;
84 bool RDisplayElement::IsEmpty()
const
86 return fRepresentation.empty();
94 void RDisplay::EnsureCurrentColumnWidth(
size_t w)
97 if (fWidths[fCurrentColumn] < w) {
98 if (w > std::numeric_limits<unsigned short>::max()) {
99 w = std::numeric_limits<unsigned short>::max();
101 fWidths[fCurrentColumn] = (
unsigned short) w;
105 void RDisplay::AddToRow(
const std::string &stringEle)
108 EnsureCurrentColumnWidth(stringEle.length());
111 fTable[fCurrentRow][fCurrentColumn] = DElement_t(stringEle);
117 void RDisplay::AddCollectionToRow(
const std::vector<std::string> &collection)
119 auto row = fCurrentRow;
122 size_t collectionSize = collection.size();
123 for (
size_t index = 0; index < collectionSize; ++index) {
124 auto stringEle = collection[index];
125 auto element = DElement_t(stringEle);
128 EnsureCurrentColumnWidth(stringEle.length());
130 if (index == 0 || index == collectionSize - 1) {
132 }
else if (index == 1) {
135 EnsureCurrentColumnWidth(3);
142 fTable[row][fCurrentColumn] = element;
145 if (index != collectionSize - 1 && fTable.size() <= row) {
148 fTable.push_back(std::vector<DElement_t>(fNColumns));
151 fNextRow = (fNextRow > row) ? fNextRow : row;
155 void RDisplay::MovePosition()
159 if (fCurrentColumn == fNColumns) {
160 fCurrentRow = fNextRow;
162 fNextRow = fCurrentRow + 1;
163 fTable.push_back(std::vector<DElement_t>(fNColumns));
167 RDisplay::RDisplay(
const VecStr_t &columnNames,
const VecStr_t &types,
int entries)
168 : fTypes(types), fWidths(columnNames.size(), 0), fRepresentations(columnNames.size()),
169 fCollectionsRepresentations(columnNames.size()), fNColumns(columnNames.size()), fEntries(entries)
173 fTable.push_back(std::vector<DElement_t>(columnNames.size()));
174 for (
auto name : columnNames) {
179 size_t RDisplay::GetNColumnsToShorten()
const
181 size_t totalWidth = 0;
183 auto size = fWidths.size();
184 for (
size_t i = 0; i < size; ++i) {
185 totalWidth += fWidths[i];
186 if (totalWidth > fgMaxWidth) {
194 void RDisplay::Print()
const
196 auto columnsToPrint =
197 fNColumns - GetNColumnsToShorten();
198 std::vector<bool> hasPrintedNext(fNColumns,
201 auto nrRows = fTable.size();
202 for (
size_t rowIndex = 0; rowIndex < nrRows; ++rowIndex) {
203 auto &row = fTable[rowIndex];
205 std::stringstream stringRow;
206 bool isRowEmpty =
true;
208 for (
size_t columnIndex = 0; columnIndex < columnsToPrint; ++columnIndex) {
209 const auto &element = row[columnIndex];
210 std::string printedElement =
"";
212 if (element.IsDot()) {
213 printedElement =
"...";
214 }
else if (element.IsPrint()) {
216 if (!hasPrintedNext[columnIndex]) {
217 printedElement = element.GetRepresentation();
219 hasPrintedNext[columnIndex] =
224 if (!hasPrintedNext[columnIndex]) {
225 size_t i = rowIndex + 1;
226 for (; !fTable[i][columnIndex].IsPrint(); ++i) {
229 printedElement = fTable[i][columnIndex].GetRepresentation();
230 hasPrintedNext[columnIndex] =
true;
233 if (!printedElement.empty()) {
238 stringRow << std::left << std::setw(fWidths[columnIndex]) << std::setfill(fgSeparator) << printedElement
242 std::cout << stringRow.str() << std::endl;
247 std::string RDisplay::AsString()
const
251 std::stringstream stringRepresentation;
252 for (
auto row : fTable) {
253 for (
size_t i = 0; i < row.size(); ++i) {
254 stringRepresentation << std::left << std::setw(fWidths[i]) << std::setfill(fgSeparator)
255 << row[i].GetRepresentation() <<
" | ";
257 stringRepresentation <<
"\n";
259 return stringRepresentation.str();