├── wac.exe ├── printcol.bat ├── COPYING ├── README.textile └── wac.c /wac.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aslakhellesoy/wac/HEAD/wac.exe -------------------------------------------------------------------------------- /printcol.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | echo red bold 3 | echo red regular 4 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2009 Aslak Hellesøy 5 | Everyone is permitted to copy and distribute verbatim or modified 6 | copies of this license document, and changing it is allowed as long 7 | as the name is changed. 8 | 9 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 10 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 11 | 12 | 0. You just DO WHAT THE FUCK YOU WANT TO. 13 | 14 | -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h1. wac - Windows ANSI Color 2 | 3 | wac.exe is a small command line utility that lets you use ANSI colors on Windows. This will print nice colors: 4 | 5 |
 6 | printcol.bat | wac
 7 | 
8 | 9 | This will not: 10 | 11 |
12 | printcol.bat
13 | 
14 | 15 | h2. Why wac? 16 | 17 | Some command line tools (like "Cucumber":http://cukes.info) like to print ANSI colors. 18 | Since Windows has no idea what ANSI colors are, all you'll see is gibberish. Unless 19 | you use wac. 20 | 21 | h2. But we have Win32::Console 22 | 23 | I know that Perl, Ruby, Python etc have native libraries that will do ANSI coloring on Windows, 24 | but not all platforms have that. If you're using e.g. JRuby on Windows there is no such 25 | library. So wac will save you. 26 | 27 | h2. Download/Installation 28 | 29 | Just download "wac.exe":http://github.com/aslakhellesoy/wac/raw/master/wac.exe and put it somewhere on your PATH. 30 | 31 | h2. How does it work? 32 | 33 | wac.exe reads from STDIN and prints to STDOUT. While doing that it looks for ANSI color codes, and if it finds one, 34 | it sets the console color accordingly and strips away the color code from the output. 35 | 36 | h2. How do I build it? 37 | 38 | Just get MinGW and run: 39 | 40 |
41 | gcc -o wac wac.c
42 | 
43 | 44 | h2. Why is it in C? 45 | 46 | Because it's easy to change colors on Windows with C. 47 | 48 | h2. Why is the C code so bad? 49 | 50 | Because I never do C. Feel free to improve that. 51 | 52 | h2. I have found a bug 53 | 54 | Great. Just fork this Git repo, clone your fork and fix it. 55 | When you have pushed your changes back to your repo, send me a pull request. 56 | 57 | h2. I have found a bug, but I don't know C. 58 | 59 | I don't know C either. Find someone who does. 60 | -------------------------------------------------------------------------------- /wac.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | const int W_BLACK = 0; 6 | const int W_BLUE = 1; 7 | const int W_GREEN = 2; 8 | const int W_RED = 4; 9 | const int W_INTENSITY = 8; 10 | const int W_CYAN = W_BLUE | W_GREEN; 11 | const int W_MAGENTA = W_BLUE | W_RED; 12 | const int W_YELLOW = W_GREEN | W_RED; 13 | const int W_WHITE = W_BLUE | W_GREEN | W_RED; 14 | const int ANSI2WIN[] = {W_BLACK, W_RED, W_GREEN, W_YELLOW, W_BLUE, W_MAGENTA, W_CYAN, W_WHITE}; 15 | 16 | const int RESET = 0; 17 | const int BOLD = 1; 18 | const int BLACK = 30; 19 | const int RED = 31; 20 | const int GREEN = 32; 21 | const int YELLOW = 33; 22 | const int BLUE = 34; 23 | const int MAGENTA = 35; 24 | const int CYAN = 36; 25 | const int GRAY = 37; 26 | const int WHITE = 37; 27 | 28 | CONSOLE_SCREEN_BUFFER_INFO consoleInfo; 29 | GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo); 30 | int initialColor = consoleInfo.wAttributes; 31 | 32 | int state, ansiColor, multiplication, pos, winColor, winIntensity = -1; 33 | int colors[] = {-1,-1}; 34 | int colorPos = 1; 35 | int ch; 36 | 37 | while ((ch = getchar()) != EOF) { 38 | if(ch == '\e') { 39 | state = '\e'; 40 | } else if (state == '\e' && ch == '[') { 41 | state = '['; 42 | } else if (state == '[') { 43 | if (ch != 'm') { 44 | colors[colorPos] = ch; 45 | colorPos--; 46 | } else { 47 | // Find ANSI Color 48 | ansiColor = 0; 49 | multiplication = 1; 50 | for (pos = colorPos + 1; pos < 2; pos++) { 51 | ansiColor += (colors[pos] - 48) * multiplication; 52 | multiplication *= 10; 53 | } 54 | 55 | // Convert ANSI Color to Windows Color 56 | if (ansiColor == BOLD) { 57 | winIntensity = W_INTENSITY; 58 | } else if (ansiColor == RESET) { 59 | winIntensity = W_BLACK; 60 | winColor = initialColor; 61 | } else if (BLACK <= ansiColor && ansiColor <= WHITE) { 62 | winColor = ANSI2WIN[ansiColor - 30]; 63 | } else if (ansiColor == 90) { 64 | // Special case for gray (it's really white) 65 | winColor = W_WHITE; 66 | winIntensity = W_BLACK; 67 | } 68 | 69 | // initialColor & 0xF0 is to keep the background color 70 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), winColor | winIntensity | (initialColor & 0xF0)); 71 | colorPos = 1; 72 | state = -1; 73 | } 74 | } else { 75 | putchar(ch); 76 | } 77 | } 78 | 79 | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), initialColor); 80 | return 0; 81 | } 82 | --------------------------------------------------------------------------------