From 4a1db74245dbd06ccdf182a73e1db02a0c67aa1e Mon Sep 17 00:00:00 2001 From: Joscha Date: Tue, 14 May 2019 16:03:23 +0000 Subject: [PATCH] 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. --- cheuph/markup.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cheuph/markup.py b/cheuph/markup.py index 88435f5..b4b145f 100644 --- a/cheuph/markup.py +++ b/cheuph/markup.py @@ -104,10 +104,28 @@ class AttributedText: # 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] = [] if text is not None: - self._chunks.append(Chunk(text)) + self._chunks.append(Chunk(text, attributes=attributes)) def __str__(self) -> str: return self.text