214 lines
5.8 KiB
C++
214 lines
5.8 KiB
C++
#pragma once
|
|
#ifndef _NB_ANSI_TERM
|
|
#define _NB_ANSI_TERM
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <type_traits>
|
|
|
|
/*
|
|
----------- TECH DEBT ------------
|
|
Idk wtf to do here. This was originally to allow me to print
|
|
to terminal easily, but it's becoming too much of an investment.
|
|
For the time being, drop it.
|
|
|
|
*/
|
|
|
|
namespace nb {
|
|
|
|
template <typename ST, typename U>
|
|
struct SmartText {
|
|
using Type = ST;
|
|
using ValType = U;
|
|
static constexpr bool HasChild = Type::HasChild;
|
|
|
|
SmartText() = default;
|
|
};
|
|
|
|
template <typename Type, typename U>
|
|
struct StyleFormatter : SmartText<StyleFormatter<Type, U>, U> {
|
|
using Base = SmartText<StyleFormatter<Type, U>, U>;
|
|
using typename Base::ValType;
|
|
static constexpr bool HasChild = true;
|
|
|
|
StyleFormatter(const std::string& _fmt, const ValType& _val, const std::string& _closing) :
|
|
fmt{_fmt}, val{_val}, closing{_closing} {}
|
|
|
|
std::string fmt;
|
|
ValType val;
|
|
std::string closing;
|
|
};
|
|
|
|
template <typename Child>
|
|
struct StyleFormatter<Child, void> : SmartText<StyleFormatter<Child, void>, void>{
|
|
using Base = SmartText<StyleFormatter<Child, void>, void>;
|
|
using Base::ValType;
|
|
static constexpr bool HasChild = false;
|
|
|
|
StyleFormatter(const std::string& _fmt) : fmt{_fmt} {}
|
|
|
|
std::string fmt;
|
|
};
|
|
|
|
template <typename U=void>
|
|
struct ANSITextColor;
|
|
|
|
template <typename U>
|
|
struct ANSITextColor : StyleFormatter<ANSITextColor<U>, U> {
|
|
using Base = StyleFormatter<ANSITextColor, U>;
|
|
using typename Base::ValType;
|
|
using Base::HasChild;
|
|
|
|
template <typename = typename std::enable_if<HasChild, U>::type>
|
|
ANSITextColor(const U& _val) : Base{"\x1b[39m", _val, "\x1b[39m"} {}
|
|
template <typename = typename std::enable_if<!HasChild, U>>
|
|
ANSITextColor() : Base{"\x1b[92m"} {}
|
|
};
|
|
|
|
template <>
|
|
struct ANSITextColor<std::string> : StyleFormatter<ANSITextColor<std::string>, std::string> {
|
|
using Base = StyleFormatter<ANSITextColor<std::string>, std::string>;
|
|
static constexpr bool HasChild = Base::HasChild;
|
|
|
|
template <std::size_t N>
|
|
ANSITextColor(char const(&_val) [N]) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
|
ANSITextColor(const char* _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
|
ANSITextColor(const std::string& _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
|
};
|
|
|
|
} // namespace nb
|
|
|
|
template<typename Type, typename U>
|
|
std::ostream& operator<<(std::ostream& stream, const nb::StyleFormatter<Type, U>& msg) {
|
|
if (&stream == &std::cout) {
|
|
stream << msg.fmt;
|
|
if (msg.HasChild) {
|
|
stream << msg.val << msg.closing;
|
|
}
|
|
} else {
|
|
if (msg.HasChild) { stream << msg.val; }
|
|
}
|
|
stream << std::flush;
|
|
return stream;
|
|
}
|
|
|
|
/* struct CellBase {
|
|
uint8_t width;
|
|
uint8_t height;
|
|
};
|
|
|
|
template <typename T>
|
|
struct Cell : SmartText<Cell<T>, T> {
|
|
using Base = SmartText<Cell<T>, T>;
|
|
using typename Base::ValType;
|
|
using Base::HasChild;
|
|
|
|
Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=1)
|
|
: val(_val), width(w), height(h) {}
|
|
|
|
ValType val;
|
|
uint8_t width;
|
|
|
|
uint8_t height;
|
|
};
|
|
|
|
template <>
|
|
struct Cell<std::string> : SmartText<Cell<std::string>, std::string> {
|
|
using Base = SmartText<Cell<std::string>, std::string>;
|
|
using typename Base::ValType;
|
|
using Base::HasChild;
|
|
|
|
template <std::size_t N>
|
|
Cell( const(&_val) [N], const uint8_t& w, const uint8_t& h=0) : Cell(std::string(_val), w, h) {}
|
|
Cell(char* _val, const uint8_t& w, const uint8_t& h=0) : Cell(std::string(_val), w, h) {}
|
|
Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=0)
|
|
: val(_val), width(w) {
|
|
unsigned int strsize = _val.size();
|
|
height = strsize / width;
|
|
height += (strsize % width) ? 1 : 0;
|
|
}
|
|
|
|
ValType val;
|
|
uint8_t width;
|
|
|
|
uint8_t height;
|
|
};
|
|
|
|
template <typename T>
|
|
std::ostream& operator<<(std::ostream& stream, const Cell<T>& cll) {
|
|
if (&stream == &std::cout) {
|
|
stream << "\x1b[s" << cll.val << "\x1b[u\x1b[" << 1+cll.width << "C\x1b[" << 1+cll.height;
|
|
}
|
|
return stream;
|
|
}
|
|
|
|
template <typename T>
|
|
void ChangeCellHeight(int i, Cell<T>& cll, const unsigned int& h) {
|
|
if (h < cll.height) { cll.height=h; }
|
|
}
|
|
|
|
template <typename T>
|
|
void GetTallestCell(int i, Cell<T>& cll, unsigned int& h) {
|
|
if (cll.height > h) { h = cll.height; }
|
|
}
|
|
|
|
template <typename... Pack>
|
|
struct TableRow {
|
|
using RowCells = std::tuple<Cell<Pack>...>;
|
|
using CellWidths = std::array<int, sizeof...(Pack)>;
|
|
using DelimiterArray = std::array<std::string, sizeof...(Pack)+1>;
|
|
|
|
TableRow(CellWidths&& widths_, DelimiterArray&& delims_, const Pack&... vals_)
|
|
: widths(widths_), delimiters(delims_), topBorder("") {
|
|
nb::ForEach(_cells, GetTallestCell, _height);
|
|
}
|
|
|
|
static constexpr int num_columns = sizeof...(Pack);
|
|
|
|
int height() const { return height; }
|
|
inline int height(const unsigned int newh) {
|
|
nb::ForEach(_cells, ChangeCellHeight, newh);
|
|
return (_height=newh);
|
|
}
|
|
|
|
const CellWidths widths;
|
|
const DelimiterArray delimiters;
|
|
const std::string topBorder;
|
|
const std::string bottomBorder;
|
|
|
|
protected:
|
|
RowCells _cells;
|
|
int _height;
|
|
|
|
};
|
|
|
|
template <typename... Pack>
|
|
std::ostream& operator<<(std::ostream& stream, const TableRow<Pack...>& row) {
|
|
|
|
}
|
|
|
|
template<typename... Pack>
|
|
class Table {
|
|
public:
|
|
Table(const Pack&... _vals) : row(std::array<int, sizeof...(Pack)>(10),
|
|
std::array<std::string, sizeof...(Pack)>(""), _vals...) {}
|
|
private:
|
|
TableRow<Pack...> row;
|
|
};
|
|
|
|
} // namespace nb
|
|
|
|
#ifdef _NB_TARGET_WINDOWS
|
|
template<typename... Types>
|
|
std::ostream& operator<<(std::ostream& stream, const nb::TableRow<Types...>& row) {
|
|
using PosType = decltype(stream.tellp());
|
|
constexpr int num_cells = sizeof...(Types);
|
|
|
|
int col_num = 0;
|
|
int col_start = 0;
|
|
|
|
return stream;
|
|
}
|
|
#endif // _NB_TARGET_WINDOWS */
|
|
|
|
#endif // _NB_ANSI_TERM
|