TTkStringπΆοΈ
- class TTkString(text: str | TTkString = '', color: TTkColor | None = None)[source]πΆοΈ
Bases:
objectTermTk 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 toTTkColor.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: int = 0, color: ~TermTk.TTkCore.color.TTkColor = <TermTk.TTkCore.color.TTkColor object>, alignment: ~TermTk.TTkCore.constant.TTkConstant.Alignment = Alignment.NONE) TTkString[source]πΆοΈ
Align the string
- Parameters:
width (int, optional) β the new width
color (
TTkColor, optional) β the color of the padding, defaults toTTkColor.RSTalignment (
TTkK.Alignment, optional) β text alignment within the requested width
- Returns:
aligned string with preserved styling
- Return type:
- charAt(pos: int) str[source]πΆοΈ
Return the character at the given position.
- Parameters:
pos (int) β character index
- Returns:
character at
pos- Return type:
str
- colorAt(pos: int) TTkColor[source]πΆοΈ
Return the color assigned to the character at
pos.- Parameters:
pos (int) β character index
- Returns:
color at
posorTTkColor.RSTwhen out of range- Return type:
- completeColor(color: TTkColor, match: str | None = None, posFrom: int | None = None, posTo: int | None = None) TTkString[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
- extractShortcuts() Tuple[TTkString, List[str]][source]πΆοΈ
Extract
&shortcuts and underline the mnemonic characters.- Returns:
tuple of processed string and extracted shortcut characters
- Return type:
tuple[
TTkString, list[str]]
- find(*args, **kwargs) int[source]πΆοΈ
Return the first index of a substring using
str.findsemantics.- Returns:
start index of the first match, or
-1if not found- Return type:
int
- findall(regexp: str, ignoreCase: bool = False) List[Any][source]πΆοΈ
FindAll the regexp matches in the string
- Parameters:
regexp (str) β the regular expression to be matched
ignoreCase (bool) β Ignore case, defaults to False
- Returns:
list of all matches
- Return type:
list[str] | list[tuple]
- getData() Tuple[Tuple[str, ...] | List[str], List[TTkColor]][source]πΆοΈ
Return text and color data in terminal-rendered form.
- Returns:
tuple of characters and colors
- Return type:
tuple
- getIndexes(char: str) List[int][source]πΆοΈ
Return indexes where
charappears.- Parameters:
char (str) β target character
- Returns:
matching character positions
- Return type:
list[int]
- isPlainText() bool[source]πΆοΈ
Return True if the string has no color/style information.
- Returns:
True when all chars use
TTkColor.RST- Return type:
bool
- isdigit() bool[source]πΆοΈ
Check whether the string contains only digit characters.
- Returns:
True if all characters are digits and the string is non-empty
- Return type:
bool
- join(strings: GeneratorType[TTkStringType, None, None] | List[TTkStringType] | List[TTkString] | List[str]) TTkString[source]πΆοΈ
Join the input strings using the current as separator
- Parameters:
strings (list) β the list of strings to be joined
- Returns:
joined string
- Return type:
- lstrip(ch: str) TTkString[source]πΆοΈ
Return a copy with leading characters removed.
- Parameters:
ch (str) β characters to strip from the left side
- Returns:
left-stripped string preserving color alignment
- Return type:
- nextPos(pos: int) int[source]πΆοΈ
Return next editable character position.
- Parameters:
pos (int) β current position
- Returns:
next non-combining character index
- Return type:
int
- prevPos(pos: int) int[source]πΆοΈ
Return previous editable character position.
- Parameters:
pos (int) β current position
- Returns:
previous non-combining character index
- Return type:
int
- replace(*args, **kwargs) TTkString[source]πΆοΈ
replace (old, new, count)
Replace βoldβ match with βnewβ string for βcountβ times
- Parameters:
old (str) β substring to be replaced
new (str, optional) β replacement substring
count (int, optional) β maximum number of replacements
- Returns:
updated string preserving color spans
- Return type:
- sameAs(other: str | TTkString) bool[source]πΆοΈ
Check whether text and per-character colors are identical.
- Parameters:
other (
TTkString| str) β string to compare against- Returns:
True when text and colors match exactly
- Return type:
bool
- search(regexp: str, ignoreCase: bool = False) Match[str] | None[source]πΆοΈ
Return the re.match of the regexp
- Parameters:
regexp (str) β the regular expression to be matched
ignoreCase (bool) β Ignore case, defaults to False
- Returns:
first regular-expression match or None
- Return type:
re.Match | None
- setCharAt(pos: int, char: str) TTkString[source]πΆοΈ
Return a copy with one character replaced.
- Parameters:
pos (int) β character index to replace
char (str) β replacement character
- Returns:
updated string
- Return type:
- Raises:
IndexError β if
posis out of range
- setColor(color: TTkColor, match: str | None = None, posFrom: int | None = None, posTo: int | None = None) TTkString[source]πΆοΈ
Set the color of the entire string or a slice of it
If only the color is specified, the entire string is colorized
- setColorAt(pos: int, color: TTkColor) TTkString[source]πΆοΈ
Return a copy with one character color replaced.
- split(separator: str) list[TTkString][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
- Returns:
list of split chunks
- Return type:
list[
TTkString]
- substring(fr: int | None = None, to: int | None = None) TTkString[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
- Returns:
sliced string
- Return type:
- tab2spaces(tabSpaces: int = 4) TTkString[source]πΆοΈ
Expand tab characters into aligned spaces.
- Parameters:
tabSpaces (int) β tab stop size
- Returns:
string with tabs replaced by spaces
- Return type:
- tabCharPos(pos: int, tabSpaces: int = 4, alignTabRight: bool = False) int[source]πΆοΈ
Map a rendered column position to the internal character index.
Tabs and variable-width characters are resolved against the visual terminal representation.
- Parameters:
pos (int) β target visual column
tabSpaces (int) β tab stop size
alignTabRight (bool) β map positions inside a tab to the tab right edge
- Returns:
character index in
_text- Return type:
int
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]πΆοΈ
Return the rendered terminal width of this string.
- Returns:
rendered width accounting for tabs and wide/combining chars
- Return type:
int
TTkString AttributesπΆοΈ
|
|
|