How do I print out in the standard output coloured lines

C

cniharral

Hi,

I'm interested in printing out coloured lines of my application and
I don't know what to use. Can anybody give me an idea??

Regards.

Carlos Niharra López
 
R

rzed

(e-mail address removed) wrote in

Hi,

I'm interested in printing out coloured lines of my
application and
I don't know what to use. Can anybody give me an idea??

You could speed up the process if you explain what your application
is and what you mean by colored lines. Does your application emit
output to a plotter, an ink-jet printer, or a color laser printer? Is
it a drawing program? An editor in which you want lines colored to
highlight context? It might be useful to know what system you are
running as well. Just a little detail here.
 
C

cniharral

(e-mail address removed) wrote in


You could speed up the process if you explain what your application
is and what you mean by colored lines. Does your application emit
output to a plotter, an ink-jet printer, or a color laser printer? Is
it a drawing program? An editor in which you want lines colored to
highlight context? It might be useful to know what system you are
running as well. Just a little detail here.

Well, yes, it's a program that prints out lines to the standard output
with a print command, and I want to print them coloured. For example:

print "Hello World!!"

I want it in red colour.

That's all.
 
R

rzed

(e-mail address removed) wrote in

Well, yes, it's a program that prints out lines to the standard
output with a print command, and I want to print them coloured.
For example:

print "Hello World!!"

I want it in red colour.

That's all.

If you're on Linux, you could use the curses module. There may be
a precompiled Windows version compatible with your Python version,
or maybe not, but the Windows source is available, and you may be
able to get it to work with your Python with some effort. Linux
distros include curses, I think. For Windows curses, take a look
at <http://adamv.com/dev/python/curses/>. You will understand why
the phrase "Windows curses" is used, I expect.
 
B

Bart Van Loon

It said:
print "Hello World!!"

I want it in red colour.

That's all.

Use colour escape codes:

print "\033[1;31mHello World\033[0m"

That's all. :)

--
groetjes,
BBBart

Golly, I'd hate to have a kid like me!
-- Calvin
 
C

cniharral

(e-mail address removed) wrote in







If you're on Linux, you could use the curses module. There may be
a precompiled Windows version compatible with your Python version,
or maybe not, but the Windows source is available, and you may be
able to get it to work with your Python with some effort. Linux
distros include curses, I think. For Windows curses, take a look
at <http://adamv.com/dev/python/curses/>. You will understand why
the phrase "Windows curses" is used, I expect.

Yes, I'm on a Linux box. I've tried with the curses module, but I
don't how I could fetch the current use of curses of my shell. I don't
know if I'm talking about something impossible. I've made some tests
with the curses module and works fine, but I need to capture the
current window and change the attributes of texts.

Carlos Niharra López
 
C

cniharral

It said:
print "Hello World!!"
I want it in red colour.
That's all.

Use colour escape codes:

print "\033[1;31mHello World\033[0m"

That's all. :)

--
groetjes,
BBBart

Golly, I'd hate to have a kid like me!
-- Calvin


Well, this is fine. It's what I was looking for. I have done something
similar with a system command execution (echo) but it was not really
smart, but this fit my needs.

Thanks a lot.

Regards.

Carlos Niharra López
 
N

Neil Cerutti

(e-mail address removed) wrote in



If you're on Linux, you could use the curses module. There may
be a precompiled Windows version compatible with your Python
version, or maybe not, but the Windows source is available, and
you may be able to get it to work with your Python with some
effort. Linux distros include curses, I think. For Windows
curses, take a look at <http://adamv.com/dev/python/curses/>.
You will understand why the phrase "Windows curses" is used, I
expect.

On Windows, there's pdcurses for DOS or ncurses for the Cygwin
platform, but I don't know how to get either to work with Python.

Far simpler to get working in Windows is Fredrik Lundh's Console.

http://www.effbot.org/downloads/#console

