[Twisted-Python] Conch text attribute flattening quirks

Tim Allen screwtape at froup.com
Wed Feb 22 10:25:56 EST 2012

On Wed, Feb 22, 2012 at 01:47:46PM +0200, Jonathan Jacobs wrote:
> >>> from twisted.conch.insults.helper import CharacterAttribute
> >>> from twisted.conch.insults.text import flatten, attributes as A
> >>> flatten(A.normal['hello', A.bold[' world '], 'quux'], CharacterAttribute())
> 'hello\x1b[1m world quux'
> My expectations are that only " world " will be marked up with bold
> attributes, since it is the only piece of content in the "bold"
> attribute, while "hello" and "quux" both appear in normal text, i.e.
> without any additional markup. Looking at the output you can see that
> "hello" appears as normal text and then " world quux" appears in bold.

Since it's in twisted.conch, I'm guessing that this character-attribute
stuff is designed to model the VT100 character attribute system, rather
than some generic tree-of-strings-and-attributes. For example, somebody
used to the way HTML works might want to nest bold and italics like

    <i>hello <b>world</b> quux</i>

However, to achieve the same result on a traditional terminal (and using
the tput(1) command to produce the formatting codes), you'd have to do
something like this:

    tput sitm	    # enable italics
    echo -n "hello "
    tput bold	    # enable bold
    echo -n "world"
    tput sgr0	    # disable all special attributes
    tput sitm	    # enable italics again
    echo " quux"

...that is, there's no code for 'end bold' or 'end italics' (or blink, dim,
underline, invisible, any kind of colouring, etc.) just an 'end all
special attributes' code. Therefore it's reasonable for conch's helper
library to not handle nested formatting, since no terminal program will
produce such a thing, because it's impossible to represent in the
VT100/VT200 formatting language.

I guess an argument could be made that the helper function should track
which attributes are enabled at any particular point in the string, and
calculate the correct sequence of disable-everything/re-enable-the-
remaining-attributes codes, but evidently nobody's needed such a thing

More information about the Twisted-Python mailing list