173 lines
4.7 KiB
C++
173 lines
4.7 KiB
C++
#pragma once
|
|
#include <cstring>
|
|
#include <functional>
|
|
#include <iomanip>
|
|
#include <ostream>
|
|
#include <tuple>
|
|
#include <vector>
|
|
|
|
namespace bts::core {
|
|
|
|
class TableFormatter {
|
|
public:
|
|
template <class T>
|
|
using Schema =
|
|
std::vector<std::tuple<std::string, int, std::function<std::string(T)>>>;
|
|
|
|
template <class T, class Range>
|
|
static std::ostream &format(
|
|
std::ostream &out, const Range &range, const Schema<T> &schema);
|
|
|
|
template <class T, class TIter>
|
|
static std::ostream &format(
|
|
std::ostream &out, TIter first, TIter last, const Schema<T> &schema);
|
|
|
|
template <class T>
|
|
static std::ostream &format_record(
|
|
std::ostream &out,
|
|
const T &record,
|
|
const Schema<T> &schema,
|
|
bool headers = false);
|
|
|
|
template <class TRow>
|
|
class SchemaBuilder {
|
|
public:
|
|
Schema<TRow> result;
|
|
|
|
template <class GetCell>
|
|
void add(std::string name, GetCell get_cell, int width = 0);
|
|
};
|
|
};
|
|
|
|
template <class TRow>
|
|
template <class GetCell>
|
|
inline void TableFormatter::SchemaBuilder<TRow>::add(
|
|
std::string name, GetCell get_cell, int width) {
|
|
std::function<std::string(TRow)> get_cell_str = [&get_cell](const TRow &r) {
|
|
std::stringstream ss;
|
|
ss << get_cell(r);
|
|
return ss.str();
|
|
};
|
|
result.emplace_back(std::move(name), width, get_cell_str);
|
|
}
|
|
|
|
template <class T, class Range>
|
|
std::ostream &TableFormatter::format(
|
|
std::ostream &out, const Range &range, const Schema<T> &schema) {
|
|
return format(out, range.begin(), range.end(), schema);
|
|
}
|
|
|
|
template <class T, class TIter>
|
|
inline std::ostream &TableFormatter::format(
|
|
std::ostream &out,
|
|
TIter first,
|
|
TIter last,
|
|
const typename TableFormatter::Schema<T> &schema) {
|
|
if (first == last || schema.size() == 0)
|
|
return out;
|
|
int total_width = 0;
|
|
for (auto col : schema) {
|
|
total_width += std::get<1>(col);
|
|
}
|
|
// top border of table
|
|
for (int i = 0; i < total_width + static_cast<int>(schema.size()); i++) {
|
|
out << "-";
|
|
}
|
|
out << std::endl;
|
|
// column headers themselves
|
|
out << "|";
|
|
for (auto col : schema) {
|
|
out << std::setw(std::get<1>(col)) << std::get<0>(col) << "|";
|
|
}
|
|
// line between column headers and rows
|
|
out << std::endl;
|
|
for (int i = 0; i < total_width + static_cast<int>(schema.size()); i++) {
|
|
out << "-";
|
|
}
|
|
// rows
|
|
while (first != last) {
|
|
out << std::endl;
|
|
out << "|";
|
|
for (uint32_t i = 0; i < schema.size(); i++) {
|
|
out << std::setw(std::get<1>(schema[i]))
|
|
<< std::get<2>(schema[i])(*first) << "|";
|
|
}
|
|
first++;
|
|
}
|
|
// bottom border of table
|
|
out << std::endl;
|
|
for (int i = 0; i < total_width + static_cast<int>(schema.size()); i++)
|
|
out << "-";
|
|
return out;
|
|
}
|
|
|
|
template <class T>
|
|
inline std::ostream &TableFormatter::format_record(
|
|
std::ostream &out,
|
|
const T &record,
|
|
const typename TableFormatter::Schema<T> &schema,
|
|
bool headers) {
|
|
constexpr const char *const field_header = "Field";
|
|
constexpr const char *const value_header = "Value";
|
|
|
|
int ulabel_width = headers ? strlen(field_header) : 0;
|
|
for (const auto &field : schema) {
|
|
auto label_width = std::get<1>(field);
|
|
if (label_width == 0) {
|
|
label_width = static_cast<int>(std::get<0>(field).size());
|
|
}
|
|
if (label_width > ulabel_width) {
|
|
ulabel_width = label_width;
|
|
}
|
|
}
|
|
|
|
std::vector<std::string> field_vals;
|
|
for (const auto &field : schema) {
|
|
field_vals.push_back(std::get<2>(field)(record));
|
|
}
|
|
|
|
int uvalue_width = headers ? strlen(value_header) : 0;
|
|
for (const auto &v : field_vals) {
|
|
auto value_width = static_cast<int>(v.size());
|
|
if (value_width > uvalue_width) {
|
|
uvalue_width = value_width;
|
|
}
|
|
}
|
|
|
|
int table_width = ulabel_width + uvalue_width + 4;
|
|
if (headers) {
|
|
// top border of table
|
|
for (int i = 0; i < table_width + 3; i++) {
|
|
out << "-";
|
|
}
|
|
out << std::endl;
|
|
// column headers
|
|
out << "| " << std::setw(ulabel_width) << field_header << " | "
|
|
<< std::setw(uvalue_width) << value_header << " |\n";
|
|
}
|
|
|
|
// border between col headers and rows (or top border of table, if 'headers'
|
|
// is false)
|
|
for (int i = 0; i < table_width + 3; i++) {
|
|
out << "-";
|
|
}
|
|
|
|
// rows
|
|
for (std::size_t i = 0; i < field_vals.size(); i++) {
|
|
out << "\n";
|
|
const auto &label = std::get<0>(schema.at(i));
|
|
const auto &val = field_vals.at(i);
|
|
out << "| " << std::setw(ulabel_width) << label << " | "
|
|
<< std::setw(uvalue_width) << val << " |";
|
|
}
|
|
out << "\n";
|
|
|
|
// bottom border of table
|
|
for (int i = 0; i < table_width + 3; i++) {
|
|
out << "-";
|
|
}
|
|
return out;
|
|
}
|
|
|
|
} // namespace bts::core
|