PaPYrus

by Nevermore

Nevermore
:(){:|:&};:
Music
Posts: 8
Threads: 4
Joined: Oct 2015
Reputation: 2
10-28-2015, 08:05 PM
#24204 (1)
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:
Code:
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

Spoiler

Code:
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

Spoiler

- Got rid of a bunch of useless shit that was needed in earlier versions, forgot to remove it before posting.
- Made some stuff private
Code:
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:
Code:
from papyrus import *

color('lime') #case is ignored
print "Hello, world!"

print "Hello, world!".format(color('purple'))

Screenshot:
[Image: 687474703a2f2f692e696d6775722e636f6d2f78...4e2e706e67]

End notes:
  1. I was too lazy to upload the .pyc, but it'll be compiled when you import PaPYrus to any of your programs.
  2. Not all of this is my code. I got some help from StackOverflow, but most of it is from scratch.
  3. If anyone can find some cool tricks that I haven't discovered, let me know.
[+] 3 users love Nevermore's post
Melancholy
すけべ
Admin
Posts: 3,871
Threads: 213
Joined: Jul 2014
Reputation: 60
10-29-2015, 02:57 AM
#24238 (2)
This is a pretty cool library. Awesome work.

Have you considered uploading it to the PyPI as a package?

KazeFlame
Weeab Developer
Kawaii
Posts: 16
Threads: 1
Joined: Nov 2015
Reputation: 0
11-15-2015, 01:38 AM
#28210 (3)
Nice lib! I will use it to log things on console. Thanks for sharing. :3