2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121# SOFTWARE.
2222
23+ from __future__ import annotations
24+
2325__all__ = ['TTkString' ]
2426
2527import os
2628import re
2729import unicodedata
2830from types import GeneratorType
29- from typing import Any
30-
31- try :
32- from typing import Self
33- except :
34- class Self (): pass
31+ from typing import Any , Optional , Union , List , Tuple
3532
3633from TermTk .TTkCore .cfg import TTkCfg
3734from TermTk .TTkCore .constant import TTkK
@@ -69,11 +66,13 @@ class TTkString():
6966 unicodeWideOverflowColor = TTkColor .fg ("#888888" )+ TTkColor .bg ("#000088" )
7067
7168 __slots__ = ('_text' ,'_colors' ,'_baseColor' ,'_hasTab' ,'_hasSpecialWidth' )
72-
69+ _text :str
70+ _colors :List [TTkColor ]
71+ _baseColor :TTkColor
7372 def __init__ (self ,
74- text :str = "" ,
75- color :TTkColor = None ) -> None :
76- if issubclass ( type ( text ) , TTkString ):
73+ text :Union [ str , TTkString ] = "" ,
74+ color :Optional [ TTkColor ] = None ) -> None :
75+ if isinstance ( text , TTkString ):
7776 self ._text = text ._text
7877 self ._colors = text ._colors if color is None else [color ]* len (self ._text )
7978 self ._baseColor = text ._baseColor
@@ -120,7 +119,7 @@ def __len__(self) -> int:
120119 def __str__ (self ) -> str :
121120 return self ._text
122121
123- def __add__ (self , other :Self ) -> Self :
122+ def __add__ (self , other :TTkString ) -> TTkString :
124123 ret = TTkString ()
125124 ret ._baseColor = self ._baseColor
126125 if isinstance (other , TTkString ):
@@ -142,7 +141,7 @@ def __add__(self, other:Self) -> Self:
142141 ret ._baseColor = other
143142 return ret
144143
145- def __radd__ (self , other :Self ) -> Self :
144+ def __radd__ (self , other :TTkString ) -> TTkString :
146145 ret = TTkString ()
147146 ret ._baseColor = self ._baseColor
148147 if isinstance (other , TTkString ):
@@ -180,7 +179,7 @@ def __ne__(self, other): return self._text != other._text if issubclass(type(oth
180179 def __gt__ (self , other ): return self ._text > other ._text if issubclass (type (other ),TTkString ) else self ._text > other
181180 def __ge__ (self , other ): return self ._text >= other ._text if issubclass (type (other ),TTkString ) else self ._text >= other
182181
183- def sameAs (self , other :Self ) -> bool :
182+ def sameAs (self , other :TTkString ) -> bool :
184183 if not issubclass (type (other ),TTkString ): return False
185184 return (
186185 self == other and
@@ -190,7 +189,7 @@ def sameAs(self, other:Self) -> bool:
190189 def isdigit (self ) -> bool :
191190 return self ._text .isdigit ()
192191
193- def lstrip (self , ch :str ) -> Self :
192+ def lstrip (self , ch :str ) -> TTkString :
194193 ret = TTkString ()
195194 ret ._text = self ._text .lstrip (ch )
196195 ret ._colors = self ._colors [- len (ret ._text ):]
@@ -199,7 +198,7 @@ def lstrip(self, ch:str) -> Self:
199198 def charAt (self , pos :int ) -> str :
200199 return self ._text [pos ]
201200
202- def setCharAt (self , pos :int , char :str ) -> Self :
201+ def setCharAt (self , pos :int , char :str ) -> TTkString :
203202 self ._text = self ._text [:pos ]+ char + self ._text [pos + 1 :]
204203 self ._checkWidth ()
205204 return self
@@ -209,11 +208,11 @@ def colorAt(self, pos:int) -> TTkColor:
209208 return TTkColor ()
210209 return self ._colors [pos ]
211210
212- def setColorAt (self , pos , color ) -> Self :
211+ def setColorAt (self , pos , color ) -> TTkString :
213212 self ._colors [pos ] = color
214213 return self
215214
216- def tab2spaces (self , tabSpaces = 4 ) -> Self :
215+ def tab2spaces (self , tabSpaces = 4 ) -> TTkString :
217216 '''Return the string representation with the tabs (converted in spaces) trimmed and aligned'''
218217 if not self ._hasTab : return self
219218 ret = TTkString ()
@@ -327,7 +326,7 @@ def toAnsi(self, strip=False):
327326 return out
328327 return out + str (TTkColor .RST )
329328
330- def align (self , width = None , color = TTkColor .RST , alignment = TTkK .NONE ) -> Self :
329+ def align (self , width = None , color = TTkColor .RST , alignment = TTkK .NONE ) -> TTkString :
331330 ''' Align the string
332331
333332 :param width: the new width
@@ -389,7 +388,7 @@ def align(self, width=None, color=TTkColor.RST, alignment=TTkK.NONE) -> Self:
389388
390389 return ret
391390
392- def extractShortcuts (self ) -> Self :
391+ def extractShortcuts (self ) -> Tuple [ TTkString , List [ str ]] :
393392 def _chGenerator ():
394393 for ch ,color in zip (self ._text ,self ._colors ):
395394 yield ch ,color
@@ -406,7 +405,7 @@ def _chGenerator():
406405 _newColors .append (color )
407406 return TTkString ._importString1 (_newText ,_newColors ), _ret
408407
409- def replace (self , * args , ** kwargs ) -> Self :
408+ def replace (self , * args , ** kwargs ) -> TTkString :
410409 ''' **replace** (*old*, *new*, *count*)
411410
412411 Replace "**old**" match with "**new**" string for "**count**" times
@@ -454,7 +453,7 @@ def replace(self, *args, **kwargs) -> Self:
454453
455454 return ret
456455
457- def completeColor (self , color :TTkColor , match = None , posFrom = None , posTo = None ) -> Self :
456+ def completeColor (self , color :TTkColor , match = None , posFrom = None , posTo = None ) -> TTkString :
458457 ''' Complete the color of the entire string or a slice of it
459458
460459 The Fg and/or Bg of the string is replaced with the selected Fg/Bg color only if missing
@@ -495,7 +494,7 @@ def completeColor(self, color:TTkColor, match=None, posFrom=None, posTo=None) ->
495494 return ret
496495
497496
498- def setColor (self , color , match = None , posFrom = None , posTo = None ) -> Self :
497+ def setColor (self , color , match = None , posFrom = None , posTo = None ) -> TTkString :
499498 ''' Set the color of the entire string or a slice of it
500499
501500 If only the color is specified, the entire string is colorized
@@ -531,7 +530,7 @@ def setColor(self, color, match=None, posFrom=None, posTo=None) -> Self:
531530 ret ._colors += self ._colors
532531 return ret
533532
534- def substring (self , fr = None , to = None ) -> Self :
533+ def substring (self , fr = None , to = None ) -> TTkString :
535534 ''' Return the substring
536535
537536 :param fr: the starting of the slice, defaults to 0
@@ -546,7 +545,7 @@ def substring(self, fr=None, to=None) -> Self:
546545 ret ._fastCheckWidth (self ._hasSpecialWidth )
547546 return ret
548547
549- def split (self , separator ) -> list [Self ]:
548+ def split (self , separator ) -> list [TTkString ]:
550549 ''' Split the string using a separator
551550
552551 .. note:: Only a one char separator is currently supported
@@ -599,7 +598,7 @@ def findall(self, regexp, ignoreCase=False):
599598 def getIndexes (self , char ):
600599 return [i for i ,c in enumerate (self ._text ) if c == char ]
601600
602- def join (self , strings :list [Self ]) -> Self :
601+ def join (self , strings :list [TTkString ]) -> TTkString :
603602 ''' Join the input strings using the current as separator
604603
605604 :param strings: the list of strings to be joined
0 commit comments