From 759a9e3a88deaab3fc2b0f62d108671bef15c053 Mon Sep 17 00:00:00 2001 From: PashaBibko Date: Mon, 21 Jul 2025 20:21:08 +0100 Subject: [PATCH] Colors in terminal is now cross-platform --- Common/IO.h | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/Common/IO.h b/Common/IO.h index 9b2f091..61dec89 100644 --- a/Common/IO.h +++ b/Common/IO.h @@ -78,13 +78,42 @@ namespace LXC::Internal #elif defined(__unix__) - template + static const char* GetAnsiCode(Util::Color color) + { + switch (color) + { + case Util::Color::BLACK: return "\x1b[30m"; + case Util::Color::BLUE: return "\x1b[34m"; + case Util::Color::GREEN: return "\x1b[32m"; + case Util::Color::AQUA: return "\x1b[36m"; + case Util::Color::RED: return "\x1b[31m"; + case Util::Color::PURPLE: return "\x1b[35m"; + case Util::Color::YELLOW: return "\x1b[33m"; + case Util::Color::LIGHT_GRAY: return "\x1b[0m"; + case Util::Color::LIGHT_BLUE: return "\x1b[94m"; + case Util::Color::LIGHT_GREEN: return "\x1b[92m"; + case Util::Color::LIGHT_AQUA: return "\x1b[96m"; + case Util::Color::LIGHT_RED: return "\x1b[91m"; + case Util::Color::LIGHT_PURPLE: return "\x1b[95m"; + case Util::Color::LIGHT_YELLOW: return "\x1b[93m"; + case Util::Color::WHITE: return "\x1b[97m"; + default: return "\x1b[0m"; + } + } + + template inline void WriteImpl(std::ostream& os, Args&&... args) { + if constexpr (col != Util::Color::DEFAULT) + os << GetAnsiCode(col); + (os << ... << std::forward(args)); - if constexpr (newLine) - os << std::endl; + if constexpr (newLine) + os << std::endl; + + if constexpr (col != Util::Color::DEFAULT) + os << GetAnsiCode(Util::Color::DEFAULT); } #endif