Categorygithub.com/coder/pretty
modulepackage
0.0.0-20230908205945-e89ba86370e0
Repository: https://github.com/coder/pretty.git
Documentation: pkg.go.dev

# README

pretty

Go Reference

pretty is a performant Terminal pretty printer for Go. We built it after using lipgloss and experiencing significant performance issues.

pretty doesn't implement escape sequences and should be used alongside termenv.

Basic Usage

errorStyle := pretty.Style{
		pretty.FgColor(termenv.RGBColor("#ff0000")),
		pretty.BgColor(termenv.RGBColor("#000000")),
		pretty.WrapCSI(termenv.BoldSeq),
}

errorStyle.Printf("something bad")

Color

You can use termenv to adapt the colors to the terminal's color palette:

profile := termenv.NewOutput(os.Stdout, termenv.WithColorCache(true)).ColorProfile()
errorStyle := pretty.Style{
        pretty.FgColor(profile.Color("#ff0000")),
        pretty.BgColor(profile.Color("#000000")),
        pretty.WrapCSI(termenv.BoldSeq),
}

Performance

$ go test -bench=.
goos: darwin
goarch: arm64
pkg: github.com/coder/pretty/bench
BenchmarkPretty-10               5142177               232.6 ns/op        55.88 MB/s         272 B/op          8 allocs/op
BenchmarkLipgloss-10              280276              4157 ns/op           3.13 MB/s         896 B/op         72 allocs/op
PASS
ok      github.com/coder/pretty/bench   2.921s

pretty remains fast even through dozens of transformations due to its linked-list based intermediate representation of text. In general, operations scale with the number of links rather than the length of the text. For example, coloring a 1000 character string green is just as fast as wrapping a 1 character string.

Eventually we could reap even more gains by replacing the linked-list with a rope.

# Functions

BgColor returns a formatter that sets the background color.
Bold returns a formatter that makes the text bold.
CSI wraps the text in the given CSI (Control Sequence Introducer) sequence.
FgColor returns a formatter that sets the foreground color.
Fprint formats the given string with the formatter and writes it to the given writer.
Fprintf formats the given string with the formatter and writes it to the given writer.
Italic returns a formatter that makes the text italic.
LineWrap wraps the text at the given width.
Printf formats the given string with the formatter and prints it to stdout.
Sprint formats the given string with the formatter.
Sprintf formats the given string with the formatter.
String creates a new Text object from the given strings.
Underline returns a formatter that underlines the text.
Wrap wraps the text in the given prefix and suffix.
XPad pads the text on the left and right.

# Variables

Nop is a no-op formatter.

# Structs

Text is a linked-list structure that represents an in-progress text string.

# Interfaces

Formatter manipulates Text.

# Type aliases

Style is a special Formatter that applies multiple Formatters to a text in order.