Working on printing tables. Not done but have a framework going.

This commit is contained in:
NaifBanana 2026-02-12 01:05:33 -06:00
parent 9be6036265
commit cc0bca634f
4 changed files with 252 additions and 4 deletions

194
engine/NBCore/ANSITerm.hpp Normal file
View File

@ -0,0 +1,194 @@
#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

View File

@ -4,12 +4,13 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <iomanip>
#include <ostream> #include <ostream>
#include <thread> #include <thread>
#include <type_traits> #include <type_traits>
#include <unordered_map>
#include <vector> #include <vector>
#include "ANSITerm.hpp"
#include "DataSink.hpp" #include "DataSink.hpp"
#include "Processes.hpp" #include "Processes.hpp"
#include "ThreadSafeQueue.hpp" #include "ThreadSafeQueue.hpp"
@ -151,6 +152,13 @@ private:
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> { class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
using LoggerType = DefaultDebugLogger; using LoggerType = DefaultDebugLogger;
using Base = DebugLogger<DefaultDebugLogger>; using Base = DebugLogger<DefaultDebugLogger>;
template <typename U>
struct Field {
Field(int w, U v) : width{w}, val(v) {}
int width;
U val;
};
public: public:
using Base::Base; using Base::Base;
@ -158,18 +166,27 @@ public:
friend class BufferedDataProcessor<LogEvent, ThreadsafeQueue<LogEvent>, DefaultDebugLogger>; friend class BufferedDataProcessor<LogEvent, ThreadsafeQueue<LogEvent>, DefaultDebugLogger>;
template <typename STR, typename U>
friend STR& operator<<(STR&, const DefaultDebugLogger::Field<U>&);
protected: protected:
using Base::_ostream; using Base::_ostream;
bool process(const LogEvent& msg) { bool process(const LogEvent& msg) {
for (const auto os : this->_ostream) { for (const auto os : this->_ostream) {
(*os) << msg.pid << " - " << msg.tid << " : " << msg.msg << "\n"; (*os) << "[ " << ANSITextColor<std::string>("hey") << " | "
<< Field(2, msg.tid) << " | " << Field(10, msg.msg) << " ]\n";
} }
return true; return true;
} }
}; };
template <typename STR, typename U>
STR& operator<<(STR& stream, const DefaultDebugLogger::Field<U>& field) {
stream << std::setw(field.width) << field.val;
return stream;
}
/* class BasicLogger : public LoggerBase<BasicLogger> { /* class BasicLogger : public LoggerBase<BasicLogger> {
public: public:
using Base = LoggerBase<BasicLogger>; using Base = LoggerBase<BasicLogger>;

View File

@ -0,0 +1,33 @@
#pragma once
#ifndef _NB_TYPE_TRAITS
#define _NB_TYPE_TRAITS
#include <type_traits>
namespace nb {
template<std::size_t N=0, typename Func, typename... Pack>
inline typename std::enable_if<N==sizeof...(Pack), void>::type
ForEach(std::tuple<Pack...>&, Func) {}
template<std::size_t N=0, typename Func, typename... Pack>
inline typename std::enable_if<N < sizeof...(Pack), void>::type
ForEach(std::tuple<Pack...>& tup, Func f) {
f(N, std::get<N>(tup));
ForEach<N+1, Func, Pack...>(tup, f);
}
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
inline typename std::enable_if<N==sizeof...(Pack), void>::type
ForEach(std::tuple<Pack...>&, Func, const Args&) {}
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
inline typename std::enable_if<N < sizeof...(Pack), void>::type
ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
f(N, std::get<N>(tup), arg);
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
}
} // namespace nb
#endif // _NB_TYPE_TRAITS

View File

@ -3,6 +3,7 @@
#include "Errors.hpp" #include "Errors.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <iostream> #include <iostream>
#include <sstream>
#include "Logger.hpp" #include "Logger.hpp"
using namespace nb; using namespace nb;
@ -30,11 +31,14 @@ const ErrorCodeMap TestError::ErrorMessages{
TEST(ErrorTest, Test) { TEST(ErrorTest, Test) {
EXPECT_EQ(1, 1); EXPECT_EQ(1, 1);
nb::DefaultDebugLogger logr(std::cout, std::cout, std::cout); std::stringstream sstream;
nb::DefaultDebugLogger logr(std::cout, sstream);
ASSERT_TRUE(logr.run()); ASSERT_TRUE(logr.run());
ASSERT_TRUE(logr.isRunning()); ASSERT_TRUE(logr.isRunning());
while (!logr.isRunning()){ while (!logr.isRunning()){
1+1; 1+1;
} }
logr.log("Hey!"); logr.log("Hey!");
logr.stop();
std::cout << "STRINGSTREAM:\n" << sstream.str();
} }