Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How do I print out in the standard output coloured lines

Reply
Thread Tools

How do I print out in the standard output coloured lines

 
 
cniharral@gmail.com
Guest
Posts: n/a
 
      02-02-2007
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

 
Reply With Quote
 
 
 
 
rzed
Guest
Posts: n/a
 
      02-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in
news:(E-Mail Removed) oups.com:

> 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.

--
rzed
 
Reply With Quote
 
 
 
 
cniharral@gmail.com
Guest
Posts: n/a
 
      02-02-2007
On Feb 2, 1:16 pm, rzed <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote innews:(E-Mail Removed) groups.com:
>
> > 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.
>
> --
> rzed


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.

 
Reply With Quote
 
rzed
Guest
Posts: n/a
 
      02-02-2007
(E-Mail Removed) wrote in
news:(E-Mail Removed) ups.com:

> On Feb 2, 1:16 pm, rzed <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote
>> innews:(E-Mail Removed) groups.com:
>>
>> > 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.
>>
>> --
>> rzed

>
> 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.

--
rzed
 
Reply With Quote
 
Bart Van Loon
Guest
Posts: n/a
 
      02-02-2007
It was 2 Feb 2007 04:27:06 -0800, when (E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
cniharral@gmail.com
Guest
Posts: n/a
 
      02-02-2007
On Feb 2, 1:38 pm, rzed <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote innews:(E-Mail Removed) roups.com:
>
>
>
> > On Feb 2, 1:16 pm, rzed <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote
> >> innews:(E-Mail Removed) groups.com:

>
> >> > 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.

>
> >> --
> >> rzed

>
> > 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.
>
> --
> 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.

Carlos Niharra López

 
Reply With Quote
 
cniharral@gmail.com
Guest
Posts: n/a
 
      02-02-2007
On Feb 2, 2:08 pm, Bart Van Loon <(E-Mail Removed)> wrote:
> It was 2 Feb 2007 04:27:06 -0800, when (E-Mail Removed) wrote:
>
> > 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

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      02-02-2007
On 2007-02-02, rzed <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote in
> news:(E-Mail Removed) ups.com:
>
>> On Feb 2, 1:16 pm, rzed <(E-Mail Removed)> wrote:
>>> (E-Mail Removed) wrote
>>> innews:(E-Mail Removed) groups.com:
>>>
>>> > 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.
>>>
>>> --
>>> rzed

>>
>> 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.


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.

--
Neil Cerutti
 
Reply With Quote
 
Miki
Guest
Posts: n/a
 
      02-02-2007
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 <(E-Mail Removed)>"
# $Id: ansiprint 1229 2005-05-16 05:50:22Z mikit $

# ================================================== ===
# Copyright (c) Miki Tebeka <(E-Mail Removed)>
# 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[i] for i in argv[2:]])
print

# vim: ft=python

Hope you find it useful.
--
Miki <(E-Mail Removed)>
http://pythonwise.blogspot.com

 
Reply With Quote
 
Geoffrey Clements
Guest
Posts: n/a
 
      02-02-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
On Feb 2, 1:38 pm, rzed <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote
> innews:(E-Mail Removed) roups.com:
>
>
>
> > On Feb 2, 1:16 pm, rzed <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote
> >> innews:(E-Mail Removed) groups.com:

>
> >> > 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??

>


[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...80&view=markup

--
Geoff


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Differently Coloured Text in one textual area arun.hallan@gmail.com Java 2 02-11-2005 10:33 AM
How to do a coloured line Thibault HTML 9 10-11-2004 10:08 AM
Coloured Buttons Venkat HTML 3 07-01-2003 08:24 AM



Advertisments