Help us shape the future of Go! Take the 2026 Go Developer Survey

termsafe

package
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0, MIT Imports: 7 Imported by: 0

Documentation

Overview

Package termsafe makes text from outside the program, such as API responses, safe to print on a terminal.

Index

Constants

This section is empty.

Variables

View Source
var (
	Stdout = Writer{stdStream{&os.Stdout}}
	Stderr = Writer{stdStream{&os.Stderr}}
)

Stdout and Stderr are where plain text gets printed. Response data goes through the output formats instead, which render it from JSON. Like fmt.Println, they write to whatever os.Stdout and os.Stderr are at the time.

Functions

func Escape

func Escape(s string) string

Escape returns s with each non-graphic rune written as visible text, such as \x1b for ESC or \n for a newline, and each invalid byte as \xNN. Quotes, backslashes and joiners are unchanged, so ordinary text comes back as it was and escaping twice gives the same result as escaping once.

func JSON

func JSON(doc string) string

JSON rewrites encoded JSON so that no key or string in it decodes to text that Lines would change: such a rune becomes the escaped text instead, so `\u001b` becomes `\\x1b`. Valid JSON stays valid. JSON works on the text without parsing it, so a malformed document that a lenient decoder still reads comes out just as safe.

func Lines

func Lines(s string) string

Lines is Escape for multi-line text: it also keeps newlines and tabs.

Types

type Writer

type Writer struct {
	W io.Writer
}

Writer applies Lines to everything written to it. Styled output can't go through it, because the styling would be escaped too.

func (Writer) Write

func (w Writer) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

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