Expand description
Text measurement, padding and word wrapping.
Everything here works on visible columns: ANSI escape sequences count as zero width and
characters are measured with unicode_width, so Czech diacritics, box-drawing characters
and emoji line up. ⚠️ (an emoji presentation sequence of two code points) is the one case
where terminals disagree with the standard, so text that goes into a box is normalised to
the bare ⚠ first, see normalize_emoji.
Two layers live in this module:
- the public helpers (
visible_width,pad_to,center,wrap_plain, …) that the screens use directly, and - a small styled-text layout engine (crate-private) shared by
wrap_plainand the markdown renderer. Words are laid out before any escape sequence is written and every styled run of a line is closed on that same line, so a line never carries a style over to the next one, wherever it is wrapped.
Constants§
- TAB_
WIDTH - Tab stop distance used when tabs are expanded.
Functions§
- center
- Centers
lineinwidthcolumns. - expand_
tabs - Expands tab characters to spaces, with tab stops every
tab_widthcolumns. - fold_
ansi - Cuts a possibly styled line into rows of at most
widthvisible columns. - is_
self_ contained - True when
lineleaves no colour or attribute switched on at its end. - normalize_
emoji - Removes the emoji variation selector U+FE0F after a one-column character.
- pad_
start - Pads
lineon the left so it ends at columnwidth(right alignment). - pad_to
- Pads
lineon the right with spaces up towidthvisible columns. - sanitize
- Prepares text for display inside a box: control characters (except
\nand\t) become U+FFFD and emoji are normalised withnormalize_emoji. - strip_
ansi - Removes ANSI escape sequences from
s. - truncate
- Shortens
lineto at mostwidthvisible columns, ending it with…when something was cut. - visible_
width - Number of terminal columns
soccupies, ignoring ANSI escape sequences. - with_
base_ style - Wraps
textinbase, keepingbaseactive after any style reset inside the text. - wrap_
plain - Wraps plain text to
widthcolumns without interpreting it in any way.