TTkString๐ŸŒถ๏ธ

class TTkString(text: str = '', color: TTkColor = None)[source]๐ŸŒถ๏ธ

Bases: object

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 TTkColor.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'))

Methods๐ŸŒถ๏ธ

align(width=None, color=<TermTk.TTkCore.color._TTkColor object>, alignment=0) Self[source]๐ŸŒถ๏ธ

Align the string

Parameters:
  • width (int, optional) โ€“ the new width

  • color (TTkColor, optional) โ€“ the color of the padding, defaults to TTkColor.RST

  • alignment (TTkConstant.Alignment, optional) โ€“ the alignment of the text to the full width NONE

charAt(pos: int) str[source]๐ŸŒถ๏ธ
colorAt(pos: int) TTkColor[source]๐ŸŒถ๏ธ
completeColor(color, match=None, posFrom=None, posTo=None) Self[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 TTkColor.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

extractShortcuts() Self[source]๐ŸŒถ๏ธ
find(*args, **kwargs) None[source]๐ŸŒถ๏ธ
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

getData()[source]๐ŸŒถ๏ธ
getIndexes(char)[source]๐ŸŒถ๏ธ
isPlainText() bool[source]๐ŸŒถ๏ธ

Return True if the string does not include colors or modifications

isdigit() bool[source]๐ŸŒถ๏ธ
join(strings: list[Self]) Self[source]๐ŸŒถ๏ธ

Join the input strings using the current as separator

Parameters:

strings (list) โ€“ the list of strings to be joined

lstrip(ch: str) Self[source]๐ŸŒถ๏ธ
nextPos(pos)[source]๐ŸŒถ๏ธ
prevPos(pos)[source]๐ŸŒถ๏ธ
replace(*args, **kwargs) Self[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

sameAs(other: Self) bool[source]๐ŸŒถ๏ธ
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

setCharAt(pos: int, char: str) Self[source]๐ŸŒถ๏ธ
setColor(color, match=None, posFrom=None, posTo=None) Self[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 TTkColor.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

setColorAt(pos, color) Self[source]๐ŸŒถ๏ธ
split(separator) list[Self][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) Self[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) Self[source]๐ŸŒถ๏ธ

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

tabCharPos(pos, tabSpaces=4, alignTabRight=False) int[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)
termWidth() int[source]๐ŸŒถ๏ธ
toAnsi(strip=False)[source]๐ŸŒถ๏ธ

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

toAscii() str[source]๐ŸŒถ๏ธ

Return the ascii representation of the string

TTkString Attributes๐ŸŒถ๏ธ

unicodeWideOverflowColor