sequences.py
Module providing ‘sequence awareness’.
- class Sequence(sequence_text: str, term: Terminal)[source]
A “sequence-aware” version of the base
strclass.This unicode-derived class understands the effect of escape sequences of printable length, allowing a properly implemented
rjust(),ljust(),center(), andlength().Note
that other than
Sequence.padd(), this is just a thin layer overwcwidth, kept by name for API compatibility.Class constructor.
- Parameters:
- ljust(width: SupportsIndex, fillchar: str = ' ') str[source]
Return string containing sequences, left-adjusted.
- rjust(width: SupportsIndex, fillchar: str = ' ') str[source]
Return string containing sequences, right-adjusted.
- center(width: SupportsIndex, fillchar: str = ' ') str[source]
Return string containing sequences, centered.
- truncate(width: SupportsIndex) str[source]
Truncate a string in a sequence-aware manner.
Any printable characters beyond
widthare removed, while all sequences remain in place. Horizontal sequences are first expanded bypadd().Wide characters (such as CJK or emoji) that would partially exceed
widthare replaced with space padding to maintain exact width.SGR (terminal styling) sequences are propagated: the result begins with any active style at the start position and ends with a reset sequence if styles were active.
- length() int[source]
Return the printable length of string containing sequences.
Returns the maximum horizontal cursor extent reached while processing the string. Backspace and cursor-left movements do not reduce the length below the maximum position reached.
Some characters may consume more than one cell, mainly CJK (Chinese, Japanese, Korean) and Emojis and some kinds of symbols.
For example:
>>> from blessed import Terminal >>> from blessed.sequences import Sequence >>> term = Terminal() >>> msg = term.clear + term.red('コンニチハ') >>> Sequence(msg, term).length() 10
Note
Although accounted for, strings containing sequences such as
term.clearwill not give accurate returns, it is not considered lengthy (a length of 0).
- strip(chars: str | None = None) str[source]
Return string of sequences, leading and trailing whitespace removed.
- lstrip(chars: str | None = None) str[source]
Return string of all sequences and leading whitespace removed.
- rstrip(chars: str | None = None) str[source]
Return string of all sequences and trailing whitespace removed.
- iter_parse(term: Terminal, text: str) Iterator[Tuple[str, Termcap | None]][source]
Generator yields (text, capability) for characters of
text.value for
capabilitymay beNone, wheretextisstrof length 1. Otherwise,textis a full matching sequence of given capability.Note
Previously used by SequenceTextWrapper, sequence-aware text wrapping now exists in wcwidth as
wcwidth.wrap(). This function is kept for API compatibility and used bymeasure_length().