Documentation
¶
Overview ¶
Package richtext renders bracketed tag markup into ANSI-styled terminal output, gated by the terminal's colour capability. A Writer performs the rendering as an io.Writer.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnbalancedTag reports a closing tag that does not match the most // recently opened tag. ErrUnbalancedTag = errors.New("unbalanced closing tag") // ErrUnclosedTag reports that input ended while tags were still open. ErrUnclosedTag = errors.New("unclosed tag") )
var DefaultTheme = &Theme{ styles: map[string]style.Style{ "title": {Foreground: style.Green}, "heading": {Foreground: style.Yellow}, "label": {Foreground: style.Cyan}, "value": {Foreground: style.White}, "emphasis": {Foreground: style.White, Attributes: style.Bold}, "error": {Foreground: style.Red}, "warning": {Foreground: style.Yellow}, "info": {Foreground: style.Green}, "debug": {Foreground: style.Cyan}, "quote": {Foreground: style.BrightBlack}, "gutter": {Foreground: style.White}, "url": {Foreground: style.BrightWhite, Attributes: style.Underline}, }, }
DefaultTheme is the standard colour scheme used by the go-cli project. Users can derive themes from this to override basic stylings by using Theme.New.
Functions ¶
Types ¶
type TagError ¶
TagError describes a problem with a specific tag encountered while writing. It wraps one of the package's sentinel errors.
type Theme ¶
type Theme struct {
// contains filtered or unexported fields
}
Theme maps theme names to their styles. It is consulted by a Writer to resolve [theme:name] tags and is safe for concurrent reads. A Theme may derive from a parent (see Theme.New), falling back to it for names it does not define itself.
func NewTheme ¶
NewTheme builds a Theme that maps each name in styles to its style. A name is referenced by a [theme:name] tag, so it must be non-empty and must not contain a ']'; NewTheme panics if any name violates this.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer renders bracketed tag markup to an underlying writer as ANSI escapes.
Tags take the form [ns:field] and close with [/ns]. The namespaces are:
- fg / bg: a foreground or background colour, either one of the sixteen named ANSI colours or rgb(r,g,b);
- attr: a text attribute such as bold or italic, which accumulates while nested;
- theme: a named style registered in the Theme passed to NewWriter;
- richtext: with the field "off", opens a passthrough region whose contents are written verbatim without being parsed as tags, closed by [/richtext].
Tags must close in the reverse of the order they were opened. A known namespace with an unrecognised field renders as a reset. An unknown namespace is not a tag: it and its closing tag are emitted verbatim.
Colour output is governed by a term.ColourEnabler, term.DefaultEnabler by default; see Writer.EnableColour and Writer.ForceColour. When colour is disabled, text is written through unchanged and no escapes are emitted.
func NewWriter ¶
NewWriter returns a Writer that renders to dst, resolving [theme:name] tags against theme. theme may be nil, in which case every theme tag renders as a reset. The default colour policy is term.DefaultEnabler.
func (*Writer) Close ¶
Close flushes any trailing partial tag as literal text and reports whether the markup was balanced. It returns a *TagError wrapping ErrUnclosedTag if any tags remain open.
func (*Writer) EnableColour ¶
EnableColour selects the default colour policy when b is true, or disables colour entirely when b is false.
func (*Writer) Flush ¶
Flush drains any partial fragment buffered by the scanner to the destination as literal text, leaving the open-tag stack untouched. Unlike Writer.Close it never reports an unclosed tag.
func (*Writer) ForceColour ¶
func (w *Writer) ForceColour()
ForceColour emits colour unconditionally, regardless of the destination.
func (*Writer) Write ¶
Write implements io.Writer. It returns a *TagError wrapping ErrUnbalancedTag when a closing tag does not match the open tag.
func (*Writer) Writer ¶
Writer returns an io.Writer that writes verbatim to the destination without interpreting the bytes as tag markup.
This is the escape hatch for emitting untrusted text between an open and close tag: the active style is preserved and embedded closing tags cannot end the formatting early.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
sgr
Package sgr owns the raw ANSI Select Graphic Rendition escape sequences, so that the surrounding packages never hard-code escape bytes of their own.
|
Package sgr owns the raw ANSI Select Graphic Rendition escape sequences, so that the surrounding packages never hard-code escape bytes of their own. |
|
token
Package token scans the bracketed tag markup into a stream of tokens, supporting input delivered incrementally in arbitrary chunks.
|
Package token scans the bracketed tag markup into a stream of tokens, supporting input delivered incrementally in arbitrary chunks. |
|
Package style describes terminal text styling — foreground and background colours together with text attributes — as composable values that authors declare directly and reuse to build themes.
|
Package style describes terminal text styling — foreground and background colours together with text attributes — as composable values that authors declare directly and reuse to build themes. |