How do I print out in the standard output coloured lines

Discussion in 'Python' started by cniharral@gmail.com, Feb 2, 2007.

  1. Guest

    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
     
    , Feb 2, 2007
    #1
    1. Advertising

  2. rzed Guest

    wrote in
    news::

    > 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
     
    rzed, Feb 2, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 2, 1:16 pm, rzed <> wrote:
    > wrote innews::
    >
    > > 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.
     
    , Feb 2, 2007
    #3
  4. rzed Guest

    wrote in
    news::

    > On Feb 2, 1:16 pm, rzed <> wrote:
    >> wrote
    >> innews::
    >>
    >> > 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
     
    rzed, Feb 2, 2007
    #4
  5. It was 2 Feb 2007 04:27:06 -0800, when 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
     
    Bart Van Loon, Feb 2, 2007
    #5
  6. Guest

    On Feb 2, 1:38 pm, rzed <> wrote:
    > wrote innews::
    >
    >
    >
    > > On Feb 2, 1:16 pm, rzed <> wrote:
    > >> wrote
    > >> innews::

    >
    > >> > 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
     
    , Feb 2, 2007
    #6
  7. Guest

    On Feb 2, 2:08 pm, Bart Van Loon <> wrote:
    > It was 2 Feb 2007 04:27:06 -0800, when 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
     
    , Feb 2, 2007
    #7
  8. Neil Cerutti Guest

    On 2007-02-02, rzed <> wrote:
    > wrote in
    > news::
    >
    >> On Feb 2, 1:16 pm, rzed <> wrote:
    >>> wrote
    >>> innews::
    >>>
    >>> > 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
     
    Neil Cerutti, Feb 2, 2007
    #8
  9. Miki Guest

    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 <>"
    # $Id: ansiprint 1229 2005-05-16 05:50:22Z mikit $

    # =====================================================
    # Copyright (c) Miki Tebeka <>
    # 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.
    --
    Miki <>
    http://pythonwise.blogspot.com
     
    Miki, Feb 2, 2007
    #9
  10. <> wrote in message
    news:...
    On Feb 2, 1:38 pm, rzed <> wrote:
    > wrote
    > innews::
    >
    >
    >
    > > On Feb 2, 1:16 pm, rzed <> wrote:
    > >> wrote
    > >> innews::

    >
    > >> > 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/portage/main/trunk/pym/portage/output.py?rev=5780&view=markup

    --
    Geoff
     
    Geoffrey Clements, Feb 2, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    367
  2. Venkat

    Coloured Buttons

    Venkat, Jun 30, 2003, in forum: HTML
    Replies:
    3
    Views:
    2,757
    Steve Pugh
    Jul 1, 2003
  3. Thibault

    How to do a coloured line

    Thibault, Oct 7, 2004, in forum: HTML
    Replies:
    9
    Views:
    8,177
    Jan Faerber
    Oct 11, 2004
  4. sudip

    coloured text...

    sudip, Jul 1, 2005, in forum: C Programming
    Replies:
    4
    Views:
    413
    Dik T. Winter
    Jul 7, 2005
  5. keto
    Replies:
    0
    Views:
    998
Loading...

Share This Page