If you're using Windowd 98 or earlier there are versions of a
Python readline library that provide cursor addressing and color
using the ANSI excape sequences.
 
M

Miki

Hello Carlos,
I'm interested in printing out coloured lines of my application and
I don't know what to use. Can anybody give me an idea??
I use the following script:
#!/usr/bin/env python
'''Print message using ANSI terminal codes'''

__author__ = "Miki Tebeka <[email protected]>"
# $Id: ansiprint 1229 2005-05-16 05:50:22Z mikit $

# =====================================================
# Copyright (c) Miki Tebeka <[email protected]>
# This file is under the GNU Public License (GPL), see
# http://www.gnu.org/copyleft/gpl.html for more details
# =====================================================

from sys import stdout, stderr

# Format
bright = 1
dim = 2
underline = 4
blink = 5
reverse = 7
hidden = 8

# Forground
black = 30
red = 31
green = 32
yellow = 33
blue = 34
magenta = 35
cyan = 36
white = 37

# Background
on_black = 40
on_red = 41
on_green = 42
on_yellow = 43
on_blue = 44
on_magenta = 45
on_cyan = 46
on_white = 47

def ansiformat(msg, *args):
'''Format msg according to args.

See http://www.termsys.demon.co.uk/vtansi.htm for more details/
'''
return "\033[%sm%s\033[0m" % (";".join(["%s" % f for f in args]),
msg)

def ansiprint(msg, *args, **kw):
'''Print formatted message.

Should work on ANSI compatible terminal.
'''

if kw.get("stderr", 0):
outfo = stderr
else:
outfo = stdout

outfo.write(ansiformat(msg, *args))
outfo.flush()

if __name__ == "__main__":
from sys import argv, exit
from os.path import basename

h = {
"bright" : bright,
"dim" : dim,
"underline" : underline,
"blink" : blink,
"reverse" : reverse,
"hidden" : hidden,
"black" : black,
"red" : red,
"green" : green,
"yellow" : yellow,
"blue" : blue,
"magenta" : magenta,
"cyan" : cyan,
"white" : white,
"on_black" : on_black,
"on_red" : on_red,
"on_green" : on_green,
"on_yellow" : on_yellow,
"on_blue" : on_blue,
"on_magenta" : on_magenta,
"on_cyan" : on_cyan,
"on_white" : on_white
}

eg = "e.g. ansiprint hello red on_green underline -> %s" % \
ansiformat("hello", red, on_green, underline)

# Check command line
if len(argv) < 2:
print >> stderr, "usage: %s message [format ...]" %
basename(argv[0])
print >> stderr, eg
exit(1)
for i in argv[2:]:
if i not in h:
ansiprint("%s: Unknown format\n" % i, red, bright,
stderr=True)
print >> stderr, "Formats can be:",
msg = ", ".join([ansiformat(f, h[f]) for f in h.keys()])
print msg
print >> stderr, eg
exit(1)

# Print
ansiprint(argv[1], *[h for i in argv[2:]])
print

# vim: ft=python

Hope you find it useful.
 
G

Geoffrey Clements

(e-mail address removed) wrote
in


[snip]


If you're on Linux, you could use the curses module. There may be
a precompiled Windows version compatible with your Python version,
or maybe not, but the Windows source is available, and you may be
able to get it to work with your Python with some effort. Linux
distros include curses, I think. For Windows curses, take a look
at <http://adamv.com/dev/python/curses/>. You will understand why
the phrase "Windows curses" is used, I expect.

--
rzed

Yes, I'm on a Linux box. I've tried with the curses module, but I
don't how I could fetch the current use of curses of my shell. I don't
know if I'm talking about something impossible. I've made some tests
with the curses module and works fine, but I need to capture the
current window and change the attributes of texts.

You may find the following useful, it's from Gentoo's portage:
http://sources.gentoo.org/viewcvs.py/portage/main/trunk/pym/portage/output.py?rev=5780&view=markup
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top