Mailing List Archive

print color strings?
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?

regards,
Gerrit.

--
The Dutch Linuxgames homepage: http://linuxgames.nl.linux.org
Personal homepage: http://www.nl.linux.org/~gerrit/

Discoverb is a python program (in several languages) which tests the words you
learned by asking it. Homepage: http://www.nl.linux.org/~gerrit/discoverb/
Oh my god! They killed init! You bastards!
print color strings? [ In reply to ]
Gerrit Holl writes:
> 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?

Gerrit,
If you're doing this on Unix, take a look at the ncurses library;
there's a Python interface to that. (I think Oliver Andrich did it;
searching DejaNews should find it fairly quickly since this keeps
coming up.)
The standard curses module will be documented in the next release of
the Library Reference.


-Fred

--
Fred L. Drake, Jr. <fdrake@acm.org>
Corporation for National Research Initiatives
print color strings? [ In reply to ]
From: "Fred L. Drake" <fdrake@cnri.reston.va.us>


Gerrit Holl writes:
> 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?

Gerrit,
If you're doing this on Unix, take a look at the ncurses library;
there's a Python interface to that. (I think Oliver Andrich did it;
searching DejaNews should find it fairly quickly since this keeps
coming up.)
The standard curses module will be documented in the next release of
the Library Reference.


-Fred

--
Fred L. Drake, Jr. <fdrake@acm.org>
Corporation for National Research Initiatives
print color strings? [ In reply to ]
"Fred L. Drake" <fdrake@cnri.reston.va.us> writes:
|Gerrit Holl writes:
|> 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?
|
| Gerrit,
| If you're doing this on Unix, take a look at the ncurses library;
| there's a Python interface to that. (I think Oliver Andrich did it;
| searching DejaNews should find it fairly quickly since this keeps
| coming up.)
| The standard curses module will be documented in the next release of
| the Library Reference.

And if that doesn't work, probably your termcap or terminfo definitions
lack the necessary information about color escape sequences. That's a
widespread problem.

