print_color( "This is my text", COLOR_BLUE )
And this should be portable (i.e. it should work on Linux, Mac,
Windows).
...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
> 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>
Generating postscript or PDF is the only remotely portable way
to print anything.
--
Grant
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.
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
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
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
Can you explain what the connection is between my post and yours?