Exemplo n.º 1
0
static void
coloredPrintf(CSpec_Color color, const char* format, ...)
{
#ifdef _WIN32
	HANDLE console_handle;
	CONSOLE_SCREEN_BUFFER_INFO buffer_info;
	WORD default_color_attributes;
#endif	/* _WIN32 */
	va_list args;


	va_start(args, format);

#ifdef _WIN32

	console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleScreenBufferInfo(console_handle, &buffer_info);
	default_color_attributes = buffer_info.wAttributes;

	/* Set color */
	SetConsoleTextAttribute(console_handle,
							getWindowsColorAttribute(color) |
							FOREGROUND_INTENSITY);

	/* Print Text */
	vprintf(format, args);

	/* Reset color */
	SetConsoleTextAttribute(console_handle,
							default_color_attributes);

#else	/* !_WIN32 */

	/* Set color */
	printf("\033[0;%dm", getAnsiColorCode(color));

	/* Print Text */
	vprintf(format, args);

	/* Reset color */
	printf("\033[m");

#endif	/* _WIN32 */

	va_end(args);
	return;
}
Exemplo n.º 2
0
bstring Socket::getColorCode(const unsigned char ch) {
    std::ostringstream oStr;
    if(!opts.color) {
        // Color is not active, only replace a caret
        if(ch == '^')
            oStr << "^";
        return(oStr.str());
    } else {
        // Color is active, do replacement

        // Only return a color if the last color is not equal to the current color
        if(opts.lastColor != ch || opts.lastColor == '^') {
            opts.lastColor = ch;
            oStr << getAnsiColorCode(ch);
        }
        return(oStr.str());
    }
}