Created
May 25, 2019 14:44
-
-
Save mrexodia/17ebbe413da5602dfbcad291a502b54f to your computer and use it in GitHub Desktop.
ConsoleColor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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