From 8c5f7d75cfd85af486cecb8bc1b50c469123ffa9 Mon Sep 17 00:00:00 2001 From: Pier CeccoPierangioliEugenio Date: Sat, 26 Jul 2025 10:34:52 +0100 Subject: [PATCH] feat(TextDocument): append on an empty document replace the text (#429) --- libs/pyTermTk/TermTk/TTkGui/textdocument.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/pyTermTk/TermTk/TTkGui/textdocument.py b/libs/pyTermTk/TermTk/TTkGui/textdocument.py index 81972c5d..1ed4eb8f 100644 --- a/libs/pyTermTk/TermTk/TTkGui/textdocument.py +++ b/libs/pyTermTk/TermTk/TTkGui/textdocument.py @@ -126,6 +126,7 @@ class TTkTextDocument(): lines[d._i1:d._i2] = d._slice return d._snap + _default_init_text = ' ' __slots__ = ( '_dataLines', '_modified', @@ -140,7 +141,7 @@ class TTkTextDocument(): 'undoAvailable', 'redoAvailable', 'undoCommandAdded', 'modificationChanged' ) - def __init__(self, *, text:TTkString=" ") -> None: + def __init__(self, *, text:Union[TTkString,str]=_default_init_text) -> None: from TermTk.TTkGui.textcursor import TTkTextCursor self._docMutex = Lock() self.cursorPositionChanged = pyTTkSignal(TTkTextCursor) @@ -254,6 +255,8 @@ class TTkTextDocument(): def appendText(self, text): if type(text) == str: text = TTkString() + text + if len(self._dataLines) == 1 and self._dataLines[0] == TTkString(TTkTextDocument._default_init_text): + return self.setText(text) self._acquire() oldLines = len(self._dataLines) self._dataLines += text.split('\n')