Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old May 2nd, 2008, 10:25 AM
skeeter144 skeeter144 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 30 skeeter144 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 51 m 50 sec
Reputation Power: 3
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

Reply With Quote
  #2  
Old May 2nd, 2008, 10:56 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,038 Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 8 h 32 m 33 sec
Reputation Power: 330
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 ShortUnderstanding the C/C++ Preprocessor
Taming PythonA 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.

Reply With Quote
  #3  
Old May 5th, 2008, 11:24 AM
skeeter144 skeeter144 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 30 skeeter144 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 51 m 50 sec
Reputation Power: 3
That's GREAT! Thanks

Last edited by skeeter144 : May 5th, 2008 at 01:49 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Shell color function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway