CppTools
Loading...
Searching...
No Matches
Table.hpp
Go to the documentation of this file.
1
11#ifndef CPP_TOOLS_TABLE_HPP
12#define CPP_TOOLS_TABLE_HPP
13
14#include "OutputColor.hpp"
15#include "IOTools.hpp"
16#include "ErrorHandler.hpp"
17
19namespace CppTools
20{
47 class Table
48 {
49 public:
50
52 Table();
57 Table(const unsigned short nColumns, const int width = -1);
61 void Begin(const std::string& name);
65 template<typename... Ts>
66 void PrintHeader(Ts... args)
67 {
68 constexpr unsigned short size = sizeof...(args);
69 if (size != numberOfColumns)
70 {
71 PrintError("Table: Number of arguments is not equal to the number of columns:" +
72 std::to_string(size) + " vs " + std::to_string(numberOfColumns));
73 }
74
75 std::string dummy[size] = {(std::string) args...};
76
77 //checks whether or not next cell should be printed
78 bool check = true;
79 //number of the next cell to be printed
80 int currentCell = 0;
81
82 //size of the cell
83 cellWidth = static_cast<double>(tableWidth - 2*utf8_strlen(verticalBorderTable))/size;
84
85 //finishing the title
86 std::cout << " " << rightTableAdjLeftVerticalBorderTable;
89 {
90 if (i != 0 && i % static_cast<int>(cellWidth) == 0 &&
91 tableWidth - i >= cellWidth)
92 {
94 }
95 else std::cout << horizontalBorderTable;
96 }
97 std::cout << leftTableAdjRightVerticalBorderTable << std::endl;
98
99 //printing header
100 std::cout << " " << verticalBorderTable;
103 {
104 if (i != 0 && i % static_cast<int>(cellWidth) == 0 && tableWidth - i >= cellWidth)
105 {
106 std::cout << verticalBorderCell;
107 check = true;
108 }
109 else if (check && currentCell < size)
110 {
111 std::cout << " " << dummy[currentCell];
112 i += utf8_strlen(" " + dummy[currentCell]) - 1;
113 currentCell++;
114 check = false;
115 }
116 else std::cout << " ";
117 }
118 std::cout << verticalBorderTable << std::endl;
119
120 //printing header separator
121 std::cout << " " << rightCellAdjLeftVerticalBorderTable;
124 {
125 if (i != 0 && i % static_cast<int>(cellWidth) == 0 &&
126 tableWidth - i >= cellWidth) std::cout << cross;
127 else std::cout << horizontalCellBorder;
128 }
129 std::cout << leftCellAdjRightVerticalBorderTable << std::endl;
130 }
131
134 template<typename... Ts>
135 void PrintRow(Ts... args)
136 {
137 constexpr unsigned short size = sizeof...(args);
138
139 if (size != numberOfColumns)
140 {
141 PrintError("Number of arguments is not equal to the number of columns:" +
142 std::to_string(size) + " vs " + std::to_string(numberOfColumns));
143 }
144
145 std::string dummy[size] = {(std::string) args...};
146
147 //checks whether or not next cell shoudl be printed
148 bool check = true;
149 //number of the next cell to be printed
150 int currentCell = 0;
151
152 //printing the row
153 std::cout << " " << verticalBorderTable;
156 {
157 if (i != 0 && i % static_cast<int>(cellWidth) == 0 && tableWidth - i >= cellWidth)
158 {
159 std::cout << verticalBorderCell;
160 check = true;
161 }
162 else if (check && currentCell < size)
163 {
164 std::cout << " " << dummy[currentCell];
165 i += utf8_strlen(" " + dummy[currentCell]) - 1;
166 currentCell++;
167 check = false;
168 }
169 else std::cout << " ";
170 }
171 std::cout << verticalBorderTable << std::endl;
172 }
173
176 void End();
177
180 virtual ~Table();
181
182 protected:
183
185 std::string ULBorderCornerTable = "╔";
187 std::string URBorderCornerTable = "╗";
189 std::string horizontalBorderTable = "═";
191 std::string verticalBorderTable = "║";
193 std::string verticalBorderCell = "│";
207 std::string horizontalCellBorder = "─";
209 std::string BLBorderCornerTable = "╚";
211 std::string BRBorderCornerTable = "╝";
219 std::string cross = "┼";
220
226 double cellWidth;
227 };
228}
229
230#endif /* CPP_TOOLS_TABLE_HPP */
Contains useful set of functions to print errors, warning, check existence of files,...
void PrintError(const std::string &message, const bool exitProgram=true)
Prints error prompt.
Definition ErrorHandler.cpp:16
Contains useful set of functions to work with std::string and to convert various types in std::string...
Contains aliases for colors to use in output in terminal.
int utf8_strlen(const std::string &str)
Returns the length of the std::string type variable.
Definition StrTools.cpp:16
double cellWidth
width of one cell
Definition Table.hpp:226
void PrintRow(Ts... args)
Prints cells contents in the last row.
Definition Table.hpp:135
std::string BRBorderCornerTable
bottom right border of the table
Definition Table.hpp:211
std::string bottomCellAdjTopHorizontalBorderTable
Definition Table.hpp:214
Table()
Default constructor.
Definition Table.cpp:17
std::string URBorderCornerTable
upper right border corner of the table
Definition Table.hpp:187
std::string leftCellAdjRightVerticalBorderTable
Definition Table.hpp:199
virtual ~Table()
Default desctructor.
Definition Table.cpp:49
std::string leftTableAdjRightVerticalBorderTable
Definition Table.hpp:205
std::string topCellAdjBottomHorizontalBorderTable
Definition Table.hpp:217
std::string rightCellAdjLeftVerticalBorderTable
Definition Table.hpp:196
int numberOfColumns
number of columns in a table
Definition Table.hpp:222
std::string rightTableAdjLeftVerticalBorderTable
Definition Table.hpp:202
void End()
Prints the end of the table (bottom line that separates cell contents from other contents in terminal...
Definition Table.cpp:36
std::string horizontalCellBorder
horizontal border of cells
Definition Table.hpp:207
std::string cross
cross between cells borders
Definition Table.hpp:219
std::string horizontalBorderTable
horizontal border of the table
Definition Table.hpp:189
std::string verticalBorderTable
vertical border border of the table
Definition Table.hpp:191
int tableWidth
lenght of a row, i.e. width of a table
Definition Table.hpp:224
std::string ULBorderCornerTable
upper left border corner of the table
Definition Table.hpp:185
void Begin(const std::string &name)
Prints the top part of the table with it's name.
Definition Table.cpp:30
std::string verticalBorderCell
vertical separator between cells of the table
Definition Table.hpp:193
void PrintHeader(Ts... args)
Prints header of the table.
Definition Table.hpp:66
std::string BLBorderCornerTable
bottom left border of the table
Definition Table.hpp:209