NBEngine/engine/NBCore/ANSITerm.hpp

194 lines
5.1 KiB
C++

#pragma once
#ifndef _NB_ANSI_TERM
#define _NB_ANSI_TERM
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <tuple>
#include <type_traits>
#include "TypeTraits.hpp"
namespace nb {
template <typename ST, typename U>
struct SmartText {
using Type = ST;
using ValType = U;
static constexpr bool HasChild = Type::HasChild;
};
template <typename Type, typename U>
struct StyleFormatter : SmartText<StyleFormatter<Type, U>, U> {
using Base = SmartText<StyleFormatter<Type, U>>;
using Base::Type;
using Base::ValType;
static constexpr bool HasChild = true;
std::string fmt;
ValType val;
std::string closing;
};
template <typename Child>
struct StyleFormatter<Child, void> : SmartText<StyleFormatter<Child, void>, void>{
using Base = SmartText<StyleFormatter>;
using Base::Type;
using Base::ValType;
static constexpr bool HasChild = false;
std::string fmt;
};
template <typename U=void>
struct ANSITextColor;
template <typename U>
struct ANSITextColor : StyleFormatter<ANSITextColor<U>, U> {
using Base = StyleFormatter<ANSITextColor, U>;
using 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"} {}
};
struct CellBase {
uint8_t width;
uint8_t height;
};
template <typename T>
struct Cell : SmartText<Cell<T>, T> {
using Base = SmartText<Cell<T>, T>;
using Base::ValType
using Base::HasChild;
Cell(const Type& _val, const uint8_t& w, const uint8_t& h=1)
: val(_val), width(w), height(h) {}
Type 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 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) {
}
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_) {
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);
}
protected:
const CellWidths _widths;
const DelimiterArray _delimiters;
RowCells _cells;
int _height;
};
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
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;
}
#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