2023-08-31 17:35:12 +02:00
|
|
|
/* Print fancy tables.
|
2018-08-22 11:29:39 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-04-07 15:12:32 +02:00
|
|
|
#include <string>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <vector>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2021-02-16 14:15:38 +01:00
|
|
|
#include <villas/log.hpp>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
|
2019-04-07 15:12:32 +02:00
|
|
|
class Table;
|
|
|
|
|
|
|
|
class TableColumn {
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
friend Table;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-04-07 15:12:32 +02:00
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
enum class Alignment { LEFT, RIGHT };
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-04-07 15:12:32 +02:00
|
|
|
protected:
|
2023-09-07 13:19:19 +02:00
|
|
|
int _width; // The real width of this column. Calculated by Table::resize().
|
2019-04-07 15:12:32 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int width; // Width of the column.
|
2019-04-07 15:12:32 +02:00
|
|
|
|
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
TableColumn(int w, enum Alignment a, const std::string &t,
|
|
|
|
const std::string &f, const std::string &u = "")
|
|
|
|
: _width(0), width(w), title(t), format(f), unit(u), align(a) {}
|
|
|
|
|
|
|
|
std::string title; // The title as shown in the table header.
|
|
|
|
std::string format; // The format which is used to print the table rows.
|
|
|
|
std::string unit; // An optional unit which will be shown in the table header.
|
|
|
|
|
|
|
|
enum Alignment align;
|
|
|
|
|
|
|
|
int getWidth() const { return _width; }
|
2018-08-22 11:29:39 +02:00
|
|
|
};
|
|
|
|
|
2019-04-07 15:12:32 +02:00
|
|
|
class Table {
|
|
|
|
|
|
|
|
protected:
|
2023-09-07 13:19:19 +02:00
|
|
|
int resize(int w);
|
2019-04-07 15:12:32 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
int width;
|
2019-06-23 16:12:53 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::vector<TableColumn> columns;
|
2019-06-23 16:12:53 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
Logger logger;
|
2021-02-16 14:15:38 +01:00
|
|
|
|
2019-04-07 15:12:32 +02:00
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
Table(Logger log, const std::vector<TableColumn> &cols)
|
|
|
|
: width(-1), columns(cols), logger(log) {}
|
|
|
|
|
|
|
|
// Print a table header consisting of \p n columns.
|
|
|
|
void header();
|
|
|
|
|
|
|
|
// Print table rows.
|
|
|
|
void row(int count, ...);
|
|
|
|
|
|
|
|
int getWidth() const { return width; }
|
2019-04-07 15:12:32 +02:00
|
|
|
};
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace villas
|