euporie.core.ft.utils
Utilitie for manipulating formatted text.
Functions
|
Add a border around formatted text. |
|
Align formatted text at a given width. |
Write fragments tagged with "[ReverseOverwrite]" over text to their left. |
|
|
Apply a style to formatted text. |
|
Cast a value to a type. |
|
Concatenate two blocks of formatted text, aligning at a given baseline. |
|
Turn a list of (style_str, text) tuples into another list where each string is exactly one character. |
|
Concatenate all the text parts again. |
|
Split formatted text into a list of word fragments which form words. |
|
Return the character width of this text fragment list. |
|
Return width of a string. |
|
Return an instance of a Lexer subclass that has alias in its aliases list. |
|
Indent formatted text with a given margin. |
|
Join a list of lines of formatted text. |
|
Retrieve the last character of formatted text. |
|
Calculate the length of the last line in formatted text. |
|
Format formatted text using a named |
|
Calculate the length of the longest line in formatted text. |
|
Fill space at the end of lines. |
|
Pate formatted text on top of other formatted text. |
|
Take a single list of (style_str, text) tuples and yield one such list for each line. |
|
Strip whitespace (or a given character) from the ends of formatted text. |
Remove up to one trailing new-line character from formatted text. |
|
|
Extract a substring from formatted text. |
|
Turn any kind of formatted text back into plain text. |
|
Truncate all lines at a given length. |
|
Align formatted text vertically. |
|
Wrap formatted text at a given width. |
Classes
|
A tuple of four bools with directions. |
|
A tuple of four integers with directions. |
|
A tuple of four strings with directions. |
|
Create a collection of name/value pairs. |
|
Alignment of formatted text. |
|
Vertical alignment of formatted text. |
|
A collection of characters which can be used to draw a grid. |
Exceptions
Raised if one of the lookup functions didn't find a matching class. |
- class euporie.core.ft.utils.FormattedTextAlign(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
Bases:
enum.Enum
Alignment of formatted text.
- CENTER = 'center'
- LEFT = 'left'
- RIGHT = 'right'
- class euporie.core.ft.utils.FormattedTextVerticalAlign(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
Bases:
enum.Enum
Vertical alignment of formatted text.
- BOTTOM = 'bottom'
- MIDDLE = 'middle'
- TOP = 'top'
- euporie.core.ft.utils.add_border(ft: StyleAndTextTuples, width: int | None = None, style: str = '', border_grid: GridStyle = ┌─┬┐ │ ││ ├─┼┤ └─┴┘, border_visibility: DiBool | bool = True, border_style: DiStr | str = '', padding: DiInt | int = 0, padding_style: DiStr | str = '') StyleAndTextTuples
Add a border around formatted text.
- Parameters
ft – The formatted text to enclose with a border
width – The target width including the border and padding
style – The style to apply to the content background
border_grid – The grid style to use for the border
border_visibility – Determines which edges should receive a border
border_style – The style to apply to the border
padding – The width of spacing to apply between the content and the border
padding_style – The style to apply to the border
- Returns
The indented formatted text
- euporie.core.ft.utils.align(ft: StyleAndTextTuples, how: FormattedTextAlign = FormattedTextAlign.LEFT, width: int | None = None, style: str = '', placeholder: str = '…', ignore_whitespace: bool = False) StyleAndTextTuples
Align formatted text at a given width.
- Parameters
how – The alignment direction
ft – The formatted text to strip
width – The width to which the output should be padded. If
None
, the length of the longest line is usedstyle – The style to apply to the padding
placeholder – The string that will appear at the end of a truncated line
ignore_whitespace – If True, whitespace will be ignored
- Returns
The aligned formatted text
- euporie.core.ft.utils.apply_reverse_overwrites(ft: StyleAndTextTuples) StyleAndTextTuples
Write fragments tagged with “[ReverseOverwrite]” over text to their left.
- euporie.core.ft.utils.apply_style(ft: StyleAndTextTuples, style: str) StyleAndTextTuples
Apply a style to formatted text.
- euporie.core.ft.utils.concat(ft_a: StyleAndTextTuples, ft_b: StyleAndTextTuples, baseline_a: int = 0, baseline_b: int = 0, style: str = '') tuple[StyleAndTextTuples, int]
Concatenate two blocks of formatted text, aligning at a given baseline.
- Parameters
ft_a – The first block of formatted text to combine
ft_b – The second block of formatted text to combine
baseline_a – The row to use to align the first block of formatted text with the second, counted in lines down from the top of the block
baseline_b – The row to use to align the second block of formatted text with the second, counted in lines down from the top of the block
style – The style to use for any extra lines added
- Returns
A tuple containing the combined formatted text and the new baseline position
- euporie.core.ft.utils.fragment_list_to_words(fragments: StyleAndTextTuples, sep: str = ' ') Iterable[StyleAndTextTuples]
Split formatted text into a list of word fragments which form words.
- euporie.core.ft.utils.fragment_list_width(fragments: StyleAndTextTuples) int
Return the character width of this text fragment list.
Takes double width characters into account, and ignore special fragments: * ZeroWidthEscape * ReverseOverwrite
- Parameters
fragments – List of
(style_str, text)
or(style_str, text, mouse_handler)
tuples.- Returns
The width of the fragment list
- euporie.core.ft.utils.indent(ft: StyleAndTextTuples, margin: str = ' ', style: str = '', skip_first: bool = False) StyleAndTextTuples
Indent formatted text with a given margin.
- Parameters
ft – The formatted text to strip
margin – The margin string to add
style – The style to apply to the margin
skip_first – If
True
, the first line is skipped
- Returns
The indented formatted text
- euporie.core.ft.utils.join_lines(fragments: list[StyleAndTextTuples]) StyleAndTextTuples
Join a list of lines of formatted text.
- euporie.core.ft.utils.last_char(ft: StyleAndTextTuples) str | None
Retrieve the last character of formatted text.
- euporie.core.ft.utils.last_line_length(ft: StyleAndTextTuples, rows: int = 1) int
Calculate the length of the last line in formatted text.
- euporie.core.ft.utils.lex(ft: StyleAndTextTuples, lexer_name: str) StyleAndTextTuples
Format formatted text using a named
pygments
lexer.
- euporie.core.ft.utils.max_line_width(ft: StyleAndTextTuples) int
Calculate the length of the longest line in formatted text.
- euporie.core.ft.utils.pad(ft: StyleAndTextTuples, width: int | None = None, char: str = ' ', style: str = '') StyleAndTextTuples
Fill space at the end of lines.
- euporie.core.ft.utils.paste(ft_top: StyleAndTextTuples, ft_bottom: StyleAndTextTuples, row: int = 0, col: int = 0, transparent: bool = False) StyleAndTextTuples
Pate formatted text on top of other formatted text.
- euporie.core.ft.utils.strip(ft: StyleAndTextTuples, left: bool = True, right: bool = True, chars: str | None = None, only_unstyled: bool = False) StyleAndTextTuples
Strip whitespace (or a given character) from the ends of formatted text.
- Parameters
ft – The formatted text to strip
left – If
True
, strip from the left side of the inputright – If
True
, strip from the right side of the inputchars – The character to strip. If
None
, strips whitespaceonly_unstyled – If
True
, only strip unstyled fragments
- Returns
The stripped formatted text
- euporie.core.ft.utils.strip_one_trailing_newline(ft: StyleAndTextTuples) StyleAndTextTuples
Remove up to one trailing new-line character from formatted text.
- euporie.core.ft.utils.substring(ft: StyleAndTextTuples, start: int | None = None, end: int | None = None) StyleAndTextTuples
Extract a substring from formatted text.
- euporie.core.ft.utils.truncate(ft: StyleAndTextTuples, width: int, style: str = '', placeholder: str = '…', ignore_whitespace: bool = False) StyleAndTextTuples
Truncate all lines at a given length.
- Parameters
ft – The formatted text to truncate
width – The width at which to truncate the text
style – The style to apply to the truncation placeholder. The style of the truncated text will be used if not provided
placeholder – The string that will appear at the end of a truncated line
ignore_whitespace – Do not use placeholder when truncating whitespace
- Returns
The truncated formatted text
- euporie.core.ft.utils.valign(ft: StyleAndTextTuples, how: FormattedTextVerticalAlign = FormattedTextVerticalAlign.MIDDLE, height: int | None = None, style: str = '') StyleAndTextTuples
Align formatted text vertically.
- euporie.core.ft.utils.wrap(ft: StyleAndTextTuples, width: int, style: str = '', placeholder: str = '…', left: int = 0, truncate_long_words: bool = True, strip_trailing_ws: bool = False) StyleAndTextTuples
Wrap formatted text at a given width.
If words are longer than the given line they will be truncated
- Parameters
ft – The formatted text to wrap
width – The width at which to wrap the text
style – The style to apply to the truncation placeholder
placeholder – The string that will appear at the end of a truncated line
left – The starting position within the first line
truncate_long_words – If
True
words longer than a line will be truncatedstrip_trailing_ws – If
True
, trailing whitespace will be removed from the ends of lines
- Returns
The wrapped formatted text