Add AttributedTextWidget

This commit is contained in:
Joscha 2019-05-14 16:15:39 +00:00
parent f222b6a390
commit a80f4c3788
3 changed files with 43 additions and 0 deletions

View file

@ -6,6 +6,7 @@ from .exceptions import *
from .markup import * from .markup import *
from .tree_display import * from .tree_display import *
from .tree_list import * from .tree_list import *
from .widgets import *
__all__: List[str] = [] __all__: List[str] = []
__all__ += element.__all__ __all__ += element.__all__
@ -14,3 +15,4 @@ __all__ += exceptions.__all__
__all__ += markup.__all__ __all__ += markup.__all__
__all__ += tree_display.__all__ __all__ += tree_display.__all__
__all__ += tree_list.__all__ __all__ += tree_list.__all__
__all__ += widgets.__all__

View file

@ -0,0 +1,6 @@
from typing import List
from .attributed_text_widget import *
__all__: List[str] = []
__all__ += attributed_text_widget.__all__

View file

@ -0,0 +1,35 @@
from typing import Any
import urwid
from ..markup import AttributedText
__all__ = ["AttributedTextWidget", "ATWidget"]
class AttributedTextWidget(urwid.Text):
"""
A widget that works like urwid.Text, but displays AttributedText.
The AttributedText's "style" attribute is used as the style for urwid.Text,
where present.
"""
def __init__(self,
text: AttributedText,
*args: Any,
**kwargs: Any
) -> None:
"""
text - an AttributedText object
All other arguments are passed onto a urwid.Text constructor and thus
work the same way.
"""
chunk_info = [
chunk.text if style is None else (style, chunk.text)
for chunk, style in text.split_by("style")
]
super().__init__(chunk_info, *args, **kwargs)
ATWidget = AttributedTextWidget