Sinisterly
PaPYrus - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: PaPYrus (/Thread-PaPYrus)



PaPYrus - Inori - 10-06-2015

Seeing as I run winblows, my vanilla command line doesn't have the capability to use multiple colors. Naturally, I decided to fuck with it.

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:
Spoiler: 1.3.2
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)

Spoiler: 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
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: xVbrfnN.png]

End notes:
  1. While PaPYrus is cross-platform, I don't see why you would use it on Linux Tongue I'm retarded. It's windows only.
  2. I was too lazy to upload the .pyc, but it'll be compiled when you import PaPYrus to any of your programs.
  3. Not all of this is my code. I got some help from StackOverflow, but most of it is from scratch.
  4. If anyone can find some cool tricks that I haven't discovered, let me know.



RE: PaPYrus - m0dem - 10-08-2015

Looks fun little library. I've always liked colors in the terminal Biggrin


RE: PaPYrus - Penis - 10-09-2015

Aweome little library, not hating just saying, isn't there other modules out there that do the same thing? Either way nice work Smile