
Here's a tool I whipped up this afternoon in order to spruce up using the Windows command line console as well as create an easy-to-grok example project for Macaroni for C++.
The Windows console has a palette of 16 colors. While it's easy to switch the foreground and background colors between the various 16 choices on the command line using the color shell command, altering the palette itself is a huge hassle and involves clicking around in Windows 95 era GUIs. Using a modified palette routinely introduces other hassles as the colors may not work for all programs. Finally, there's no way to switch between palettes in an active session, and when you're juggling multiple command line sessions the ability to switch to a unique color scheme is crucial to maintaining sanity.
There are several pretty decent Windows command prompt replacements out there, but none ever clicked for me personally. So I always find myself using the standard command prompt and hating the lack of colors (and tabs, but I didn't write a program to fix that so we won't discuss that today).
Coding Solace
I went looking around for a program to change the built in palette of colors from the command line and found a tool called consattr which seemed to do exactly what I needed, but alas it didn't work in Windows Vista onward (I think- the page is a bit out of date). However, the author derelict did include a note on a Windows API call named “SetConsolePalette”, which lead me to a source file from catch22.net that included a note referencing a new Vista API called SetConsoleScreenBufferEx, and at that point I figured writing a program myself might not be that difficult.
The big trick, as usual, was mitigating the pathological edge case bugs that seem to exist in all Windows APIs (the call to change the color palette moves the window for some reason, so code had to be written to move it back), but with a little bit of Googling and elbow grease I got it done in a few hours.
Even though Solace is Windows only, I hope it might serve as a decent example of how to use Macaroni as it is both simple enough to understand but also manages to accomplish something worth doing. A unique thing about the project's code is that I tried to keep all of the source to one single file, though I did move some external project declarations out as it seemed like better practice. The cool thing is this all worked without any changes to Macaroni, which is not something I could have said two years or even a year ago. Every C++ translation unit necessary to create Solace, including the two main functions for each exe file, comes from a single Macaroni source file. Though it's kind of a cheat to say this since it generates a Boost Build script,the entire project's build configuration is only 15 lines long.
Using Solace
Solace works by writing all the colors used by the current palette to stdout. They can then be scooped up into a file which can be loaded later.
The file format is just the palette index, an equals sign, and the three RGB values seperated by spaces and ending with a newline.
Once you create a few color schemes you like, it's easy to write a batch file like the following to load them:
solace-load coffee.solace
color 07
Put that in a file called “coffee.bat” somewhere in your path and you've got a quick way to put a color scheme into effect whenever you need it. Make a few of these and the color experience approaches something usually seen only on Linux or OSX.