Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ConsoleColor
//Copyleft Vincent Godin
//https://www.codeproject.com/Articles/16431/Add-color-to-your-std-cout
#pragma once
#include <iostream>
#ifdef _WIN32
#include <windows.h>
inline void setAttributes(unsigned int attributes)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, attributes);
}
inline void writeConsole(const char* value)
{
DWORD written = 0;
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), value, lstrlenA(value), &written, nullptr);
}
#endif //_WIN32
inline std::ostream& blue(std::ostream &s)
{
#if defined(WIN32)
setAttributes(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#endif
return s;
}
inline std::ostream& red(std::ostream &s)
{
#if defined(WIN32)
setAttributes(FOREGROUND_RED | FOREGROUND_INTENSITY);
#endif
return s;
}
inline std::ostream& green(std::ostream &s)
{
#if defined(WIN32)
setAttributes(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#endif
return s;
}
inline std::ostream& yellow(std::ostream &s)
{
#if defined(WIN32)
setAttributes(FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
#endif
return s;
}
inline std::ostream& white(std::ostream &s)
{
#if defined(WIN32)
setAttributes(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#endif
return s;
}
inline std::ostream& reset(std::ostream &s)
{
#if defined(WIN32)
setAttributes(FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
#endif
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.