Python/IDLE - text in different colours
TouTaTis
toutatis at xsFOURall.nl
Wed Jun 29 11:23:16 EDT 2005
"Bill Davy" <Bill at SynectixLtd.com> wrote in
news:d9rfb9$ob7$1 at nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com:
> To make life easier for my users, I'd like to colour my prompt string
> (as handed to raw_input()) a different colour to that produced by
> print. I'm using Python 2.4.1 and IDLE 1.1.1 on Windows XP. Is it
> possible, and if so, how?
> tia,
> Bill
>
>
Communicating with a Program
Say we want the shell to distinguish more clearly, the output of external
programs from the input prompt, the commands, and the shell feedback. We
want the output of external programs to be indented and displayed in a
different colour than the other text.
Setting the colour of the text is fairly easy using ANSI terminal escape
sequences. For instance, to set the text colour to dark red, write "<Esc>
[31;2m" to the terminal (where <Esc> is the escape code in emacs use
"C-q ESC" to write <Esc>). We can reset the output colour using "<Esc>
0m".
Printing the output of external programs in dark red, we can do using the
execute() function:
def runCommand(command):
print 'Running:', command
# set output colour:
sys.stdout.write("<Esc>[31;2m") ; sys.stdout.flush()
os.system(command)
# reset output colour
sys.stdout.write("<Esc>[0m")
(Here we need to flush the stdout file to make sure that the escape code
is written to the terminal before the output of the program)
http://www.daimi.au.dk/~mailund/scripting2005/lecture-notes/process-
management.html
More information about the Python-list
mailing list