TermTk.TTkCore.string

TTkString

class TTkString(text: str = '', color: TTkColor = None)[source]

TermTk String Helper

The TTkString constructor creates a terminal String object.

Parameters
  • text (str, optional) – text of the string, defaults to “”

  • color (TTkColor, optional) – the color of the string, defaults to RST

Example:

# No params Constructor
str1 = TTkString() + "test 1"
str2 = TTkString() + TTkColor.BOLD + "test 2"

# Indexed params constructor
str3 = TTkString("test 3")
str4 = TTkString("test 4", TTkColor.ITALIC)

# Named params constructor
str5 = TTkString(text="test 5")
str6 = TTkString(text="test 6", color=TTkColor.ITALIC+TTkColor.bg("000044"))

# Combination of constructors (Highly Unrecommended)
str7 = TTkString("test 7", color=TTkColor.fg('#FF0000'))
align(width=None, color=<TermTk.TTkCore.color._TTkColor object>, alignment=0)[source]

Align the string

Parameters
  • width (int, optional) – the new width

  • color (TTkColor, optional) – the color of the padding, defaults to RST

  • alignment (Alignment, optional) – the alignment of the text to the full width NONE

completeColor(color, match=None, posFrom=None, posTo=None)[source]

Complete the color of the entire string or a slice of it

The Fg and/or Bg of the string is replaced with the selected Fg/Bg color only if missing

If only the color is specified, the entire string is colorized

Parameters
  • color (TTkColor) – the color to be used, defaults to RST

  • match (str, optional) – the match to colorize

  • posFrom (int, optional) – the initial position of the color

  • posTo (int, optional) – the final position of the color

findall(regexp, ignoreCase=False)[source]

FindAll the regexp matches in the string

Parameters
  • regexp (str) – the regular expression to be matched

  • ignoreCase (bool) – Ignore case, defaults to False

isPlainText() bool[source]

Return True if the string does not include colors or modifications

join(strings)[source]

Join the input strings using the current as separator

Parameters

strings (list) – the list of strings to be joined

replace(*args, **kwargs)[source]

replace (old, new, count)

Replace “old” match with “new” string for “count” times

Parameters
  • old (str) – the match to be placed

  • new (str, optional) – the match to replace

  • count (int, optional) – the number of occurrences

search(regexp, ignoreCase=False)[source]

Return the re.match of the regexp

Parameters
  • regexp (str) – the regular expression to be matched

  • ignoreCase (bool) – Ignore case, defaults to False

setColor(color, match=None, posFrom=None, posTo=None)[source]

Set the color of the entire string or a slice of it

If only the color is specified, the entire string is colorized

Parameters
  • color (TTkColor) – the color to be used, defaults to RST

  • match (str, optional) – the match to colorize

  • posFrom (int, optional) – the initial position of the color

  • posTo (int, optional) – the final position of the color

split(separator)[source]

Split the string using a separator

Note

Only a one char separator is currently supported

Parameters

separator (str) – the “char” separator to be used

substring(fr=None, to=None)[source]

Return the substring

Parameters
  • fr (int, optional) – the starting of the slice, defaults to 0

  • to (int, optional) – the ending of the slice, defaults to the end of the string

tab2spaces(tabSpaces=4)[source]

Return the string representation with the tabs (converted in spaces) trimmed and aligned

tabCharPos(pos, tabSpaces=4, alignTabRight=False)[source]

Return the char position in the string from the position in its representation with the tab and variable char sizes are solved

i.e.

pos                   X = 11
tab2Spaces |----------|---------------------|
Tabs             |-|  |  |-|     |-|   |
_text      L😁rem   ipsum   dolor   sit amet,
chars      .. ...t  .....t  .....t  ...t.....
ret                   x = 7 (tab is a char)
toAnsi(strip=False)[source]

Return the ansii (terminal colors/events) representation of the string

toAscii() str[source]

Return the ascii representation of the string