Documentation
¶
Overview ¶
Package termsafe makes text from a document safe to show on a terminal by turning the characters a terminal acts on into text.
A block's code is shown so its reader can decide whether to run it. Escape sequences and other control characters in it would be acted on instead of shown: "\r\x1b[2K" at the end of a line erases what came before it, so code could run that its reader never saw. Visible shows such characters instead, so what is on the screen is what runs.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HasControl ¶
HasControl reports whether s has a character Visible would replace.
func SGROnly ¶ added in v1.5.0
SGROnly is s with every control character and escape sequence taken out but SGR sequences (colours), newlines and tabs: for text a terminal should draw rather than obey, where showing the sequences would only be noise, such as a shell prompt, which commonly carries shell-integration marks (OSC 133) and title sets. A CSI sequence goes up to its final byte and an OSC, DCS, APC, PM or SOS string up to BEL or ST; an unterminated one runs to the end of s.
func Styled ¶ added in v1.5.0
Styled is s for printing to a terminal as document text or output: SGR sequences (ESC [ params m, which only set colours and the like) are kept, so coloured output stays coloured, and every other control character is shown as Visible shows it, so a saved output or a line of prose can't write the clipboard (OSC 52), set the window title, make a link whose target isn't its text (OSC 8), or move the cursor to hide text. A newline, a tab and a carriage return just before a newline (CRLF line ends) pass as they are. Unlike Visible it leaves zero-width and bidi characters alone: they don't drive the terminal, and prose and output use them (emoji joiners, right-to-left text).
func Visible ¶
Visible returns s with every control character other than newline and tab replaced by a visible stand-in: C0 controls in caret notation (ESC as ^[, CR as ^M), DEL as ^?, and as <U+XXXX> the C1 controls, the Unicode direction overrides that can reorder how a line reads, and the zero-width characters that can hide inside a word. Text without such characters is returned as it is. A byte that isn't valid UTF-8 shows as <0xXX>, since a lone 0x9b is a CSI to some terminals.
Types ¶
This section is empty.