Movatterモバイル変換


[0]ホーム

URL:


pretty

packagemodule
v0.0.0-...-e89ba86Latest Latest
Warning

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

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

Details

Repository

github.com/coder/pretty

Links

README

pretty

Go Reference

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

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

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 usetermenv 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: darwingoarch: arm64pkg: github.com/coder/pretty/benchBenchmarkPretty-10               5142177               232.6 ns/op        55.88 MB/s         272 B/op          8 allocs/opBenchmarkLipgloss-10              280276              4157 ns/op           3.13 MB/s         896 B/op         72 allocs/opPASSok      github.com/coder/pretty/bench   2.921s

pretty remains fast even through dozens of transformations due to its linked-listbased intermediate representation of text. In general, operations scale withthe number of links rather than the length of the text. For example, coloringa 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 arope.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

funcFprint

func Fprint(wio.Writer, fFormatter, args ...interface{})

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

funcFprintf

func Fprintf(wio.Writer, fFormatter, formatstring, args ...interface{})

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

funcPrintf

func Printf(fFormatter, formatstring, args ...interface{})

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

funcSprint

func Sprint(fFormatter, args ...interface{})string

Sprint formats the given string with the formatter.

funcSprintf

func Sprintf(fFormatter, formatstring, args ...interface{})string

Sprintf formats the given string with the formatter.

Types

typeFormatter

type Formatter interface {Format(*Text)}

Formatter manipulates Text.

var NopFormatter = formatterFunc(func(t *Text) {})

Nop is a no-op formatter.

funcBgColor

func BgColor(ctermenv.Color)Formatter

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

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

funcBold

func Bold()Formatter

Bold returns a formatter that makes the text bold.

funcCSI

func CSI(seqstring)Formatter

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

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

funcFgColor

func FgColor(ctermenv.Color)Formatter

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

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

funcItalic

func Italic()Formatter

Italic returns a formatter that makes the text italic.

funcLineWrap

func LineWrap(widthint)Formatter

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

funcUnderline

func Underline()Formatter

Underline returns a formatter that underlines the text.

funcWrap

func Wrap(prefix, suffixstring)Formatter

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

funcXPad

func XPad(left, rightint)Formatter

XPad pads the text on the left and right.

typeStyle

type Style []Formatter

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

func (Style)Format

func (sStyle) Format(t *Text)

Format applies all formatters in the style to the text andreturns the modified text.

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

func (Style)With

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

With returns a new style with the given formatters appended.

typeText

type Text struct {SstringNext *TextPrev *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 structureis 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 atthe tail or head respectively.

funcString

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 textin 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(sstring) *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 textin order.Example:

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

func (*Text)Split

func (t *Text) Split(nint) *Text

Split splits the current text into two parts at the given index. The currentnode 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(wio.Writer) (int64,error)

WriteTo writes the text to the given writer, avoidingstring allocations.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp