print color strings?
Michael Hudson
mwh21 at cam.ac.uk
Thu Jul 1 19:17:47 EDT 1999
Gerrit Holl <gerrit.holl at pobox.com> writes:
> Hello,
>
> is there an easy way to print color strings in python?
>
> A function like printcolor('brightyellow', 'red', 'spam'), that finds
> out on what kind of terminal you are and prints it to stdout?
That sort of finding out is what terminfo is for; there's no Python
interface that I know of (such a beast shouldn't be too hard to write,
but a bit pointless, I suspect). However there is the `tput' program;
os.system("tput setaf 1")
should turn your foreground colour red.
Read the terminfo man pages for more codes.
You'd probably want to cache the escape codes rather than spinning a
new process each time you need one.
codes['redf'] = os.popen("tput setaf 1").read()
codes['redb'] = os.popen("tput setab 1").read()
codes['greenf']=os.popen("tput setaf 2").read()
...
and so on.
I'm certainly not an expert on such matters; reading man pages for
tput, terminfo and term and playing around will get you exactly where
I've got to.
All terribly unix-centric, of course.
HTH
Michael
More information about the Python-list
mailing list