Skip to content

Instantly share code, notes, and snippets.

@zdxerr
Created July 9, 2013 09:24
Show Gist options
  • Select an option

  • Save zdxerr/5955956 to your computer and use it in GitHub Desktop.

Select an option

Save zdxerr/5955956 to your computer and use it in GitHub Desktop.
Colorfull command line output on windows (using python > 2.6).
# -*- coding: utf-8 -*-
"""
Colorfull command line output on windows.
"""
from __future__ import print_function
import sys
from ctypes import windll, c_ulong
windll.Kernel32.GetStdHandle.restype = c_ulong
_HANDLE = windll.Kernel32.GetStdHandle(c_ulong(0xfffffff5))
COLORS = {
'dark-blue': 1,
'dark-green': 2,
'dark-turquoise': 3,
'dark-red': 4,
'dark-purple': 5,
'gold': 6,
'light-gray': 7,
'gray': 8,
'blue': 9,
'green': 10,
'turquoise': 11,
'red': 12,
'purple': 13,
'yellow': 14,
'white': 15
}
def print_c(s, end='\n', color='white'):
windll.Kernel32.SetConsoleTextAttribute(_HANDLE, COLORS[color])
print(s, end=end)
windll.Kernel32.SetConsoleTextAttribute(_HANDLE, COLORS['white'])
if __name__ == '__main__':
for color in COLORS:
print_c("{}".format(color), end='', color=color)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment