Added colored text in terminal
This commit is contained in:
60
Common/IO.h
Normal file
60
Common/IO.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
// Platform specific includes //
|
||||
#ifdef _WIN32
|
||||
#define NOMINMAX
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#error "Code is currently only supported on Win32"
|
||||
#endif // _WIN32
|
||||
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
namespace LXC::Util
|
||||
{
|
||||
// Checks if a type can be outputted to std::ostream //
|
||||
template<typename T> concept Logable = requires(std::ostream& os, T t)
|
||||
{
|
||||
// I have no idea what this part does at all //
|
||||
{ os << t } -> std::same_as<std::ostream&>;
|
||||
};
|
||||
|
||||
// Checks if a list of types can be outputted to std::ostream //
|
||||
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
|
||||
|
||||
// Enum to translate to the Win32 code for the colors //
|
||||
enum class Color : WORD
|
||||
{
|
||||
BLACK = 0x00,
|
||||
BLUE = 0x01,
|
||||
GREEN = 0x02,
|
||||
AQUA = 0x03,
|
||||
RED = 0x04,
|
||||
PURPLE = 0x05,
|
||||
YELLOW = 0x06,
|
||||
LIGHT_GRAY = 0x07,
|
||||
LIGHT_BLUE = 0x09,
|
||||
LIGHT_GREEN = 0x0a,
|
||||
LIGHT_AQUA = 0x0b,
|
||||
LIGHT_RED = 0x0c,
|
||||
LIGHT_PURPLE = 0x0d,
|
||||
LIGHT_YELLOW = 0x0e,
|
||||
WHITE = 0x0f
|
||||
};
|
||||
|
||||
// Prints arguments to the console with the given color //
|
||||
template<Color col, typename... Args>
|
||||
requires AllLogable<Args...>
|
||||
inline void PrintAs(Args... args)
|
||||
{
|
||||
// Permenant handle to the console //
|
||||
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
|
||||
// Prints it to the console and resets the color //
|
||||
SetConsoleTextAttribute(hConsole, (WORD)col);
|
||||
(std::cout << ... << args);
|
||||
SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user