[Urwid] Rendering standout attribute across entire Columns widget

Ian Ward ian at excess.org
Thu Sep 1 10:10:53 EDT 2011


Jacob Courtneay wrote on 2011-08-31 23:35:
> Here you go:
> https://github.com/sporkexec/urwid/commits/AttrMap_callable
> 
> It's not exposed in decoration.py yet because I don't know if you would prefer 
> changing AttrMap or adding another class.
> 
> The idea is that you could do something like AttrMap(w, lambda attr: 
> 'prefix.'+attr) to put attributes in a namespace. Or you could force all 
> children widgets to use an attribute, or whatever else anybody comes up with. 
> For what it's worth, this would also remove the requirement that attributes be 
> hashable.


Interesting change.  I think that moving to a slightly more CSS-like
display attribute system might be clearer and easier to use though.

Also in this specific case you can't add strings to AttrSpec instances
(which sometimes are used as display attributes)

How about this:

1. Canvases would start remembering the order that display attributes
were applied

eg:
    w1 = Text(('first', ('second', "Hello")))
    w2 = AttrMap(Text(('first', "Hello")), {'first':'second'})

would both be widgets with display attributes applied to the "Hello"
text in the order 'first', 'second'.

2. Screen palette lookups would match tuples of display attributes in
order from last to first, stopping when no further match is found.

eg:
    palette = [('first', 'yellow', 'black'),
               ('second', light blue', 'black')]

would colour "Hello" light blue as it does now, but:

    palette = [('first', 'yellow', 'black'),
               ('second', light blue', 'black'),
               (('first', 'second'), 'dark green', 'black')]

would colour "Hello" dark green. Note that if there was another display
attribute applied earlier:

    w3 = Text(('zeroth', ('first', ('second', "Hello"))))

The "Hello" text would still be dark green because ('first', 'second')
is the last match found in the palette.

This should cleanly solve the common "in focus" case.

It doesn't go as far as allowing arbitrary combinations like adding
standout to any display attribute.  You could do that with a list
comprehension in the palette definition, similar to the "prefix."+attr
case you site above.

Thoughts?

Ian



More information about the Urwid mailing list