
Colorize the Windows console under Khokhloma
- Tutorial

Happy New Year, colleagues. Everyone knows that for the console in windows, you can easily set the color of text and background. But few people know that the remaining 14 colors can also be set, greatly simplifying your work with console applications that support colors in windows - for example, git or mercurial. Under the cut, in faces and pictures I’ll talk about my struggle with colors and the resulting utility in python, which allows you to colorize the console with one command so that you no longer see this white-blue horror of powershell.
ANSI Colors
So, I want to change all the colors in the console, not just text and background. First, let's see what kind of colors. In linux and osx, everything is simple - there is the ANSI Colors standard, which says that if you print a certain sequence of characters to the console, the text following it will be painted in all the colors of the rainbow:

Unfortunately, the authors of windows did not support this standard, but something There is a similar one - 16 colors with which you can colorize the text and background. A simple python program shows how it looks:
from ctypes import *
windll.Kernel32.GetStdHandle.restype = c_ulong
h = windll.Kernel32.GetStdHandle(c_ulong(0xfffffff5))
for color in xrange(16):
windll.Kernel32.SetConsoleTextAttribute(h, color)
print "color {0}".format( color )

The colors are, to put it mildly, sad. Especially if you compare the results with the default color scheme in ubuntu:

Change colors
The console color settings are stored, as expected, in the registry. We are interested in the key HKEY_CURRENT_USER \ Console, values from "ColorTable00 (black, it is the background color) to ColorTable15 (bright white). Values: DWORD, 0x00BBGGRR. We change these magic keys and get the same thing as in Ubuntu. The only difference is that colors go in a different order:

Is the goal achieved? It seems like that. But there is ...
Intrigue
Rejoice in new colors. After some time, we understand that we have Windows 7 or Windows 8 and we want to attach the console to the taskbar in order to call it via the “win + number” hotkey. We make “pin this program to taskbar”, run it, and see THIS:

Where did this blue background come from, which becomes black when painted? Indeed, if you launch the console through “win + r”, we see our colors set in the previous step. Here lies the most interesting thing - when we attach the console application to the taskbar, Windows creates a shortcut for it (a file with the extension .lnk) and sets personal color settings for this shortcut. What to do? We take up python, a little COM, and modify the shortcut settings. Now everything works.
Promised Two Clicks
In order not to lose the qualifications of a programmer, I designed all of the above as a small module in python, which can be used as a command-line utility. If you already have python installed, then to install my masterpiece, just run the command:
pip install pywincmdtheme
If you don’t have python, then I recommend installing the ActivePython assembly - in addition to python, it also contains a number of predefined extensions useful for working under Windows. How to use the utility to paint the console under Khokhloma? If you run the utility without command line arguments, it will try to find the .Xresources file in the user's directory and apply the colors from it. If there is no such file, then the colors from ubuntu, which I demonstrated in the examples above, will be applied. The .Xresources file is a standard way for * nix to store terminal settings, in particular color schemes. If you google, then you can find beautiful ready-made schemes.
Working with shortcuts is a bit more complicated - to modify the shortcut settings, you need to call the utility with the command line switch '--update-link' and the full path to the shortcut. The shortcuts of the programs attached to the taskbar in Windows are stored in the directory "% USERPROFILE% \ AppData \ Roaming \ Microsoft \ Internet Explorer \ Quick Launch \ User Pinned \ TaskBar". An example of modifying a powershell shortcut attached to a taskbar:
pywincmdtheme --update-link "%USERPROFILE%\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Windows PowerShell.lnk"
Look like that's it. Once again with the upcoming all, I hope someone the above will come in handy :).