https://github.com/coder/pretty
TTY styles for Go
https://github.com/coder/pretty
Last synced: 15 days ago
JSON representation
TTY styles for Go
- Host: GitHub
- URL: https://github.com/coder/pretty
- Owner: coder
- License: cc0-1.0
- Created: 2023-09-05T15:36:32.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-08T20:59:47.000Z (over 1 year ago)
- Last Synced: 2025-04-02T22:35:30.206Z (23 days ago)
- Language: Go
- Homepage:
- Size: 39.1 KB
- Stars: 14
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pretty
[](https://pkg.go.dev/github.com/coder/pretty)`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](https://pkg.go.dev/github.com/muesli/termenv).
## Basic Usage
```go
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:
```go
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.