Package decor provides facilities for decorating a string of characters with display attributes for the current (or specified) terminal type. This is done using a notation inspired by (but slightly different from) Zsh prompt formatting. Supported attributes are bold, italic, and/or underlined characters as well as 256-color support for foreground and background colors.
At its basis, this package is about decorating short blurbs of text to display in a terminal. As a simple example, consider the following program:
package main
import (
"fmt"
"os"
"toolman.org/terminal/decor"
)
func main() {
d, err := decor.New()
if err != nil {
panic(err)
}
input := "[@B@F{44}@Iuser@i@F{Orchid1}@@@F{Green3}host@f@b]"
output, err := d.Format(input)
if err != nil {
panic(err)
}
fmt.Printf("\nTERM....: %q\n\n", os.Getenv("TERM"))
fmt.Printf("Input...: %q\n\n", input)
fmt.Printf("Quoted..: %q\n\n", output)
fmt.Printf("RAW.....: %s\n\n", output)
}
When executed, this emits output similar to the following:
This shows I'm using a terminal of type rxvt-256color
, the input I'd like to format,
a quoted representation of the formatted output for this terminal and then the output
as rendered by the terminal itself.
Below is a quick-n-dirty chart that walks through each of the attribute markings used above: