|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Shell color function
I have the following:
Code:
def colorize(data,color):
close = "^[0m"
if color == "red":
start = "^[31m"
return start + data + close
But, it doesn't actually work. It's like it doesn't like the escape string or something.. and it just prints them in the output. Any idea? Thanks ![]() |
|
#2
|
||||
|
||||
|
ANSI graphics... haven't seen that in quite a while. While it should work in a command console or Unix shell, I'm not at all certain if it would work in an IDE such as IDLE or Dr Python.
As for the control sequence itself: the caret is actually a representation of the ASCII ESC character (Hex 1B). Writing in '^' won't do this; you would get the caret character itself, not the actual escape. You will need to use the numeric escape sequence for the character, though if you use it more than a few times you would probably want it as a defined constant and concatenate it to the code itself to make it easier to read (or else have the codes themselves as defined constants). The general form an an ANSI graphics control sequence is CSI + code number(s) [ + ';' + second code number + ] + letter operation code (The semicolon and second code number is only used in a few of the control sequences.) so in this case, the number 0 represents resetting the text properties, while 31 represents setting the color to red. You can take advantage of this with judicious use of named constants and string concatenation: Code:
ESC = '\x1B' # ASCII escape character in hex
CSI = ESC + '[' # Control Sequence Initiator for ANSI graphics
TEXT_PROP = 'm' # control code for setting the text properties
TEXT_COLOR = '3' # color setting code
SET_COLOR = CSI + TEXT_COLOR
RESET_COLOR = CSI + '0' + TEXT_PROP
BLACK = '0'
RED = '1'
# etc...
def colorize(data,color):
start = SET_COLOR + color + TEXT_PROP
return start + data + RESET_COLOR
if __name__ == "__main__":
print colorize("Hello,", RED), "World!"
In this example I intentionally separated it out more than it needed to be to illustrate the actual escape sequences, but as you can see, it makes the whole issue of selecting colors much, much easier this way. I've tested this in Linux, and it works correctly in a BASH terminal; running it in the interaction windows of IDLE and DrPython shows a representation of the escape character, however.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF #define KINSEY (rand() % 7) λ Scheme is the Red Pill Scheme in Short • Understanding the C/C++ Preprocessor Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1 FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov Last edited by Schol-R-LEA : May 2nd, 2008 at 11:28 AM. |
|
#3
|
|||
|
|||
|
That's GREAT! Thanks
![]() Last edited by skeeter144 : May 5th, 2008 at 01:49 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Shell color function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|