Skip to main content

Module text

Module text 

Source
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_plain and 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 line in width columns.
expand_tabs
Expands tab characters to spaces, with tab stops every tab_width columns.
fold_ansi
Cuts a possibly styled line into rows of at most width visible columns.
is_self_contained
True when line leaves 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 line on the left so it ends at column width (right alignment).
pad_to
Pads line on the right with spaces up to width visible columns.
sanitize
Prepares text for display inside a box: control characters (except \n and \t) become U+FFFD and emoji are normalised with normalize_emoji.
strip_ansi
Removes ANSI escape sequences from s.
truncate
Shortens line to at most width visible columns, ending it with … when something was cut.
visible_width
Number of terminal columns s occupies, ignoring ANSI escape sequences.
with_base_style
Wraps text in base, keeping base active after any style reset inside the text.
wrap_plain
Wraps plain text to width columns without interpreting it in any way.