termsafe

package
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func HasControl(s string) bool

HasControl reports whether s has a character Visible would replace.

func SGROnly added in v1.5.0

func SGROnly(s string) string

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

func Styled(s string) string

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

func Visible(s string) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL