pretty
packagemoduleThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
README¶
pretty
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¶
- func Fprint(w io.Writer, f Formatter, args ...interface{})
- func Fprintf(w io.Writer, f Formatter, format string, args ...interface{})
- func Printf(f Formatter, format string, args ...interface{})
- func Sprint(f Formatter, args ...interface{}) string
- func Sprintf(f Formatter, format string, args ...interface{}) string
- type Formatter
- type Style
- type Text
- func (t *Text) Append(ss ...string) *Text
- func (t *Text) Bytes(b []byte) []byte
- func (t *Text) Head() *Text
- func (t *Text) Insert(s string) *Text
- func (t *Text) Len() int
- func (t *Text) Prepend(ss ...string) *Text
- func (t *Text) Split(n int) *Text
- func (t *Text) String() string
- func (t *Text) Tail() *Text
- func (t *Text) WriteTo(w io.Writer) (int64, error)
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
Types¶
typeFormatter¶
type Formatter interface {Format(*Text)}
Formatter manipulates Text.
funcBgColor¶
BgColor returns a formatter that sets the background color.Example:
BgColor(termenv.RGBColor("#ff0000"))BgColor(termenv.ANSI256Color(196))BgColor(termenv.ANSIColor(31))
funcCSI¶
CSI wraps the text in the given CSI (Control Sequence Introducer) sequence.Example:
CSI(termenv.BoldSeq)CSI(termenv.UnderlineSeq)CSI(termenv.ItalicSeq)
funcFgColor¶
FgColor returns a formatter that sets the foreground color.Example:
FgColor(termenv.RGBColor("#ff0000"))FgColor(termenv.ANSI256Color(196))FgColor(termenv.ANSIColor(31))
funcLineWrap¶
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.
typeStyle¶
type Style []Formatter
Style is a special Formatter that applies multiple Formatters to a textin order.
typeText¶
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.
func (*Text)Append¶
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¶
Bytes allocates a new byte slice containing the entire text.It uses the given buffer if it is large enough.
func (*Text)Head¶
Head returns the absolute head of the text.It adjusts the pointer to the head of the text.
func (*Text)Prepend¶
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¶
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.