❯ go-clikit
v0.10.0 pkg.go.dev ↗

packages › progress & output › termtext

termtext

import "github.com/GSI-HPC/go-clikit/termtext"

Escape untrusted text for a terminal, and measure, cut and wrap it in columns.

since v0.1.0 imports: no package of the kitrequires: golang.org/x/text/width decisions 7 · 8 · 14 · 42
The place of termtext in go-clikit: the packages it imports, and the packages that import it.termtextprogressdisplaycliprogressprogresstestmcpprogresseventlogfanoutfanouttestclioutdoctorfsutilcobratreecobratreetestlocalwebsessionsselocalwebtestlaunchassetspagehtmx

termtext is the one escaper of the kit. Text from elsewhere, such as the output of a remote host, can move the cursor, retitle the terminal or show a name in another order. termtext shows such characters as visible escapes. It also measures text in the columns that a terminal gives it.

remote := "backup done\x1b]0;owned\x07\rbackup FAILED\n\tsee the log\n"
fmt.Print(termtext.EscapeLines(remote))
backup done\x1b]0;owned\x07\rbackup FAILED
	see the log
ExampleEscapeLines in termtext/example_test.go. CI runs it and checks its output.

The main names#

NameWhat it does
termtext.Escapeone value on one line: newline and tab are escaped too
termtext.EscapeLinestext of many lines: newline, tab and a CRLF line end stay
termtext.NewEscapeWritera stream, escaped line by line as it comes
termtext.Widththe columns of text: two for a wide rune, none for a mark
termtext.Truncatetext cut to a number of columns, never inside a wide rune
termtext.Wrapa paragraph filled to a number of columns, with indents
text := "Run the command on every host in the set, one after the other, and stop at the first failure."
fmt.Print(termtext.Wrap(text, 36, "  -x, --exit  ", "              "))
  -x, --exit  Run the command on
              every host in the set,
              one after the other,
              and stop at the first
              failure.
ExampleWrap in termtext/example_test.go. CI runs it and checks its output.

Fixed interfaces#

  • The forms of the escapes: \r, \n, \t, \xNN and \uNNNN.
  • The CRLF rule (decision 42).
  • The set of escaped runes can grow in a minor release (decision 14).

Read more#

Untrusted text explains the policy.