Improve AttributedText constructor usability
The AttributedText constructor now allows setting attributes as a dict via the "attributes" argument. It also interprets all other named arguments as attributes, which override the attributes set via the "attributes" argument.
This commit is contained in:
parent
3a542caac7
commit
4a1db74245
1 changed files with 20 additions and 2 deletions
|
|
@ -104,10 +104,28 @@ class AttributedText:
|
||||||
|
|
||||||
# Common special methods
|
# Common special methods
|
||||||
|
|
||||||
def __init__(self, text: Optional[str] = None) -> None:
|
def __init__(self,
|
||||||
|
text: Optional[str] = None,
|
||||||
|
attributes: Attributes = {},
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
text - the content of the AttributedText, omitting it results in the
|
||||||
|
AttributedText equivalent to an empty string
|
||||||
|
|
||||||
|
attributes - a dict of attributes that apply to the whole
|
||||||
|
AttributedText
|
||||||
|
|
||||||
|
**kwargs - all named arguments besides "attributes" are interpreted
|
||||||
|
as attributes that override any options set in the "attributes" dict
|
||||||
|
"""
|
||||||
|
|
||||||
|
attributes = dict(attributes)
|
||||||
|
attributes.update(kwargs)
|
||||||
|
|
||||||
self._chunks: List[Chunk] = []
|
self._chunks: List[Chunk] = []
|
||||||
if text is not None:
|
if text is not None:
|
||||||
self._chunks.append(Chunk(text))
|
self._chunks.append(Chunk(text, attributes=attributes))
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.text
|
return self.text
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue