I decided to make this library not too long ago, as my vanilla command line doesn't have the capability to use multiple colors(thanks, microsoft). Naturally, I decided to break the rules.
Commands:
papyrus.color(c) - Changes the display color for the outputted code
papyrus.reset() - Resets the color to 'DEFAULT'
Just a quick note: I included some exit handling, so you don't need to reset at the end of your file (be sure to set your DEFAULT, speaking of).
Source:
1.3.2
import ctypes
import struct
import atexit
#Set DEFAULT to your default commandline color (WHITE is default)
DEFAULT="WHITE"
STD_OUTPUT_HANDLE=-11
colors={"BLACK": 0x0000,
"BLUE": 0x0001,
"GREEN": 0x0002,
"AQUA": 0x0003,
"RED": 0x0004,
"PURPLE": 0x0005,
"GOLD": 0x0006,
"L_GRAY": 0x0007,
"GRAY": 0x0008,
"L_BLUE": 0x0009,
"LIME": 0x000a,
"L_AQUA": 0x000b,
"L_RED": 0x000c,
"PINK": 0x000d,
"YELLOW": 0x000e,
"WHITE": 0x000f}
def get_csbi_attributes(handle):
csbi=ctypes.create_string_buffer(22)
res=ctypes.windll.kernel32.GetConsoleScreenBufferInfo(handle,csbi)
assert res
def set_attr(a,b):
ctypes.windll.kernel32.SetConsoleTextAttribute(a,b)
handle=ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
reset=get_csbi_attributes(handle)
def color(c):
set_attr(handle, colors[c.upper()])
def reset():
set_attr(handle, colors[DEFAULT])
atexit.register(reset)
1.3.8
- Got rid of a bunch of useless shit that was needed in earlier versions, forgot to remove it before posting.
- Made some stuff private
from ctypes import windll from atexit import register #Set DEFAULT to your default commandline color (WHITE is default) DEFAULT="WHITE" __STD_OUTPUT_HANDLE=-11 __HANDLE=windll.kernel32.GetStdHandle(__STD_OUTPUT_HANDLE) __COLORS={"BLACK": 0x0000, "BLUE": 0x0001, "GREEN": 0x0002, "AQUA": 0x0003, "RED": 0x0004, "PURPLE": 0x0005, "GOLD": 0x0006, "L_GRAY": 0x0007, "GRAY": 0x0008, "L_BLUE": 0x0009, "LIME": 0x000a, "L_AQUA": 0x000b, "L_RED": 0x000c, "PINK": 0x000d, "YELLOW": 0x000e, "WHITE": 0x000f} #actually changes the color def __set(a,b): windll.kernel32.SetConsoleTextAttribute(a,b) #publically called methods, both refer to __set def color(c): __set(__HANDLE,__COLORS[c.upper()]) def reset(): __set(__HANDLE,__COLORS[DEFAULT]) register(reset)
Example usage:
from papyrus import *
color('lime') #case is ignored
print "Hello, world!"
print "Hello, world!".format(color('purple'))
Screenshot:

End notes:
I was too lazy to upload the .pyc, but it'll be compiled when you import PaPYrus to any of your programs.
Not all of this is my code. I got some help from StackOverflow, but most of it is from scratch.
If anyone can find some cool tricks that I haven't discovered, let me know.
