Documentation
¶
Overview ¶
Package termtext makes untrusted text safe to write to a terminal, and measures how many columns text takes there.
Text from elsewhere, such as the output of a program on a remote host or in a container, an error it returned, or a value a service reported, is not trusted: a carriage return or a cursor movement can overwrite a line printed before, an escape sequence can retitle the terminal or write the clipboard, and a bidirectional control can show text in another order than it has. EscapeText and EscapeCell show such characters as visible escapes instead. A program that escapes everything with this package, and nothing with anything else, has one place to fix when a character turns out to need escaping too.
Escape policy ¶
The policy is a deny-list: a rune is escaped when it can move the cursor, change the state of the terminal, or change the order or the lines text is shown in, and is written as it is otherwise. Escaped are:
- every C0 control (U+0000 to U+001F) and DEL (U+007F), except that EscapeText keeps newline and tab, which cannot move the cursor back over text already written;
- every C1 control (U+0080 to U+009F), which some terminals read as the introducer of a control sequence;
- the bidirectional controls U+061C, U+200E, U+200F, U+202A to U+202E and U+2066 to U+2069;
- the line and paragraph separators U+2028 and U+2029;
- every byte that is not part of valid UTF-8, such as a lone 0x9b.
Other invisible characters, such as a zero-width space, a byte order mark or a tag character, are written as they are: they change neither the cursor nor the terminal, and an allow-list that escaped them would escape text in scripts that need joiners and marks too. A caller that compares names should not rely on this package to make look-alikes visible.
Widths ¶
RuneWidth and Width approximate the columns a terminal gives text, one rune at a time: two for East Asian wide and fullwidth characters, none for combining marks and format characters, one for the rest. They do not cluster graphemes, so a sequence joined with U+200D, a skin tone modifier, a variation selector that turns a character into an emoji, or a flag is counted as the sum of its runes, which may be more than a terminal shows. Truncate shortens text to a number of columns with the same widths.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EscapeCell ¶
EscapeCell is EscapeText for a value that has to stay on one line, such as a table cell or a name in a header: newline and tab are escaped too, as \n and \t, so that a value cannot start a row that seems to be another.
func EscapeText ¶
EscapeText makes untrusted text safe to write to a terminal, keeping its lines: untrusted text from a remote host or a container, say, whose control characters would otherwise act on the terminal. It replaces every rune the package's escape policy names with a visible escape such as \x1b or \u009b, and a byte that is not UTF-8 with one such as \xff. Newline and tab are kept, since they cannot move the cursor back over text already written.
Text that holds none of these is returned unchanged.
func RuneWidth ¶
RuneWidth returns how many columns a terminal gives r: two for a character whose East Asian Width is wide or fullwidth, as CJK, kana, Hangul and the emoji drawn as pictures are; none for a mark that combines with the character before it or a character that only formats, such as a zero-width joiner; and one for everything else.
func Truncate ¶
Truncate shortens s to at most cols columns, as Width counts them, so that a row never wraps. A wide character that would reach past cols is left out whole, never split; cols of zero or less leaves nothing.
s must be plain text that is already escaped, by EscapeCell for one line: a control character or an escape sequence would be counted as the columns of its runes, not as what it does. Truncate does not cluster graphemes, so it may cut a sequence of runes a terminal draws as one character, such as an emoji joined with U+200D, between its runes.
Types ¶
This section is empty.