MCPcopy Create free account
hub / github.com/Compaile/ctrack / BeautifulTable

Class BeautifulTable

include/ctrack.hpp:196–428  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

194 };
195
196 class BeautifulTable
197 {
198 private:
199 std::vector<std::pair<std::string, int>> top_header;
200 std::vector<std::string> header;
201 std::vector<std::vector<std::string>> rows;
202 std::vector<size_t> columnWidths;
203 bool useColor;
204 ColorScheme colors;
205 static inline const std::string RESET_COLOR = "\033[0m";
206
207 void updateColumnWidths(const std::vector<std::string> &row)
208 {
209 for (size_t i = 0; i < row.size(); ++i)
210 {
211 if (i >= columnWidths.size())
212 {
213 columnWidths.push_back(row[i].length());
214 }
215 else
216 {
217 columnWidths[i] = std::max<size_t>(columnWidths[i], row[i].length());
218 }
219 }
220 }
221
222 template <typename StreamType>
223 void printHorizontalLine(StreamType &stream) const
224 {
225 if (useColor)
226 stream << colors.border_color;
227 stream << "+";
228 for (size_t width : columnWidths)
229 {
230 stream << std::string(width + 2, '-') << "+";
231 }
232 if (useColor)
233 stream << RESET_COLOR;
234 stream << "\n";
235 }
236
237 template <typename StreamType>
238 void printRow(StreamType &stream, const std::vector<std::string> &row, const std::string &color, bool center = false) const
239 {
240 if (useColor)
241 stream << colors.border_color;
242 stream << "|";
243 if (useColor)
244 stream << RESET_COLOR << color;
245 for (size_t i = 0; i < row.size(); ++i)
246 {
247 if (center)
248 {
249 size_t padding = columnWidths[i] - row[i].length();
250 size_t leftPadding = padding / 2;
251 size_t rightPadding = padding - leftPadding;
252 stream << std::string(leftPadding + 1, ' ') << row[i] << std::string(rightPadding + 1, ' ');
253 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected