Add AttributedTextWidget
This commit is contained in:
parent
f222b6a390
commit
a80f4c3788
3 changed files with 43 additions and 0 deletions
6
cheuph/widgets/__init__.py
Normal file
6
cheuph/widgets/__init__.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from typing import List
|
||||
|
||||
from .attributed_text_widget import *
|
||||
|
||||
__all__: List[str] = []
|
||||
__all__ += attributed_text_widget.__all__
|
||||
35
cheuph/widgets/attributed_text_widget.py
Normal file
35
cheuph/widgets/attributed_text_widget.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue