terminalcanvas

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrWriterUnavailable = errors.New("terminal canvas writer is unavailable")

ErrWriterUnavailable is latched when a canvas is constructed without its mandatory serialized output writer.

Functions

func DisplayCells

func DisplayCells(text string, cellWidth CellWidthFunc) int

DisplayCells escapes text before measuring it, matching Canvas output.

func EscapeText

func EscapeText(text string) string

EscapeText makes arbitrary display text inert before styling and measurement. Visible ASCII escapes preserve diagnostic meaning without letting user values move the cursor, split records, or change bidi ordering.

func GraphemeCellWidth

func GraphemeCellWidth(text string) int

GraphemeCellWidth follows terminal grapheme and East Asian/emoji width rules.

Types

type Canvas

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

Canvas serializes complete lines around at most one dynamic progress line. Writer failures are observable through Err but deliberately never returned from rendering calls, so diagnostics cannot become transfer authority.

func New

func New(config Config) *Canvas

New constructs a Canvas. Native callers should pass the serialized stderr writer here and the raw stderr handle only to NewNativeEnvironment.

func (*Canvas) Close

func (canvas *Canvas) Close()

Close finishes any active progress row once and disables later output.

func (*Canvas) Err

func (canvas *Canvas) Err() error

Err returns the first writer or short-write error.

func (*Canvas) FinishProgress

func (canvas *Canvas) FinishProgress()

FinishProgress removes the transient line and terminates its terminal row exactly once. It does not close the Canvas; later progress may start again.

func (*Canvas) Insert

func (canvas *Canvas) Insert(lines []Line)

Insert preserves complete records, letting the terminal wrap long lines so help and diagnostics never lose content. Only the transient progress row is clipped to the current width when it is redrawn after the insertion.

func (*Canvas) ReplaceProgress

func (canvas *Canvas) ReplaceProgress(line Line)

ReplaceProgress replaces the active dynamic line. When cursor ANSI is not available it writes an ordinary complete line instead of using carriage-return animation.

type Capabilities

type Capabilities struct {
	Interactive bool
	ANSI        bool
	Color       bool
	Unicode     bool
	Columns     int
}

Capabilities deliberately keeps terminal properties orthogonal. In particular, interactivity does not imply ANSI, and ANSI does not imply color.

type CapabilityProvider

type CapabilityProvider interface {
	Snapshot() Capabilities
}

CapabilityProvider is sampled at each render so resize events are observed.

type CapabilityProviderFunc

type CapabilityProviderFunc func() Capabilities

CapabilityProviderFunc is a deterministic injection seam for renderers/tests.

func (CapabilityProviderFunc) Snapshot

func (provider CapabilityProviderFunc) Snapshot() Capabilities

type CellWidthFunc

type CellWidthFunc func(string) int

CellWidthFunc reports terminal display cells, not bytes or runes.

type Clock

type Clock interface {
	Now() time.Time
}

Clock remains separate from terminal detection because progress sampling and ANSI policy have unrelated authorities.

type Config

type Config struct {
	Writer       io.Writer
	Capabilities CapabilityProvider
	CellWidth    CellWidthFunc
}

Config keeps the serialized writer independent from the raw descriptor used by native capability detection.

type Environment

type Environment struct {
	RawOutput    RawTerminal
	Capabilities CapabilityProvider
	Clock        Clock
}

Environment groups independently injectable native dependencies without coupling the raw terminal handle to serialized output.

func NewNativeEnvironment

func NewNativeEnvironment(rawOutput RawTerminal) Environment

NewNativeEnvironment detects capabilities from the raw output while leaving actual writes to a separately supplied Canvas writer.

type Line

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

Line is a complete semantic line without a trailing newline.

Its spans are private so a caller cannot mutate an active progress line while another goroutine is redrawing it.

func NewLine

func NewLine(spans ...Span) Line

NewLine copies spans into an immutable semantic line.

func Plain

func Plain(text string) Line

Plain constructs an unstyled semantic line.

func (Line) DisplayCells

func (line Line) DisplayCells(cellWidth CellWidthFunc) int

DisplayCells reports the width of the line exactly as Canvas will display it.

func (Line) Spans

func (line Line) Spans() []Span

Spans returns a copy suitable for renderer-side inspection or composition.

type RawTerminal

type RawTerminal interface {
	Fd() uintptr
}

RawTerminal exposes only the native identity used for capability detection. It must remain distinct from the serialized io.Writer owned by Canvas.

type Span

type Span struct {
	Text  string
	Style Style
}

Span is semantic terminal text. Text may be untrusted; Canvas escapes it before measuring or writing it.

type Style

type Style uint8

Style describes presentation intent without admitting terminal control bytes. The canvas remains the only component that translates intent into ANSI.

const (
	StyleDefault Style = iota
	StyleStrong
	StyleMuted
	StyleAccent
	StyleSuccess
	StyleWarning
	StyleError
)

type SystemClock

type SystemClock struct{}

SystemClock supplies monotonic-capable time.Time values to production callers.

func (SystemClock) Now

func (SystemClock) Now() time.Time

Jump to

Keyboard shortcuts

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