Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing with colors in a portable way

5 views
Skip to first unread message

Robert Dailey

unread,
Jul 30, 2009, 6:40:37 PM7/30/09
to
Anyone know of a way to print text in Python 3.1 with colors in a
portable way? In other words, I should be able to do something like
this:

print_color( "This is my text", COLOR_BLUE )

And this should be portable (i.e. it should work on Linux, Mac,
Windows).

Rhodri James

unread,
Jul 30, 2009, 7:29:50 PM7/30/09
to Robert Dailey, pytho...@python.org
On Thu, 30 Jul 2009 23:40:37 +0100, Robert Dailey <rcda...@gmail.com>
wrote:

...in an xterm, over a telnet connection, via VNC...

There's no portable way of doing this in any language, since it's
up to the terminal emulator exactly what it does with what incoming
bytes. Some respect the ANSI colour codes, some don't; that's about
all that you can say.

--
Rhodri James *-* Wildebeest Herder to the Masses

Jan Kaliszewski

unread,
Jul 30, 2009, 7:54:20 PM7/30/09
to Rhodri James, Robert Dailey, pytho...@python.org
31-07-2009 o 01:29:50 Rhodri James <rho...@wildebst.demon.co.uk> wrote:

> On Thu, 30 Jul 2009 23:40:37 +0100, Robert Dailey <rcda...@gmail.com>
> wrote:
>

> ...in an xterm, over a telnet connection, via VNC...
>
> There's no portable way of doing this in any language, since it's
> up to the terminal emulator exactly what it does with what incoming
> bytes. Some respect the ANSI colour codes, some don't; that's about
> all that you can say.

Yeah. Although you could try to provide "term-sensitive" mapping
of colour names -> codes, e.g.:

# color_codes.py

class NoColors:
blue = ''
red = ''

class XtermColors:
blue = '<a code here>'
red = '<a code here>'

class FooBarColors:
blue = '<a code here>'
red = '<a code here>'

COLORS = {
'nocolors': NoColors,
'xterm': XtermColors,
'another term': FooBarColors,
}


# a_program.py

import color_codes

# e.g. with used_term == 'xterm'...
colors = color_codes.COLORS[used_term]

print('Some text, {colors.blue}Something in blue, '
'{colors.red}And now in red.').format(colors=colors)


Regards,
*j

--
Jan Kaliszewski (zuo) <z...@chopin.edu.pl>

Grant Edwards

unread,
Jul 30, 2009, 11:02:17 PM7/30/09
to

Generating postscript or PDF is the only remotely portable way
to print anything.

--
Grant

Nobody

unread,
Jul 31, 2009, 2:22:33 PM7/31/09
to

The way that terminals (and emulators) handle colour is fundamentally
different from the DOS/Windows console. If you want something which will
work on both, you will have write separate implementations for terminals
and the DOS/Windows console.

For terminals, you can use the "curses" package, e.g.:

import curses

curses.setupterm()
setaf = curses.tigetstr('setaf')
setab = curses.tigetstr('setab')

def foreground(num):
if setaf:
sys.stdout.write(curses.tparm(setaf, num))

def background(num):
if setab:
sys.stdout.write(curses.tparm(setab, num))

For the Windows console, you'll need to use ctypes to interface to the
SetConsoleTextAttribute() function from Kernel32.dll.

Aahz

unread,
Aug 2, 2009, 10:52:14 PM8/2/09
to
In article <33e19169-19b4-497a...@r38g2000yqn.googlegroups.com>,

Much as I hate to say, use a cross-platform GUI -- Tkinter comes with
Python, and you can also use SDL via PyGame, wxWindows, pyQT, PyOpenGL,
and so on. If that's not an option, you should have said "command-line
program". ;-)
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings,
it's about treating strings as sequences of characters. The fact that
characters are also strings is the reason we have problems, but characters
are strings for other good reasons." --Aahz

r

unread,
Aug 3, 2009, 3:07:39 AM8/3/09
to
On Aug 2, 9:52 pm, a...@pythoncraft.com (Aahz) wrote:
[snip]

> Much as I hate to say, use a cross-platform GUI -- Tkinter comes with
> Python,
[snip]

Why is Tkinter such a whipping boy of the Python community? I know
it's very simple and does not have all the bells and whistles of wx,
but i think the gentle learning curve is very important for those
struggling to learn GUI's. Even today I always use Tkinter first, and
then only reach for wx when absolutely necessary. Seems to me the
relativity small footprint(GUI wise) compared to the payoffs are worth
the inclusion. I think if Tkinter where ever removed from Python it
would be not only a disservice to the language but also a detriment to
the *new* users of the language.

Just my humble opinion

Jean-Michel Pichavant

unread,
Aug 3, 2009, 6:46:45 AM8/3/09
to Nobody, pytho...@python.org

FYI

http://github.com/jbowes/markymark/blob/59511b36a752b40243cc18fb0fb9800c74549ac1/markymark.py

If the URL ever becomes invalid, then google for markymark.py
You can use it either to color your Linux/Unix terms, or you can just
look at the python code to see how to set colors and attributes.


JM

Aahz

unread,
Aug 5, 2009, 4:11:37 PM8/5/09
to
In article <19e16923-b200-498b...@d32g2000yqh.googlegroups.com>,
r <rt8...@gmail.com> wrote:

>On Aug 2, 9:52=A0pm, a...@pythoncraft.com (Aahz) wrote:
>
>> Much as I hate to say, use a cross-platform GUI -- Tkinter comes with
>> Python,
>
>Why is Tkinter such a whipping boy of the Python community?

Can you explain what the connection is between my post and yours?

0 new messages