pretty

package module
v0.0.0-...-e89ba86 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2023 License: CC0-1.0 Imports: 6 Imported by: 5

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprint

func Fprint(w io.Writer, f Formatter, args ...interface{})

Fprint formats the given string with the formatter and writes it to the given writer.

func Fprintf

func Fprintf(w io.Writer, f Formatter, format string, args ...interface{})

Fprintf formats the given string with the formatter and writes it to the given writer.

func Printf

func Printf(f Formatter, format string, args ...interface{})

Printf formats the given string with the formatter and prints it to stdout.

func Sprint

func Sprint(f Formatter, args ...interface{}) string

Sprint formats the given string with the formatter.

func Sprintf

func Sprintf(f Formatter, format string, args ...interface{}) string

Sprintf formats the given string with the formatter.

Types

type Formatter

type Formatter interface {
	Format(*Text)
}

Formatter manipulates Text.

var Nop Formatter = formatterFunc(func(t *Text) {})

Nop is a no-op formatter.

func BgColor

func BgColor(c termenv.Color) Formatter

BgColor returns a formatter that sets the background color. Example:

BgColor(termenv.RGBColor("#ff0000"))
BgColor(termenv.ANSI256Color(196))
BgColor(termenv.ANSIColor(31))

func Bold

func Bold() Formatter

Bold returns a formatter that makes the text bold.

func CSI

func CSI(seq string) Formatter

CSI wraps the text in the given CSI (Control Sequence Introducer) sequence. Example:

CSI(termenv.BoldSeq)
CSI(termenv.UnderlineSeq)
CSI(termenv.ItalicSeq)

func FgColor

func FgColor(c termenv.Color) Formatter

FgColor returns a formatter that sets the foreground color. Example:

FgColor(termenv.RGBColor("#ff0000"))
FgColor(termenv.ANSI256Color(196))
FgColor(termenv.ANSIColor(31))

func Italic

func Italic() Formatter

Italic returns a formatter that makes the text italic.

func LineWrap

func LineWrap(width int) Formatter

LineWrap wraps the text at the given width. It breaks lines at word boundaries when possible. It will never break up a word so that URLs and other long strings present correctly.

func Underline

func Underline() Formatter

Underline returns a formatter that underlines the text.

func Wrap

func Wrap(prefix, suffix string) Formatter

Wrap wraps the text in the given prefix and suffix. It is useful for wrapping text in ANSI sequences.

func XPad

func XPad(left, right int) Formatter

XPad pads the text on the left and right.

type Style

type Style []Formatter

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

func (Style) Format

func (s Style) Format(t *Text)

Format applies all formatters in the style to the text and returns the modified text.

When performance is a concern, use WriteTo instead of String on the returned text.

func (Style) With

func (s Style) With(fs ...Formatter) Style

With returns a new style with the given formatters appended.

type Text

type Text struct {
	S    string
	Next *Text
	Prev *Text
}

Text is a linked-list structure that represents an in-progress text string. Most formatters work by prepending and appending to text, so this structure is far more efficient than manipulating strings directly.

The pointer is instrinsicly a cursor that points to the current text segment. So, subsequent appends and prepends are O(1) since the cursor is already at the tail or head respectively.

func String

func String(s ...string) *Text

String creates a new Text object from the given strings.

func (*Text) Append

func (t *Text) Append(ss ...string) *Text

Append appends strings to the end of the text in order. Example:

txt := String("a")
txt = txt.Append("b", "c")
fmt.Println(txt.String())
// Output: abc

func (*Text) Bytes

func (t *Text) Bytes(b []byte) []byte

Bytes allocates a new byte slice containing the entire text. It uses the given buffer if it is large enough.

func (*Text) Head

func (t *Text) Head() *Text

Head returns the absolute head of the text. It adjusts the pointer to the head of the text.

func (*Text) Insert

func (t *Text) Insert(s string) *Text

Insert inserts the given text before the current text. It returns the new node.

func (*Text) Len

func (t *Text) Len() int

Len returns the length of the text.

func (*Text) Prepend

func (t *Text) Prepend(ss ...string) *Text

Prepend prepends strings to the beginning of the text in order. Example:

txt := String("c")
txt = txt.Prepend("a", "b")
fmt.Println(txt.String())
// Output: abc

func (*Text) Split

func (t *Text) Split(n int) *Text

Split splits the current text into two parts at the given index. The current node contains the first part, and the new node contains the second part. It returns the new node, and does not adjust the pointer.

func (*Text) String

func (t *Text) String() string

String allocates a new string containing the entire text.

func (*Text) Tail

func (t *Text) Tail() *Text

Tail returns the absolute tail of the text. It adjusts the pointer to the tail of the text

func (*Text) WriteTo

func (t *Text) WriteTo(w io.Writer) (int64, error)

WriteTo writes the text to the given writer, avoiding string allocations.

Jump to

Keyboard shortcuts

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