[Urwid] How to use ANSI color sequences with urwid?

Ian Ward ian at excess.org
Thu Aug 14 13:49:45 EDT 2008

Friedrich Weber wrote:
> Hello,
> is it possible to use ansi color escape sequences (like "\033[31m") in urwid
> Text widgets? I'd like to use pygments console output formatter for syntax
> highlighting.

Not directly.  You would have to convert that to Urwid's text markup
format, eg:

"\033[31mFOO \033[0mBAR" might become: [('attrname', "FOO "), "BAR"]

where 'attrname' is set up as one of the palette entries in your Screen
class.. something like:

    source = "\033[31mFOO\033 [0mBAR"
    table = {"[31":'bluefg', "[0":'default'}
    markup = []
    for at in source.split("\033")[1:]:
        attr, text = at.split("m",1)
        markup.append((table[attr], text))

It might be convenient to subclass urwid.Text to make a class that takes
text formatted by pygments.


More information about the Urwid mailing list