The color escape sequences that have worked for me on a couple of terminal
types have been of the form <ESC>[32m, where 32 is a number from 30 to 37.
I use only 31 to 36. 30 and 37 are more or less black and white, but the
details vary, so I switch from color to non-color with <ESC>[m instead of
trying to sort out the details of black and white vs. background and
foreground.

Of course these are terminal specific escape sequences. They're evil and
you must not use them, especially if you have a choice.

Donn Cave, University Computing Services, University of Washington
donn@u.washington.edu
print color strings? [ In reply to ]
From: donn@u.washington.edu (Donn Cave)

"Fred L. Drake" <fdrake@cnri.reston.va.us> writes:
|Gerrit Holl writes:
|> 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?
|
| Gerrit,
| If you're doing this on Unix, take a look at the ncurses library;
| there's a Python interface to that. (I think Oliver Andrich did it;
| searching DejaNews should find it fairly quickly since this keeps
| coming up.)
| The standard curses module will be documented in the next release of
| the Library Reference.

And if that doesn't work, probably your termcap or terminfo definitions
lack the necessary information about color escape sequences. That's a
widespread problem.

The color escape sequences that have worked for me on a couple of terminal
types have been of the form <ESC>[32m, where 32 is a number from 30 to 37.
I use only 31 to 36. 30 and 37 are more or less black and white, but the
details vary, so I switch from color to non-color with <ESC>[m instead of
trying to sort out the details of black and white vs. background and
foreground.

Of course these are terminal specific escape sequences. They're evil and
you must not use them, especially if you have a choice.

Donn Cave, University Computing Services, University of Washington
donn@u.washington.edu
print color strings? [ In reply to ]
Gerrit Holl <gerrit.holl@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
print color strings? [ In reply to ]
From: Gerrit Holl <gerrit.holl@pobox.com>

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?

regards,
Gerrit.

--
The Dutch Linuxgames homepage: http://linuxgames.nl.linux.org
Personal homepage: http://www.nl.linux.org/~gerrit/

Discoverb is a python program (in several languages) which tests the words you
learned by asking it. Homepage: http://www.nl.linux.org/~gerrit/discoverb/
Oh my god! They killed init! You bastards!
print color strings? [ In reply to ]
From: Michael Hudson <mwh21@cam.ac.uk>

Gerrit Holl <gerrit.holl@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
print color strings? [ In reply to ]
On Thu, Jul 01, 1999 at 09:55:55PM +0000, Donn Cave wrote:
> "Fred L. Drake" <fdrake@cnri.reston.va.us> writes:
> |Gerrit Holl writes:
> |> 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?
> |
> | Gerrit,
> | If you're doing this on Unix, take a look at the ncurses library;
> | there's a Python interface to that. (I think Oliver Andrich did it;
> | searching DejaNews should find it fairly quickly since this keeps
> | coming up.)
> | The standard curses module will be documented in the next release of
> | the Library Reference.
>
> And if that doesn't work, probably your termcap or terminfo definitions
> lack the necessary information about color escape sequences. That's a
> widespread problem.
>
> The color escape sequences that have worked for me on a couple of terminal
> types have been of the form <ESC>[32m, where 32 is a number from 30 to 37.
> I use only 31 to 36. 30 and 37 are more or less black and white, but the
> details vary, so I switch from color to non-color with <ESC>[m instead of
> trying to sort out the details of black and white vs. background and
> foreground.
>

Ah, thanks. I was forgotten that would work on python the same as in the
bash-shell...

> Of course these are terminal specific escape sequences. They're evil and
> you must not use them, especially if you have a choice.
>

Thanks. They're only for my sys.ps1, so it'll be good enough for me..,

But I thought there might be a simpler interface than a whole curses library,
but there's not.

regards,
Gerrit.

--
The Dutch Linuxgames homepage: http://linuxgames.nl.linux.org
Personal homepage: http://www.nl.linux.org/~gerrit/

Discoverb is a python program (in several languages) which tests the words you
learned by asking it. Homepage: http://www.nl.linux.org/~gerrit/discoverb/
Oh my god! They killed init! You bastards!
print color strings? [ In reply to ]
From: Gerrit Holl <gerrit.holl@pobox.com>

On Thu, Jul 01, 1999 at 09:55:55PM +0000, Donn Cave wrote:
> "Fred L. Drake" <fdrake@cnri.reston.va.us> writes:
> |Gerrit Holl writes:
> |> 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?
> |
> | Gerrit,
> | If you're doing this on Unix, take a look at the ncurses library;
> | there's a Python interface to that. (I think Oliver Andrich did it;
> | searching DejaNews should find it fairly quickly since this keeps
> | coming up.)
> | The standard curses module will be documented in the next release of
> | the Library Reference.
>
> And if that doesn't work, probably your termcap or terminfo definitions
> lack the necessary information about color escape sequences. That's a
> widespread problem.
>
> The color escape sequences that have worked for me on a couple of terminal
> types have been of the form <ESC>[32m, where 32 is a number from 30 to 37.
> I use only 31 to 36. 30 and 37 are more or less black and white, but the
> details vary, so I switch from color to non-color with <ESC>[m instead of
> trying to sort out the details of black and white vs. background and
> foreground.
>

Ah, thanks. I was forgotten that would work on python the same as in the
bash-shell...

> Of course these are terminal specific escape sequences. They're evil and
> you must not use them, especially if you have a choice.
>

Thanks. They're only for my sys.ps1, so it'll be good enough for me..,

But I thought there might be a simpler interface than a whole curses library,
but there's not.

regards,
Gerrit.

--
The Dutch Linuxgames homepage: http://linuxgames.nl.linux.org
Personal homepage: http://www.nl.linux.org/~gerrit/

Discoverb is a python program (in several languages) which tests the words you
learned by asking it. Homepage: http://www.nl.linux.org/~gerrit/discoverb/
Oh my god! They killed init! You bastards!