Results 1 to 2 of 2

Thread: curses and ansi color codes

  1. #1
    Join Date
    Feb 2005
    Location
    Sweden
    Beans
    315

    curses and ansi color codes

    I'm trying to write a program that gets text containing ansi color codes and displays it to the user using curses. But I just can't figure out how to make curses handle the ansi codes correctly.

    Here is an example of my problem. This program prints the text with curses and waits for the user to hit a key. Then exits curses and prints the text normally. The colors doesn't work in curses but do work when printed normally.
    Code:
    #!/usr/bin/env python
    import curses
    def colortest(main_window):
    	main_window.addstr(colorsample)
    	main_window.getch()
    if __name__ == "__main__":
    	colorsample = """This is colored text.
    
    This is normal text.
    This is colored text.
    
    This is normal text.
    This is colored text.
    
    This is normal text.
    """
    	curses.wrapper(colortest)
    	print colorsample

  2. #2
    Join Date
    Aug 2007
    Location
    Oslo
    Beans
    52
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: curses and ansi color codes

    you should let curses figure out the color escape sequenses for your terminal, this is an old thread so a short answer..
    curses is very poorly documented in Python, in good old Python tradition


    try:
    import curses
    except: return #to whatever

    ansi_foreground = curses.tigetstr('setaf')

    if(ansi_foreground):
    ansi_black_fg = curses.tparm(ansi_foreground, 1)
    ansi_blue_fg = cuses.tparm(ansi_foreground, 2)
    #
    #

    print ansi_blue_fg+"i'm blue"

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •