Skip to content

Instantly share code, notes, and snippets.

@MarioLiebisch
Created July 14, 2019 12:42
Show Gist options
  • Save MarioLiebisch/2aa374c2887823f27df659bcb6dd184c to your computer and use it in GitHub Desktop.
Save MarioLiebisch/2aa374c2887823f27df659bcb6dd184c to your computer and use it in GitHub Desktop.
An old simple wrapper I've used in a few old tools to get cross-platform coloring in console
/**
* @file conext.h
* @author Mario Liebisch <mario.liebisch@gmail.com>
* @section LICENSE
*
* © 2011 All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @section DESCRIPTION
*
* Simple console control extension
*
*/
#pragma once
#ifndef _CONEXT_H
#define _CONEXT_H
#include <cstdio>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
/// Simple console control extension
namespace conext
{
/// Console colors
enum CON_COLOR
{
#ifndef _WIN32
BLACK = 0,
RED = 1,
GREEN = 2,
YELLOW = 3,
BLUE = 4,
MAGENTA = 5,
CYAN = 6,
WHITE = 7,
FDEFAULT = 9,
BDEFAULT = 9
#else
BLACK = 0,
RED = FOREGROUND_RED,
GREEN = FOREGROUND_GREEN,
YELLOW = RED | GREEN,
BLUE = FOREGROUND_BLUE,
MAGENTA = RED | BLUE,
CYAN = BLUE | GREEN,
WHITE = RED | BLUE | GREEN,
FDEFAULT = WHITE,
BDEFAULT = BLACK
#endif
};
/// Class to support colored console output
class Color
{
private:
#ifndef _WIN32
char tmp[16]; //< Buffer to store the escape codes to be written
wchar_t wtmp[16]; //< Buffer to store the escape codes to be written
#endif
CON_COLOR fcode; //< Set foreground color
CON_COLOR bcode; //< Set background color
public:
/**
* Constructor
*
* @param front Foreground color (see ::CON_COLOR)
* @param back Background color (see ::CON_COLOR)
*/
Color(CON_COLOR front = FDEFAULT, CON_COLOR back = BDEFAULT) : fcode(front), bcode(back)
{
#ifndef _WIN32
tmp[0] = wtmp[0] = 0;
snprintf(tmp, 15, "\x1b[%d;%dm", 30 + fcode, 40 + bcode);
swprintf(wtmp, 15, L"\x1b[%d;%dm", 30 + fcode, 40 + bcode);
#endif
}
/**
* Toggles console color (Windows) or returns the escape sequence to be written to console (other OSs)
* This method is meant to be used with stream objects.
*
* @return Empty string (Windows) or escape sequence (other OSs)
*/
inline operator const char *()
{
#ifndef _WIN32
return tmp;
#else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fcode | bcode);
return "";
#endif
}
/**
* Toggles console color (Windows) or returns the escape sequence to be written to console (other OSs)
* This method is meant to be used with stream objects.
*
* @return Empty string (Windows) or escape sequence (other OSs)
*/
inline operator const wchar_t *()
{
#ifndef _WIN32
return wtmp;
#else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fcode | bcode);
return L"";
#endif
}
/**
* Sets the color scheme represented by this object
*/
inline void set(void)
{
#ifndef _WIN32
printf("%s", tmp);
#else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fcode | bcode);
#endif
}
};
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment