Last active
August 10, 2016 13:58
-
-
Save maldevel/527a292c0cbcf3a34061f13fca48c095 to your computer and use it in GitHub Desktop.
Color text in Windows terminal application
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
/* | |
Copyright (C) 2016 @maldevel | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
For more see the file 'LICENSE' for copying permission. | |
*/ | |
//-------------------------------------------------------------------------// | |
//------------------------------- colorama.h -------------------------------// | |
#ifndef COLORAMA_H_ | |
#define COLORAMA_H_ | |
#include <windows.h> | |
#include <stdio.h> | |
#define FOREGROUND_BLACK 0x0 | |
#define FOREGROUND_AQUA 0x3 | |
#define FOREGROUND_PURPLE 0x5 | |
#define FOREGROUND_YELLOW 0x6 | |
#define FOREGROUND_WHITE 0x7 | |
#define FOREGROUND_GRAY 0x8 | |
#define FOREGROUND_LIGHT_BBLUE 0x9 | |
#define FOREGROUND_LIGHT_GREEN 0xA | |
#define FOREGROUND_LIGHT_AQUA 0xB | |
#define FOREGROUND_LIGHT_RED 0xC | |
#define FOREGROUND_LIGHT_PURPLE 0xD | |
#define FOREGROUND_LIGHT_YELLOW 0xE | |
#define FOREGROUND_BRIGHT_WHITE 0xF | |
int printColored(WORD wColorAttrs, const char *format, ...); | |
HANDLE StartColoring(WORD wNewColorAttrs, WORD *wOldColorAttrs); | |
int EndColoring(HANDLE hStdout, WORD wOldColorAttrs); | |
#endif | |
//-------------------------------------------------------------------------// | |
//------------------------------- colorama.c -------------------------------// | |
#include "colorama.h" | |
int printColored(WORD wColorAttrs, const char *format, ...){ | |
va_list arglist; | |
int written = 0; | |
va_start(arglist, format); | |
HANDLE hStdout = INVALID_HANDLE_VALUE; | |
WORD wOldColorAttrs = 0; | |
hStdout = StartColoring(wColorAttrs, &wOldColorAttrs); | |
written = vprintf(format, arglist); | |
va_end(arglist); | |
EndColoring(hStdout, wOldColorAttrs); | |
return written; | |
} | |
HANDLE StartColoring(WORD wNewColorAttrs, WORD *wOldColorAttrs){ | |
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
if (hStdout == INVALID_HANDLE_VALUE) | |
{ | |
return INVALID_HANDLE_VALUE; | |
} | |
if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) | |
{ | |
return INVALID_HANDLE_VALUE; | |
} | |
*wOldColorAttrs = csbiInfo.wAttributes; | |
if (! SetConsoleTextAttribute(hStdout, wNewColorAttrs)) | |
{ | |
return INVALID_HANDLE_VALUE; | |
} | |
return hStdout; | |
} | |
int EndColoring(HANDLE hStdout, WORD wOldColorAttrs){ | |
if(hStdout == INVALID_HANDLE_VALUE) return FALSE; | |
if(! SetConsoleTextAttribute(hStdout, wOldColorAttrs)) | |
{ | |
return FALSE; | |
} | |
return TRUE; | |
} | |
//-------------------------------------------------------------------------// | |
//------------------------------- example.c -------------------------------// | |
#include "colorama.h" | |
int main() | |
{ | |
printColored(FOREGROUND_RED | FOREGROUND_INTENSITY, | |
"\n" | |
"\n" | |
"test1\n" | |
"test2\n" | |
"test3\n" | |
"\n"); | |
printColored(FOREGROUND_BRIGHT_WHITE | FOREGROUND_INTENSITY, "test"); | |
printColored(FOREGROUND_YELLOW | FOREGROUND_INTENSITY, "test %s\n", "test"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment