Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » Set text colour in command line (For example to remark warnings and errors)
Set text colour in command line [message #54539] Tue, 11 August 2020 09:02
koldo is currently offline  koldo
Messages: 3372
Registered: August 2008
Senior Veteran
index.php?t=getfile&id=6160&private=0

enum CONSOLE_COLOR {
#ifdef PLATFORM_WIN32
    BLACK       = 0,
    BLUE        = FOREGROUND_BLUE,
    GREEN       = FOREGROUND_GREEN,
    CYAN        = FOREGROUND_GREEN | FOREGROUND_BLUE,
    RED         = FOREGROUND_RED,
    MAGENTA     = FOREGROUND_RED | FOREGROUND_BLUE,
    YELLOW      = FOREGROUND_RED | FOREGROUND_GREEN,
    GRAY        = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
    LTBLUE      = FOREGROUND_INTENSITY | FOREGROUND_BLUE,
    LTGREEN     = FOREGROUND_INTENSITY | FOREGROUND_GREEN,
    LTCYAN      = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
    LTRED       = FOREGROUND_INTENSITY | FOREGROUND_RED,
    LTMAGENTA	= FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
    LTYELLOW    = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
    WHITE      	= FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
    RESET
#else
	BLACK     	= 30,
	RED     	= 31,
	GREEN   	= 32,
	YELLOW  	= 33,
	BLUE    	= 34,
	MAGENTA 	= 35,
	CYAN    	= 36,
	GRAY    	= 37,
	LTRED     	= 131,
	LTGREEN   	= 132,
	LTYELLOW  	= 133,
	LTBLUE    	= 134,
	LTMAGENTA 	= 135,
	LTCYAN    	= 136,
	WHITE    	= 137,
	RESET   	= 0
#endif
};

bool SetConsoleColor(CONSOLE_COLOR color) {
#ifdef PLATFORM_WIN32
	static HANDLE hstdout = 0;
	static CONSOLE_SCREEN_BUFFER_INFO csbiInfo = {};
	static WORD woldattrs;
	
	if (hstdout == 0) {
		hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
		if (!GetConsoleScreenBufferInfo(hstdout, &csbiInfo)) {
			hstdout = 0;
			return false;
		}
		woldattrs = csbiInfo.wAttributes;
	}
	switch (color) {
	case RESET:		return SetConsoleTextAttribute(hstdout, woldattrs);
	default:		return SetConsoleTextAttribute(hstdout, color);
	}
#else
	if (color < 100)
		printf("\x1b[%dm", color);
	else
		printf("\x1b[1;%dm", color-100);
	return true;
#endif
}


Best regards
Iñaki

[Updated on: Tue, 11 August 2020 11:16]

Report message to a moderator

Previous Topic: Optimized bit setting/clearing
Next Topic: Disable/re-enable printf()/Cout() console output
Goto Forum:
  


Current Time: Sat Jun 15 16:08:38 CEST 2024

Total time taken to generate the page: 0.01246 seconds