
Happy, colleagues. Everyone knows that for the console in windows you can easily set the text and background color. But few people know that the remaining 14 colors can also be set, greatly simplifying the work with console applications that support colors in windows — for example, git or mercurial. Under the cut, in faces and pictures I will talk about my struggle with colors and the resulting python utility that allows you to color the console with one command, in order not to see this white and blue horror of powershell.
ANSI Colors
')
So, I want to change all the colors in the console, not just the text and background. First, let's see what these colors are. Everything is simple in linux and osx - there is the “ANSI Colors” standard, which says that if you output 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 there is something similar - 16 colors that can be used to color text and background. A simple python program shows what it looks like:
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 sad to say the least. 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 HKEY_CURRENT_USER \ Console key, values ​​from “ColorTable00 (black, it's the background color) to ColorTable15 (bright white). Values: DWORD, 0x00BBGGRR. We change these magic keys and get the same as in Ubuntu. The only difference is that the colors go in a different order:

Goal achieved? It looks like it. But there is ...
Intrigue
We 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 through the hotkey „win + number“. We do the "pin this program to taskbar", launch it, and see IT:

Where did this blue background come from, which becomes black when coloring? After all, if you run the console through "win + r" - we see our colors, set in the previous step. Here the most interesting lies - when we attach a console application to the taskbar, Windows creates a shortcut for it (a file with the .lnk extension) and sets the personal color settings for this shortcut. What to do? We take in hands python, a little COM, and we modify settings of a label. Now it all works.
Promised two clicks
In order not to lose the qualifications of a programmer, I designed all of the above as a small python module that 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 need python, I recommend installing the ActivePython build - besides python, it also contains a number of pre-installed extensions that are useful for working under Windows. How to use the utility to color the console under the 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 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 the standard for * nix method of storing terminal settings, in particular, color schemes. If you google, 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 key '--update-link' and the full path to the shortcut. The labels of 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 the powershell shortcut attached to the 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, happy everyone, I hope someone the above will come in handy :).