Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 309302b5cd502387ec8eea1c423bc5ec9c246449 @aslakhellesoy committed Oct 12, 2009
Showing with 150 additions and 0 deletions.
  1. +13 −0 COPYING
  2. +53 −0 README.textile
  3. +3 −0 printcol.bat
  4. +81 −0 wac.c
  5. BIN wac.exe
@@ -0,0 +1,13 @@
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ Version 2, December 2004
+
+ Copyright (C) 2009 Aslak Hellesøy
+ Everyone is permitted to copy and distribute verbatim or modified
+ copies of this license document, and changing it is allowed as long
+ as the name is changed.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
+
@@ -0,0 +1,53 @@
+= wac - Windows ANSI Color
+
+wac.exe is a small command line utility that lets you use ANSI colors on Windows. This will print nice colours:
+
+ printcol.bat | wac
+
+This will not:
+
+ printcol.bat
+
+h2. Why wac?
+
+Some command line tools (like "Cucumber":http://cukes.info) like to print ANSI colors.
+Since Windows has no idea what ANSI colours are, all you'll see is gibberish. Unless
+you use wac.
+
+h2. But we have Win32::Console
+
+I know that Perl, Ruby, Python etc have native libraries that will do ANSI coloring on Windows,
+but not all platforms have that. If you're using e.g. JRuby on Windows there is no such
+library. So wac will save you.
+
+h2. Installation
+
+Just download wac.exe and put it somewhere on your PATH.
+
+h2. How does it work?
+
+wac.exe reads from STDIN and prints to STDOUT. While doing that it looks for ANSI color codes, and if it finds one,
+it sets the console color accordingly and strips away the color code from the output.
+
+h2. How do I build it?
+
+Just get MinGW and run:
+
+ gcc -o wac wac.c
+
+h2. Why is it in C?
+
+Because it's easy to change colours on Windows with C.
+
+h2. Why is the C code so bad?
+
+Because I never do C. Feel free to improve that.
+
+h2. I have found a bug
+
+Great. Just fork this Git repo, clone your fork and fix it.
+When you have pushed your changes back to your repo, send me a pull request.
+
+h2. I have found a bug, but I don't know C.
+
+I don't know C either. Find someone who does.
@@ -0,0 +1,3 @@
+@echo off
+echo red bold
+echo red regular
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <windows.h>
+
+int main(void) {
+ const int W_BLACK = 0;
+ const int W_BLUE = 1;
+ const int W_GREEN = 2;
+ const int W_RED = 4;
+ const int W_INTENSITY = 8;
+ const int W_CYAN = W_BLUE | W_GREEN;
+ const int W_MAGENTA = W_BLUE | W_RED;
+ const int W_YELLOW = W_GREEN | W_RED;
+ const int W_WHITE = W_BLUE | W_GREEN | W_RED;
+ const int ANSI2WIN[] = {W_BLACK, W_RED, W_GREEN, W_YELLOW, W_BLUE, W_MAGENTA, W_CYAN, W_WHITE};
+
+ const int RESET = 0;
+ const int BOLD = 1;
+ const int BLACK = 30;
+ const int RED = 31;
+ const int GREEN = 32;
+ const int YELLOW = 33;
+ const int BLUE = 34;
+ const int MAGENTA = 35;
+ const int CYAN = 36;
+ const int GRAY = 37;
+ const int WHITE = 37;
+
+ CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
+ GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo);
+ int initialColor = consoleInfo.wAttributes;
+
+ int state, ansiColor, multiplication, pos, winColor, winIntensity = -1;
+ int colors[] = {-1,-1};
+ int colorPos = 1;
+ int ch;
+
+ while ((ch = getchar()) != EOF) {
+ if(ch == '\e') {
+ state = '\e';
+ } else if (state == '\e' && ch == '[') {
+ state = '[';
+ } else if (state == '[') {
+ if (ch != 'm') {
+ colors[colorPos] = ch;
+ colorPos--;
+ } else {
+ // Find ANSI Color
+ ansiColor = 0;
+ multiplication = 1;
+ for (pos = colorPos + 1; pos < 2; pos++) {
+ ansiColor += (colors[pos] - 48) * multiplication;
+ multiplication *= 10;
+ }
+
+ // Convert ANSI Color to Windows Color
+ if (ansiColor == BOLD) {
+ winIntensity = W_INTENSITY;
+ } else if (ansiColor == RESET) {
+ winIntensity = W_BLACK;
+ winColor = initialColor;
+ } else if (BLACK <= ansiColor && ansiColor <= WHITE) {
+ winColor = ANSI2WIN[ansiColor - 30];
+ } else if (ansiColor == 90) {
+ // Special case for gray (it's really white)
+ winColor = W_WHITE;
+ winIntensity = W_BLACK;
+ }
+
+ // initialColor & 0xF0 is to keep the background color
+ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), winColor | winIntensity | (initialColor & 0xF0));
+ colorPos = 1;
+ state = -1;
+ }
+ } else {
+ putchar(ch);
+ }
+ }
+
+ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), initialColor);
+ return 0;
+}
Binary file not shown.

0 comments on commit 309302b

Please sign in to comment.