attributes

package
v0.0.0-...-bf0e5c6 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package attributes exposes an interface that knows how to deal with text presentation attributes (color, typeface, etc).

A Renderer is a unitary block of text, with an (optional) presentation context (StartSequence, EndSequence).

A sequence may typically be a terminal ANSI SGR escape sequence, but any tag language (e.g. HTML, LaTeX) works with a similar logic.

The State is a chained list of Renderers to track the context of Start/Stop sequences.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	Text          []rune
	StartSequence []rune
	StopSequence  []rune
	NestingLevel  int
}

Attribute implements a basic renderer.

The start and stop mode are rendered simply by writing their runes. This typically corresponds to a terminal use-case with ANSI escape sequences.

func New

func New(text, start, stop []rune) *Attribute

New attribute that will output to a io.Writer.

func (Attribute) HasStart

func (a Attribute) HasStart() bool

func (Attribute) HasStop

func (a Attribute) HasStop() bool

func (Attribute) Level

func (a Attribute) Level() int

func (Attribute) Render

func (a Attribute) Render(w runesio.Writer)

Render an attribute.

func (Attribute) Runes

func (a Attribute) Runes() []rune

func (*Attribute) SetLevel

func (a *Attribute) SetLevel(level int)

func (*Attribute) SetStop

func (a *Attribute) SetStop(stop []rune)

func (Attribute) Start

func (a Attribute) Start(w runesio.Writer)

func (Attribute) Stop

func (a Attribute) Stop(w runesio.Writer)

type Iterator

type Iterator interface {
	// Next indicates if there is a next item to be consumed.
	Next() bool
	// Item returns the currently iterated Renderer.
	Item() Renderer
}

Iterator walks a chained list of Renderers.

type Renderer

type Renderer interface {
	// Start renders the start formatting sequence, if any.
	Start(runesio.Writer)
	// Start renders the stop formatting sequence, if any.
	Stop(runesio.Writer)
	// Runes renders the runes of the token, without formatting sequences.
	Runes() []rune
	// Level indicates the level of nested start/stop sequences.
	Level() int
	// SetLevel sets the level of nested start/stop sequences.
	SetLevel(int)
	// HasStart is true when the renderer contains a start sequence.
	HasStart() bool
	// HasStop is true when the renderer contains a stop sequence.
	HasStop() bool

	// Render knows how to render the token with the formatting sequences.
	Render(runesio.Writer)

	SetStop([]rune)
}

Renderer knows how to

type Renderers

type Renderers []Renderer

Renderers is a collection of Renderer(s)

func (Renderers) Render

func (c Renderers) Render(w runesio.Writer)

func (Renderers) Start

func (c Renderers) Start(w runesio.Writer)

func (Renderers) Stop

func (c Renderers) Stop(w runesio.Writer)

type State

type State struct {
	// contains filtered or unexported fields
}

State maintains the state of attribute renderers.

The state is constructed from a flow of Renderers using Push().

func NewState

func NewState() *State

func (*State) First

func (s *State) First() *StateIterator

func (*State) Iterator

func (s *State) Iterator() *StateIterator

Iterator yields an iterator to walk over the renderers in this State, from first to last.

func (*State) Push

func (s *State) Push(attr Renderer)

type StateIterator

type StateIterator struct {
	// contains filtered or unexported fields
}

StateIterator iterates over the list of renderers.

A StateIterator knows how to close the current rendering context when reaching the end of line, and to resume the previous rendering context when starting a new line.

func (*StateIterator) EndOfLine

func (i *StateIterator) EndOfLine(w runesio.Writer)

EndOfLine closes the state of attributes to end the line with an empty state.

func (*StateIterator) Item

func (i *StateIterator) Item() Renderer

Item returns the currently iterated Renderer.

func (*StateIterator) Next

func (i *StateIterator) Next() bool

Next yields true if there are more items to consume.

func (*StateIterator) StartOfLine

func (i *StateIterator) StartOfLine(w runesio.Writer)

StartOfLine restores the state of attributes to start a new line.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL