Files
kwa.fr/sdi_toolBox_1.0.x/toolBox/sdi_toolBox/console/ansi.h
2026-03-12 16:31:18 +01:00

85 lines
2.2 KiB
C++

/*
{{copyright}}
*/
/*
{{version}}
*/
/*
{{license}}
*/
#pragma once
#include <cstdint>
#include <string>
namespace sdi_toolBox::console::ANSI
{
//--------------------------------------------------------------
class EscapeCommand
{
public:
enum class Color : uint8_t
{
Black = 0,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
};
static constexpr uint8_t LIGHT = 0x01 << 0; // 0x01
static constexpr uint8_t BRIGHT = 0x01 << 1; // 0x02
static constexpr uint8_t DIM = 0x01 << 2; // 0x04
static constexpr uint8_t UNDERLINE = 0x01 << 3; // 0x08
static constexpr uint8_t BLINK = 0x01 << 4; // 0x10
static constexpr uint8_t REVERSE = 0x01 << 5; // 0x20
public:
static std::string clear();
static std::string get(const Color foregroundColor,
const uint8_t foregroundStyle = 0,
const Color backgroundColor = Color::Black,
const uint8_t backgroundStyle = 0);
};
//--------------------------------------------------------------
inline std::string EscapeCommand::clear()
{
return "\x1b[0m";
}
//--------------------------------------------------------------
inline std::string EscapeCommand::get(const Color foregroundColor, const uint8_t foregroundStyle, const Color backgroundColor, const uint8_t backgroundStyle)
{
std::string command = "\x1b[";
int foregroundColorValue = static_cast<int>(foregroundColor) + 30;
if (foregroundStyle & LIGHT)
foregroundColorValue += 60;
command += std::to_string(foregroundColorValue) + ";";
int backgroundColorValue = static_cast<int>(backgroundColor) + 40;
if (backgroundStyle & LIGHT)
backgroundColorValue += 60;
command += std::to_string(backgroundColorValue);
if (foregroundStyle & BRIGHT)
command += ";1";
if (foregroundStyle & DIM)
command += ";2";
if (foregroundStyle & UNDERLINE)
command += ";4";
if (foregroundStyle & BLINK)
command += ";5";
if (foregroundStyle & REVERSE)
command += ";7";
command += "m";
return command;
}
//--------------------------------------------------------------
} // namespace sdi_toolBox::console::ANSI