Change AnsiEscapeCodes to static const, should compile on MSVC.

This commit is contained in:
2025-06-01 03:48:28 -05:00
parent 18289d321c
commit 36e180b6c0
2 changed files with 130 additions and 111 deletions

View File

@@ -54,180 +54,151 @@ namespace mcolor
BG_BRIGHT_WHITE = 107,
};
// TODO: Refactor to constexpr string literals
#ifndef _MSC_VER
constexpr std::string AnsiEscapePrefix = "\033[";
static const std::string AnsiEscapePrefix = "\033[";
namespace AnsiEscapeCodes {
#pragma region General
constexpr std::string Bell = "\a";
constexpr std::string Backspace = "\b";
constexpr std::string Tab = "\t";
constexpr std::string Newline = "\n";
constexpr std::string VerticalTab = "\v";
constexpr std::string Newpage = "\f";
constexpr std::string Return = "\r";
constexpr std::string Escape = "\033";
static const std::string Bell = "\a";
static const std::string Backspace = "\b";
static const std::string Tab = "\t";
static const std::string Newline = "\n";
static const std::string VerticalTab = "\v";
static const std::string Newpage = "\f";
static const std::string Return = "\r";
static const std::string Escape = "\033";
#pragma endregion
#pragma region Cursor Controls
/// Moves cursor to home position {0, 0}.
constexpr std::string CursorHome = AnsiEscapePrefix + "H";
static const std::string CursorHome = AnsiEscapePrefix + "H";
/// Moves cursor to line Y, column X.
constexpr std::string CursorTo(int line, int column) {
return AnsiEscapePrefix + std::format("{};{}H", line, column);
}
static std::string CursorTo(int line, int column);
/// Moves the cursor up # lines.
constexpr std::string CursorUp(int lines) {
return AnsiEscapePrefix + std::format("{}A", lines);
}
static std::string CursorUp(int lines);
/// Moves the cursor down # lines.
constexpr std::string CursorDown(int lines) {
return AnsiEscapePrefix + std::format("{}B", lines);
}
static std::string CursorDown(int lines);
/// Moves the cursor right # lines.
constexpr std::string CursorRight(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
static std::string CursorRight(int lines);
/// Moves the cursor left # lines.
constexpr std::string CursorLeft(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
static std::string CursorLeft(int lines);
/// Moves cursor to the beginning of line, # lines down from the current line.
constexpr std::string CursorNextLineStart(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
static std::string CursorNextLineStart(int lines);
/// Moves cursor to the beginning of line, # lines up from the current line.
constexpr std::string CursorPrevLineStart(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
static std::string CursorPrevLineStart(int lines);
/// Moves cursor to column #.
constexpr std::string CursorColumn(int column) {
return AnsiEscapePrefix + std::format("{}G", column);
}
static std::string CursorColumn(int column);
/// Save current cursor position.
constexpr std::string CursorSave = AnsiEscapePrefix + "7";
static const std::string CursorSave = AnsiEscapePrefix + "7";
/// Restore cursor to last saved position.
constexpr std::string CursorRestore = AnsiEscapePrefix + "8";
static const std::string CursorRestore = AnsiEscapePrefix + "8";
#pragma endregion
#pragma region Erase Functions
constexpr std::string EraseInDisplay = AnsiEscapePrefix + "J";
static const std::string EraseInDisplay = AnsiEscapePrefix + "J";
/// Erase from cursor until end of screen.
constexpr std::string EraseAfterCursor = AnsiEscapePrefix + "0J";
static const std::string EraseAfterCursor = AnsiEscapePrefix + "0J";
/// Erase from cursor to beginning of screen.
constexpr std::string EraseBeforeCursor = AnsiEscapePrefix + "1J";
constexpr std::string EraseEntireScreen = AnsiEscapePrefix + "2J";
constexpr std::string EraseSavedLines = AnsiEscapePrefix + "3J";
constexpr std::string EraseInLine = AnsiEscapePrefix + "K";
constexpr std::string EraseLineAfterCursor = AnsiEscapePrefix + "0K";
constexpr std::string EraseLineBeforeCursor = AnsiEscapePrefix + "1K";
static const std::string EraseBeforeCursor = AnsiEscapePrefix + "1J";
static const std::string EraseEntireScreen = AnsiEscapePrefix + "2J";
static const std::string EraseSavedLines = AnsiEscapePrefix + "3J";
static const std::string EraseInLine = AnsiEscapePrefix + "K";
static const std::string EraseLineAfterCursor = AnsiEscapePrefix + "0K";
static const std::string EraseLineBeforeCursor = AnsiEscapePrefix + "1K";
#pragma endregion
#pragma region Graphics Sequences
constexpr std::string ResetAll = AnsiEscapePrefix + "0m";
constexpr std::string Bold = AnsiEscapePrefix + "1m";
constexpr std::string Dim = AnsiEscapePrefix + "2m";
constexpr std::string Italic = AnsiEscapePrefix + "3m";
constexpr std::string Underline = AnsiEscapePrefix + "4m";
constexpr std::string Blinking = AnsiEscapePrefix + "5m";
constexpr std::string Inverse = AnsiEscapePrefix + "6m";
constexpr std::string Hidden = AnsiEscapePrefix + "7m";
constexpr std::string Strikethrough = AnsiEscapePrefix + "8m";
static const std::string ResetAll = AnsiEscapePrefix + "0m";
static const std::string Bold = AnsiEscapePrefix + "1m";
static const std::string Dim = AnsiEscapePrefix + "2m";
static const std::string Italic = AnsiEscapePrefix + "3m";
static const std::string Underline = AnsiEscapePrefix + "4m";
static const std::string Blinking = AnsiEscapePrefix + "5m";
static const std::string Inverse = AnsiEscapePrefix + "6m";
static const std::string Hidden = AnsiEscapePrefix + "7m";
static const std::string Strikethrough = AnsiEscapePrefix + "8m";
#pragma endregion
#pragma region Color Sequences
constexpr std::string FgBlack = AnsiEscapePrefix + "30";
constexpr std::string BgBlack = AnsiEscapePrefix + "40";
static const std::string FgBlack = AnsiEscapePrefix + "30";
static const std::string BgBlack = AnsiEscapePrefix + "40";
constexpr std::string FgRed = AnsiEscapePrefix + "31";
constexpr std::string BgRed = AnsiEscapePrefix + "41";
static const std::string FgRed = AnsiEscapePrefix + "31";
static const std::string BgRed = AnsiEscapePrefix + "41";
constexpr std::string FgGreen = AnsiEscapePrefix + "32";
constexpr std::string BgGreen = AnsiEscapePrefix + "42";
static const std::string FgGreen = AnsiEscapePrefix + "32";
static const std::string BgGreen = AnsiEscapePrefix + "42";
constexpr std::string FgYellow = AnsiEscapePrefix + "33";
constexpr std::string BgYellow = AnsiEscapePrefix + "43";
static const std::string FgYellow = AnsiEscapePrefix + "33";
static const std::string BgYellow = AnsiEscapePrefix + "43";
constexpr std::string FgBlue = AnsiEscapePrefix + "34";
constexpr std::string BgBlue = AnsiEscapePrefix + "44";
static const std::string FgBlue = AnsiEscapePrefix + "34";
static const std::string BgBlue = AnsiEscapePrefix + "44";
constexpr std::string FgMagenta = AnsiEscapePrefix + "35";
constexpr std::string BgMagenta = AnsiEscapePrefix + "45";
static const std::string FgMagenta = AnsiEscapePrefix + "35";
static const std::string BgMagenta = AnsiEscapePrefix + "45";
constexpr std::string FgCyan = AnsiEscapePrefix + "36";
constexpr std::string BgCyan = AnsiEscapePrefix + "46";
static const std::string FgCyan = AnsiEscapePrefix + "36";
static const std::string BgCyan = AnsiEscapePrefix + "46";
constexpr std::string FgWhite = AnsiEscapePrefix + "37";
constexpr std::string BgWhite = AnsiEscapePrefix + "47";
static const std::string FgWhite = AnsiEscapePrefix + "37";
static const std::string BgWhite = AnsiEscapePrefix + "47";
constexpr std::string FgDefault = AnsiEscapePrefix + "38";
constexpr std::string BgDefault = AnsiEscapePrefix + "48";
static const std::string FgDefault = AnsiEscapePrefix + "38";
static const std::string BgDefault = AnsiEscapePrefix + "48";
constexpr std::string FgBrightBlack = AnsiEscapePrefix + "90";
constexpr std::string BgBrightBlack = AnsiEscapePrefix + "100";
static const std::string FgBrightBlack = AnsiEscapePrefix + "90";
static const std::string BgBrightBlack = AnsiEscapePrefix + "100";
constexpr std::string FgBrightRed = AnsiEscapePrefix + "91";
constexpr std::string BgBrightRed = AnsiEscapePrefix + "101";
static const std::string FgBrightRed = AnsiEscapePrefix + "91";
static const std::string BgBrightRed = AnsiEscapePrefix + "101";
constexpr std::string FgBrightGreen = AnsiEscapePrefix + "92";
constexpr std::string BgBrightGreen = AnsiEscapePrefix + "102";
static const std::string FgBrightGreen = AnsiEscapePrefix + "92";
static const std::string BgBrightGreen = AnsiEscapePrefix + "102";
constexpr std::string FgBrightYellow = AnsiEscapePrefix + "93";
constexpr std::string BgBrightYellow = AnsiEscapePrefix + "103";
static const std::string FgBrightYellow = AnsiEscapePrefix + "93";
static const std::string BgBrightYellow = AnsiEscapePrefix + "103";
constexpr std::string FgBrightBlue = AnsiEscapePrefix + "94";
constexpr std::string BgBrightBlue = AnsiEscapePrefix + "104";
static const std::string FgBrightBlue = AnsiEscapePrefix + "94";
static const std::string BgBrightBlue = AnsiEscapePrefix + "104";
constexpr std::string FgBrightMagenta = AnsiEscapePrefix + "95";
constexpr std::string BgBrightMagenta = AnsiEscapePrefix + "105";
static const std::string FgBrightMagenta = AnsiEscapePrefix + "95";
static const std::string BgBrightMagenta = AnsiEscapePrefix + "105";
constexpr std::string FgBrightCyan = AnsiEscapePrefix + "96";
constexpr std::string BgBrightCyan = AnsiEscapePrefix + "106";
static const std::string FgBrightCyan = AnsiEscapePrefix + "96";
static const std::string BgBrightCyan = AnsiEscapePrefix + "106";
constexpr std::string FgBrightWhite = AnsiEscapePrefix + "97";
constexpr std::string BgBrightWhite = AnsiEscapePrefix + "107";
static const std::string FgBrightWhite = AnsiEscapePrefix + "97";
static const std::string BgBrightWhite = AnsiEscapePrefix + "107";
constexpr std::string FgTrueColor(int r, int g, int b) {
return AnsiEscapePrefix + std::format("38;2;{};{};{}m", r, g, b);
}
static std::string FgTrueColor(int r, int g, int b);
constexpr std::string FgTrueColor(const Color4& c) {
return FgTrueColor(c.r, c.g, c.b);
}
static std::string FgTrueColor(const Color4& c);
constexpr std::string BgTrueColor(int r, int g, int b) {
return AnsiEscapePrefix + std::format("48;2;{};{};{}m", r, g, b);
}
static std::string BgTrueColor(int r, int g, int b);
constexpr std::string BgTrueColor(const Color4& c) {
return BgTrueColor(c.r, c.g, c.b);
}
static std::string BgTrueColor(const Color4& c);
#pragma endregion
#pragma region Private Modes
constexpr std::string CursorInvisible = AnsiEscapePrefix + "?25l";
constexpr std::string CursorVisible = AnsiEscapePrefix + "?25h";
constexpr std::string RestoreScreen = AnsiEscapePrefix + "?47l";
constexpr std::string SaveScreen = AnsiEscapePrefix + "?47h";
constexpr std::string EnableAltBuffer = AnsiEscapePrefix + "?1049h";
constexpr std::string DisableAltBuffer = AnsiEscapePrefix + "?1049l";
inline static const std::string CursorInvisible = AnsiEscapePrefix + "?25l";
static const std::string CursorVisible = AnsiEscapePrefix + "?25h";
static const std::string RestoreScreen = AnsiEscapePrefix + "?47l";
static const std::string SaveScreen = AnsiEscapePrefix + "?47h";
static const std::string EnableAltBuffer = AnsiEscapePrefix + "?1049h";
static const std::string DisableAltBuffer = AnsiEscapePrefix + "?1049l";
#pragma endregion
}
#endif
}

View File

@@ -1 +1,49 @@
#include <AnsiEscapeCodes.hpp>
#include <AnsiEscapeCodes.hpp>
std::string mcolor::AnsiEscapeCodes::CursorTo(int line, int column) {
return AnsiEscapePrefix + std::format("{};{}H", line, column);
}
std::string mcolor::AnsiEscapeCodes::CursorUp(int lines) {
return AnsiEscapePrefix + std::format("{}A", lines);
}
std::string mcolor::AnsiEscapeCodes::CursorDown(int lines) {
return AnsiEscapePrefix + std::format("{}B", lines);
}
std::string mcolor::AnsiEscapeCodes::CursorRight(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
std::string mcolor::AnsiEscapeCodes::CursorLeft(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
std::string mcolor::AnsiEscapeCodes::CursorNextLineStart(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
std::string mcolor::AnsiEscapeCodes::CursorPrevLineStart(int lines) {
return AnsiEscapePrefix + std::format("{}D", lines);
}
std::string mcolor::AnsiEscapeCodes::CursorColumn(int column) {
return AnsiEscapePrefix + std::format("{}G", column);
}
std::string mcolor::AnsiEscapeCodes::FgTrueColor(int r, int g, int b) {
return AnsiEscapePrefix + std::format("38;2;{};{};{}m", r, g, b);
}
std::string mcolor::AnsiEscapeCodes::FgTrueColor(const Color4 &c) {
return FgTrueColor(c.r, c.g, c.b);
}
std::string mcolor::AnsiEscapeCodes::BgTrueColor(int r, int g, int b) {
return AnsiEscapePrefix + std::format("48;2;{};{};{}m", r, g, b);
}
std::string mcolor::AnsiEscapeCodes::BgTrueColor(const Color4 &c) {
return BgTrueColor(c.r, c.g, c.b);
}