Compare commits

...

6 Commits

Author SHA1 Message Date
e0eb274d8b Even better code than yesterday! 2 2025-06-30 19:47:55 -04:00
72ee2410a2 Even better code than yesterday! 2025-06-30 19:44:50 -04:00
6c0ca2ab1f More logging work 2025-06-26 20:59:57 -04:00
2b9d1c9549 SGR, logging 2025-06-26 16:37:43 -04:00
83604a8ef5 console control 2025-06-26 13:07:21 -04:00
e4420a9c59 Color 16 and text style enums 2025-06-26 10:34:06 -04:00
3 changed files with 242 additions and 115 deletions

View File

@@ -7,90 +7,183 @@
#include <stdint.h>
#include <iostream>
#include <format>
namespace Mutil::Console::Sequences {
const char ESC = 0x1b;
const char CSI = 0x9b;
const char DCS = 0x90;
const char OSC = 0x9d;
}
#include <initializer_list>
namespace Mutil::Console::General {
const char BEL = 0x07;
const char BS = 0x08;
const char HT = 0x09;
const char LF = 0x0A;
const char VT = 0x0B;
const char FF = 0x0C;
const char CR = 0x0D;
using Mutil::Console::Sequences::ESC;
const char DEL = 0x7f;
}
namespace Mutil::Console::Cursor {
class Movement {
public:
void Home() {};
void LnCol(uint64_t line, uint64_t column) {};
void Up(uint64_t lines) {};
void Down(uint64_t lines) {};
void Right(uint64_t columns) {};
void Left(uint64_t columns) {};
void NextLn(uint64_t lines) {};
void PrevLn(uint64_t lines) {};
void JmpCol(uint64_t column) {};
std::pair<uint64_t, uint64_t> Pos() {};
void Save() {};
void Restore() {};
enum Codes : char {
BEL = 0x07,
BS,
HT,
LF,
VT,
FF,
CR,
ESC = 0x1b,
DEL = 0x7f,
};
}
namespace Mutil::Console::Erase {
}
namespace Mutil::Console::Graphics {
class Format {
void Reset() {};
void Bold() {};
void ResetBold() {};
void Dim() {};
void ResetDim() {};
void Italic() {};
void ResetItalic() {};
void Underline() {};
void ResetUnderline() {};
void Blink() {};
void ResetBlink() {};
void Inverse() {};
void ResetInverse() {};
void Hidden() {};
void ResetHidden() {};
void Strikethrough() {};
void ResetStrikethrough() {};
// Rendition flags
const uint64_t RNormal = 0;
const uint64_t RBold = 1<<0;
const uint64_t RDim = 1<<1;
const uint64_t RItalic = 1<<2;
const uint64_t RUnderline = 1<<3;
const uint64_t RBlink = 1<<4;
const uint64_t RInverse = 1<<4;
const uint64_t RHidden = 1<<5;
const uint64_t RStrikethrough = 1<<6;
const uint64_t RResetBold = 1<<7;
const uint64_t RResetDim = 1<<8;
const uint64_t RResetItalic = 1<<9;
const uint64_t RResetUnderline = 1<<10;
const uint64_t RResetBlink = 1<<11;
const uint64_t RResetInverse = 1<<12;
const uint64_t RResetHidden = 1<<13;
const uint64_t RResetStrikethrough = 1<<14;
const uint64_t RColor16 = 1<<15;
const uint64_t RColor256 = 1<<16;
const uint64_t RColorRGB = 1<<17;
const uint64_t RForegroundColor = 1<<18;
const uint64_t RBackgroundColor = 1<<19;
const uint64_t RDefaultColor = 1<<20;
class Rendition {
uint64_t flags = 0;
uint32_t color = 0;
public:
Rendition() {};
Rendition(uint64_t f) : flags(f) {};
Rendition(uint64_t f, uint8_t c) : flags(f | RColor256), color(c) {
if ((flags & (RForegroundColor | RBackgroundColor)) == 0)
flags |= RForegroundColor;
};
Rendition(uint64_t f, uint8_t r, uint8_t g, uint8_t b) : flags(f | RColorRGB) {
if ((flags & (RForegroundColor | RBackgroundColor)) == 0)
flags |= RForegroundColor;
this->color = (uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b << 0;
}
std::string Compile() {
std::ostringstream s;
s << Mutil::Console::General::Codes::ESC << '[';
if (flags == RNormal) {
s << "0m";
return s.str();
}
if ((flags & RBold) == RBold)
s << "1" << ";";
if ((flags & RDim) == RDim)
s << "2" << ";";
if ((flags & RItalic) == RItalic)
s << "3" << ";";
if ((flags & RUnderline) == RUnderline)
s << "4" << ";";
if ((flags & RBlink) == RBlink)
s << "5" << ";";
if ((flags & RInverse) == RInverse)
s << "7" << ";";
if ((flags & RHidden) == RHidden)
s << "8" << ";";
if ((flags & RStrikethrough) == RStrikethrough)
s << "9" << ";";
if ((flags & RResetBold) == RResetBold)
s << "22" << ";";
if ((flags & RResetDim) == RResetDim)
s << "22" << ";";
if ((flags & RResetItalic) == RResetItalic)
s << "23" << ";";
if ((flags & RResetUnderline) == RResetUnderline)
s << "24" << ";";
if ((flags & RResetBlink) == RResetBlink)
s << "25" << ";";
if ((flags & RResetInverse) == RResetInverse)
s << "27" << ";";
if ((flags & RResetHidden) == RResetHidden)
s << "28" << ";";
if ((flags & RResetStrikethrough) == RResetStrikethrough)
s << "29" << ";";
if ((flags & RForegroundColor) == RForegroundColor)
s << "38" << ";";
if ((flags & RBackgroundColor) == RBackgroundColor)
s << "48" << ";";
if ((flags & RColor256) == RColor256)
s << "5" << ";" << std::to_string(color) << ";";
if ((flags & RColorRGB) == RColorRGB)
s << "2" << ";" << std::to_string((color >> 16) & 0x000000ff) << ";" << std::to_string((color >> 8) & 0x000000ff) << ";" << std::to_string((color >> 0) & 0x000000ff) << ";";
if ((flags & (RDefaultColor | RForegroundColor)) == (RDefaultColor | RForegroundColor))
s << "39" << ";";
if ((flags & (RDefaultColor | RBackgroundColor)) == (RDefaultColor | RBackgroundColor))
s << "49" << ";";
std::string c = s.str();
c.back() = 'm';
return c;
}
};
// Color 16
/* const uint8_t Black = 30;
const uint8_t Red = 31;
const uint8_t Green = 32;
const uint8_t Yellow = 33;
const uint8_t Blue = 34;
const uint8_t Magenta = 35;
const uint8_t Cyan = 36;
const uint8_t White = 37;
const uint8_t Default = 39;
const uint8_t BlackBG = 40;
const uint8_t
const uint8_t
const uint8_t
const uint8_t
const uint8_t
const uint8_t
}
class Color16 {
namespace Mutil::Console::Functions {
void CursorUp(uint n) {};
void CursorDown(uint n) {};
void CursorForward(uint n) {};
void CursorBack(uint n) {};
void CursorNextLine(uint n) {};
void CursorPreviousLine(uint n) {};
void CursorHorizontalAbsolute(uint n) {};
void CursorPosition(uint n, uint m) {};
void RequestCursorPosition() {};
void EraseInDisplay(uint n) {};
void EraseInLine(uint n) {};
void ScrollUp(uint n) {};
void ScrollDown(uint n) {};
void SaveCursorPosition() {};
void RestoreCursorPosition() {};
};*/
/* Yucky
void SelectGraphicsRendition(std::initializer_list<uint8_t> args) {
if (empty(args)) { return; };
std::stringstream s;
s << Mutil::Console::General::Codes::ESC << '[';
for (uint8_t a : args) {
//std::cout << (uint16_t)a << std::endl;
if (a == *std::prev(args.end())) {
s << std::to_string(a);
break;
}
s << std::to_string(a) << ';';
}
s << 'm';
std::cout << s.str();
};
*/
}

View File

@@ -5,61 +5,85 @@
#pragma once
#include <iostream>
#include <sstream>
#include <functional>
#include <format>
namespace Mutil::Logging {
namespace Mutil::Logging {
/// Logger object for constructing loggers.
class LoggerObj {
class LoggerObj : public std::ostream {
private:
/// Output stream, default std::clog.
std::ostream &os = std::clog;
/// Lambda formatting function, primarily for storing a format string in a versatile way, users allowed to supply their own.
std::function<std::string(void)> λ;
std::function<std::string()> λPrefix;
//std::function<std::string(const std::string_view)> λMessage;
std::function<std::string(const std::string)> λMessage;
std::function<std::string()> λSuffix;
public:
/// Default constructor, just simple messages to be written to console
LoggerObj() {};
/// Construct with specified output stream
LoggerObj(std::ostream &os);
/// Construct with a custom, formatted prefix applied to every message, such as "* Mutil Logging >> message".
template<typename... Args>
LoggerObj(const std::format_string<Args...> fmt, Args&&... args) {
λ = [fmt, args...](){
return std::vformat(fmt.get(), std::make_format_args(args...));
};
}
/// Construct with a custom lambda formatting function
LoggerObj(std::function<std::string(void)> λ) : λ(λ) {};
/// Construct with a specified output stream and custom formatting function
LoggerObj(std::ostream &os, std::function<std::string(void)> λ) : λ(λ) {};
public:
/// If provided only runs formatting function, formatting function may retrieve data for its own messages.
void operator()() {
λ && os << λ();
}
/// Full construction
LoggerObj(std::ostream &os,
std::function<std::string()> λprefix,
std::function<std::string(const std::string)> λmessage,
std::function<std::string()> λsuffix
) : os(os), λPrefix(λprefix), λMessage(λmessage), λSuffix(λsuffix) {};
public:
/// Send a simple log message
void operator()(const std::string_view message) {
(λ && os << λ() << message << '\n') ||
os << message << '\n';
//void operator()(const std::string_view message) {
void operator()(const std::string message) {
//std::ostringstream oss;
λPrefix && os << λPrefix();
(λMessage && os << λMessage(message)) || os << message;
λSuffix && os << λSuffix();
//os << oss.str() << std::endl;
os << std::endl;
}
/// Send a formatted/complex log message
template<typename... Args>
void operator()(const std::format_string<Args...> fmt, Args&&... args) {
(λ && os << λ() << std::vformat(fmt.get(), std::make_format_args(args...)) << '\n') ||
os << std::vformat(fmt.get(), std::make_format_args(args...)) << '\n';
//std::ostringstream oss;
std::string m = std::vformat(fmt.get(), std::make_format_args(args...));
λPrefix && os << λPrefix();
(λMessage && os << λMessage(m)) || os << m;
λSuffix && os << λSuffix();
//os << oss.str() << std::endl;
os << std::endl;
};
/// Send log messages using logger as a stream
template<typename In>
std::ostream& operator<<(In message) {
(λ && os << λ() << message) ||
os << message;
return os;
LoggerObj& operator<<(In message) {
//std::ostringstream oss;
//oss << message;
//operator()(oss.str());
//(λ && os << λ() << message) ||
//os << message;
//return os;
λPrefix && os << λPrefix();
(λMessage && os << λMessage(message)) || os << message;
λSuffix && os << λSuffix();
os << std::endl;
return *this;
};
};
/*
class Info : public LoggerObj {
public:
Info() : LoggerObj("[I] | ") {};
@@ -84,5 +108,6 @@ namespace Mutil::Logging {
public:
Debug() : LoggerObj("[D] | ") {};
};
*/
}

View File

@@ -1,18 +1,27 @@
#include <Logging.hpp>
#include <Desktop.hpp>
#include <Console.hpp>
int main() {
Mutil::Logging::LoggerObj lo;
lo("Hello world!!!!");
Mutil::Logging::Info I;
Mutil::Logging::Error E;
Mutil::Logging::Verbose V;
Mutil::Logging::Debug D;
I("Something...");
E("Something...");
V("Something...");
D("Something...");
std::cout << (char)0x1b << (char)0x5b << "33m" << "BULLOCKS" << '\n';
//std::cout << 0x1b5b << "33m" << "BALLS" << '\n';
Desktop::Browser::OpenURL("https://git.redacted.cc/maxine/mutil");
std::function<std::string()> λprefix = [] {
return Mutil::Console::Graphics::Rendition(Mutil::Console::Graphics::RItalic, 252, 186, 3).Compile() + "--> " + Mutil::Console::Graphics::Rendition(Mutil::Console::Graphics::RNormal).Compile();
};
std::function<std::string(const std::string)> λmessage = [](const std::string m) {
return Mutil::Console::Graphics::Rendition(Mutil::Console::Graphics::RUnderline, 74).Compile() + m + Mutil::Console::Graphics::Rendition(Mutil::Console::Graphics::RNormal).Compile();
};
//std::function<std::string()> λsuffix = [] {
// return " 0";
//};
Mutil::Logging::LoggerObj l(std::cout, λprefix, λmessage, nullptr);
l("Hello world");
Mutil::Console::Graphics::Rendition r(Mutil::Console::Graphics::RItalic, 252, 186, 3);
std::string c = r.Compile();
std::cout << c << "ass" << std::endl;
std::cout << Mutil::Console::Graphics::Rendition(Mutil::Console::Graphics::RBold | Mutil::Console::Graphics::RItalic, 160, 186, 3).Compile() << "SHIIIIIIT" << Mutil::Console::Graphics::Rendition(Mutil::Console::Graphics::RNormal).Compile() << std::endl;
//Desktop::Browser::OpenURL("https://git.redacted.cc/maxine/mutil");
//Mutil::Console::Graphics::SGARG a = Mutil::Console::Graphics::TextStyle::Underline
}