247 lines
7.6 KiB
C++
247 lines
7.6 KiB
C++
//
|
|
// Created by maxine on 6/16/25.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <stdint.h>
|
|
#include <iostream>
|
|
#include <format>
|
|
#include <initializer_list>
|
|
|
|
namespace Mutil::Console::General {
|
|
enum Codes : char {
|
|
BEL = 0x07,
|
|
BS,
|
|
HT,
|
|
LF,
|
|
VT,
|
|
FF,
|
|
CR,
|
|
ESC = 0x1b,
|
|
DEL = 0x7f,
|
|
};
|
|
}
|
|
|
|
namespace Mutil::Console::Graphics {
|
|
// 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<<5;
|
|
const uint64_t RHidden = 1<<6;
|
|
const uint64_t RStrikethrough = 1<<7;
|
|
const uint64_t RResetBold = 1<<8;
|
|
const uint64_t RResetDim = 1<<9;
|
|
const uint64_t RResetItalic = 1<<10;
|
|
const uint64_t RResetUnderline = 1<<11;
|
|
const uint64_t RResetBlink = 1<<12;
|
|
const uint64_t RResetInverse = 1<<13;
|
|
const uint64_t RResetHidden = 1<<14;
|
|
const uint64_t RResetStrikethrough = 1<<15;
|
|
const uint64_t RColor16 = 1<<16;
|
|
const uint64_t RColor256 = 1<<17;
|
|
const uint64_t RColorRGB = 1<<18;
|
|
const uint64_t RForegroundColor = 1<<19;
|
|
const uint64_t RBackgroundColor = 1<<20;
|
|
const uint64_t RDefaultColor = 1<<21;
|
|
|
|
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;
|
|
}
|
|
};
|
|
|
|
|
|
class String {
|
|
std::string str;
|
|
Rendition rendition;
|
|
public:
|
|
String(std::string s) : str(s) {}
|
|
String(std::string s, Rendition r) : str(s), rendition(r) {};
|
|
void Style(Rendition r) { rendition = r;};
|
|
std::string Render() { return rendition.Compile() + str + Rendition(RNormal).Compile(); };
|
|
std::string Render(Rendition r) {
|
|
Style(r);
|
|
return Render();
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
namespace Mutil::Console::Functions {
|
|
void call(std::ostream &os, const std::string& arg, const char& func) {
|
|
std::ostringstream s;
|
|
s << General::Codes::ESC << '[' << arg << func;
|
|
os << s.str();
|
|
}
|
|
|
|
class Display {
|
|
std::ostream &os = std::cout;
|
|
public:
|
|
Display() {};
|
|
Display(std::ostream &os) : os(os) {};
|
|
void A(uint n = 1) { call(os, std::to_string(n), 'A'); };
|
|
void B(uint n = 1) { call(os, std::to_string(n), 'B'); };
|
|
void C(uint n = 1) { call(os, std::to_string(n), 'C'); };
|
|
void D(uint n = 1) { call(os, std::to_string(n), 'D'); };
|
|
void E(uint n = 1) { call(os, std::to_string(n), 'E'); };
|
|
void F(uint n = 1) { call(os, std::to_string(n), 'F'); };
|
|
void G(uint n = 1) { call(os, std::to_string(n), 'G'); };
|
|
void H(uint n = 0, uint m = 0) { call(os, std::format("{};{}",n,m), 'H'); };
|
|
void J(uint n = 0) { call(os, std::to_string(n), 'J'); };
|
|
void K(uint n = 0) { call(os, std::to_string(n), 'K'); };
|
|
void S(uint n = 1) { call(os, std::to_string(n), 'S'); };
|
|
void T(uint n = 1) { call(os, std::to_string(n), 'T'); };
|
|
void s() { os << General::Codes::ESC << "7"; };
|
|
void u() { os << General::Codes::ESC << "8"; };
|
|
void m(Graphics::Rendition r) { os << r.Compile(); };
|
|
//Display& Write(const char* in) {
|
|
// os << in;
|
|
// return *this;
|
|
//}
|
|
|
|
template<typename In>
|
|
friend std::ostream& operator<<(Display& d, In in);
|
|
};
|
|
|
|
template<typename In>
|
|
std::ostream& operator<<(Display& d, In in) {
|
|
return d.os << in;
|
|
}
|
|
|
|
|
|
std::string CursorUp(uint n) { return std::format("{}[{}A", "\x1b[", 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) {};
|
|
std::string CursorPosition(uint n, uint m) { return std::format("\x1b[{};{}H", n, 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();
|
|
};
|
|
*/
|
|
} |