[Urwid] Rendering standout attribute across entire Columns widget

Ian Ward ian at excess.org
Wed Aug 24 09:22:52 EDT 2011


On 08/24/2011 03:15 AM, Jacob Courtneay wrote:
> Hello,
>
> I'm trying to get a ListBox full of 1-row Columns containing styled Text's to
> render the focused row (or any row for that matter) in standout. Here's my
> smallest possible example:
>
> #!/usr/bin/env python2
> from urwid import MainLoop, Text, Columns, ListBox, AttrMap
> palette = [
>      ('one',       'yellow',          'default'),
>      ('two',       'white',          'default'),
>      ('one.focus', 'yellow,standout', 'default'),
>      ('two.focus', 'white,standout', 'default'),
> ]
>
> def mkcolumns(text, moretext):
>      one = ('weight', 1.0, Text(('col1', text)))
>      two = ('weight', 2.0, Text(('col2', moretext)))

try this instead:

        one = ('weight', 1.0, AttrMap(Text(text), 'col1')))
        two = ('weight', 2.0, AttrMap(Text(moretext), 'col2')))

>      cols = Columns((one, two))
>      return AttrMap(cols,
>                     {'col1': 'one', 'col2': 'two'},
>                     {'col1': 'one.focus', 'col2': 'two.focus'})
>
> cols = mkcolumns('text', 'more text'), mkcolumns('stuff', 'more stuff')
> root = ListBox(cols)
> loop = MainLoop(root, palette)
> loop.run()
> # EOF
>
> http://i.imgur.com/zOdYM.png
> The output can be seen here on the bottom right. My actual application is on the
> left, and the desired behavior on the middle right.
>
> I'm not sure if the Columns is truncating the output, or not rendering the Text
> with all available width, or something else. A standout'ed Text put directly in
> a ListBox renders full width as expected. Am I getting something wrong with
> Columns?

When you use text attributes inline they will follow the text exactly, 
including wrapping and line ends, not extend to fill the whole rendered 
area.  They let you hilight individual words or phrases in a body of 
text.  AttrMap is what you need to colour a whole widget.

>
> Also, is there a simple way to have attributes "inherit" from their parents or
> vice versa? i.e. in the above example, instead of making the row standout is
> there a way to make the entire row bold without remapping every attribute?

Not at the moment, but if you can think of what a good API would look 
like I'd be interested.  Problems include supporting mono/16/88/256 
colour modes equally well, and the fact that some combinations don't 
work very well on some terminals.



More information about the Urwid mailing list