backend

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package backend defines the terminal backend interface for the TUI. This abstraction allows swapping between tcell (real terminals) and simulation backends (testing), enabling golden-frame tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AttrMask

type AttrMask uint32

AttrMask represents text attributes.

const (
	AttrBold AttrMask = 1 << iota
	AttrBlink
	AttrReverse
	AttrUnderline
	AttrDim
	AttrItalic
	AttrStrikeThrough
)

Attribute flags

type Backend

type Backend interface {
	// Init initializes the backend (enters alt screen, raw mode, etc).
	Init() error

	// Fini cleans up the backend (restores terminal state).
	Fini()

	// Size returns the current terminal dimensions.
	Size() (width, height int)

	// SetContent sets a cell at position (x, y) with the given rune and style.
	// The comb parameter contains combining characters (can be nil).
	SetContent(x, y int, mainc rune, comb []rune, style Style)

	// Show synchronizes the internal buffer to the terminal.
	// This is where actual output happens.
	Show()

	// Clear clears the screen.
	Clear()

	// HideCursor hides the terminal cursor.
	HideCursor()

	// ShowCursor shows the terminal cursor.
	ShowCursor()

	// SetCursorPos sets the cursor position.
	SetCursorPos(x, y int)

	// PollEvent blocks until an event is available and returns it.
	// Returns nil if the backend is shutting down.
	PollEvent() terminal.Event

	// PostEvent injects an event into the event queue.
	// Useful for testing and for posting timer/internal events.
	PostEvent(ev terminal.Event) error

	// Beep emits an audible bell.
	Beep()

	// Sync forces a full redraw on next Show().
	Sync()
}

Backend is the terminal abstraction layer. Implementations handle terminal I/O, input events, and screen rendering.

type Cell

type Cell struct {
	Rune  rune
	Style Style
}

Cell represents a single character cell for bulk updates.

type Color

type Color int32

Color represents a terminal color. Values 0-255 are palette colors, values >= 256 are true colors.

const (
	ColorDefault Color = -1
	ColorBlack   Color = 0
	ColorRed     Color = 1
	ColorGreen   Color = 2
	ColorYellow  Color = 3
	ColorBlue    Color = 4
	ColorMagenta Color = 5
	ColorCyan    Color = 6
	ColorWhite   Color = 7

	// Bright variants
	ColorBrightBlack   Color = 8
	ColorBrightRed     Color = 9
	ColorBrightGreen   Color = 10
	ColorBrightYellow  Color = 11
	ColorBrightBlue    Color = 12
	ColorBrightMagenta Color = 13
	ColorBrightCyan    Color = 14
	ColorBrightWhite   Color = 15
)

Color constants

func ColorRGB

func ColorRGB(r, g, b uint8) Color

ColorRGB creates a true color from RGB components.

func (Color) IsRGB

func (c Color) IsRGB() bool

IsRGB returns true if this is a true color (not palette).

func (Color) RGB

func (c Color) RGB() (r, g, b uint8)

RGB returns the red, green, blue components of an RGB color. Returns 0, 0, 0 for non-RGB colors.

type CursorShape

type CursorShape int

CursorShape controls the terminal cursor appearance.

const (
	CursorDefault   CursorShape = iota // Terminal default
	CursorBlock                        // Block cursor
	CursorUnderline                    // Underline cursor
	CursorBeam                         // Beam/bar cursor
)

type CursorShapeSetter

type CursorShapeSetter interface {
	SetCursorShape(shape CursorShape)
}

CursorShapeSetter allows backends to control cursor appearance. This is optional — backends that don't support it will be skipped.

type FrameWriter

type FrameWriter interface {
	WriteFrame(encoded []byte)
}

FrameWriter writes raw frame bytes to the backend.

type Image

type Image struct {
	Width      int
	Height     int
	CellWidth  int
	CellHeight int
	Format     ImageFormat
	Protocol   ImageProtocol
	Pixels     []byte
}

Image describes a pixel image to be rendered.

type ImageFormat

type ImageFormat int

ImageFormat identifies the pixel format.

const (
	ImageFormatRGBA ImageFormat = iota
)

type ImageProtocol

type ImageProtocol int

ImageProtocol identifies the terminal image protocol.

const (
	ImageProtocolKitty ImageProtocol = iota
	ImageProtocolSixel
)

type ImageTarget

type ImageTarget interface {
	SetImage(x, y int, img Image)
}

ImageTarget captures image operations during rendering.

type ImageWriter

type ImageWriter interface {
	DrawImage(x, y int, img Image)
}

ImageWriter renders images to the terminal backend.

type InlineHeightSetter

type InlineHeightSetter interface {
	SetInlineHeight(lines int)
}

InlineHeightSetter allows backends to constrain inline rendering to a fixed number of terminal rows.

type InlineModeSetter

type InlineModeSetter interface {
	SetInlineMode(enabled bool)
}

InlineModeSetter allows backends to opt into inline rendering mode. Inline mode should avoid switching to the alternate screen when possible.

type RectWriter

type RectWriter interface {
	SetRect(x, y, width, height int, cells []Cell)
}

RectWriter is an optional optimization for bulk rectangle updates. The cells slice is row-major and must have width*height entries.

type RenderTarget

type RenderTarget interface {
	Size() (width, height int)
	SetContent(x, y int, mainc rune, comb []rune, style Style)
}

RenderTarget is a subset of Backend for rendering operations only. Widgets render to this interface, not the full Backend.

type RowWriter

type RowWriter interface {
	SetRow(y int, startX int, cells []Cell)
}

RowWriter is an optional optimization for bulk row updates.

type Style

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

Style combines foreground, background colors and attributes.

func DefaultStyle

func DefaultStyle() Style

DefaultStyle returns the default style (default colors, no attributes).

func (Style) Attributes

func (s Style) Attributes() AttrMask

Attributes returns all attributes.

func (Style) BG

func (s Style) BG() Color

BG returns the background color.

func (Style) Background

func (s Style) Background(c Color) Style

Background sets the background color.

func (Style) BackgroundColor

func (s Style) BackgroundColor() Color

BackgroundColor returns the current background color.

func (s Style) Blink(on bool) Style

Blink enables or disables blink.

func (Style) Bold

func (s Style) Bold(on bool) Style

Bold enables or disables bold.

func (Style) Decompose

func (s Style) Decompose() (fg, bg Color, attrs AttrMask)

Decompose returns the foreground, background, and attributes.

func (Style) Dim

func (s Style) Dim(on bool) Style

Dim enables or disables dim.

func (Style) FG

func (s Style) FG() Color

FG returns the foreground color.

func (Style) Foreground

func (s Style) Foreground(c Color) Style

Foreground sets the foreground color.

func (Style) ForegroundColor

func (s Style) ForegroundColor() Color

ForegroundColor returns the current foreground color.

func (s Style) Hyperlink(url string) Style

Hyperlink sets the OSC-8 hyperlink URL.

func (Style) HyperlinkURL

func (s Style) HyperlinkURL() string

HyperlinkURL returns the OSC-8 hyperlink URL.

func (Style) Italic

func (s Style) Italic(on bool) Style

Italic enables or disables italic.

func (Style) Reverse

func (s Style) Reverse(on bool) Style

Reverse enables or disables reverse video.

func (Style) StrikeThrough

func (s Style) StrikeThrough(on bool) Style

StrikeThrough enables or disables strikethrough.

func (Style) Underline

func (s Style) Underline(on bool) Style

Underline enables or disables underline.

type SubTarget

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

SubTarget wraps a RenderTarget with an offset for sub-region rendering.

func NewSubTarget

func NewSubTarget(parent RenderTarget, x, y, w, h int) *SubTarget

NewSubTarget creates a sub-region of a RenderTarget.

func (*SubTarget) SetContent

func (s *SubTarget) SetContent(x, y int, mainc rune, comb []rune, style Style)

SetContent sets content with coordinates relative to the sub-target.

func (*SubTarget) Size

func (s *SubTarget) Size() (width, height int)

Size returns the sub-target dimensions.

type SyncOutputWriter

type SyncOutputWriter interface {
	// SetSyncOutput enables or disables synchronized output support.
	// Call this after detecting terminal capabilities.
	SetSyncOutput(enabled bool)

	// SyncOutputSupported reports whether the terminal supports
	// synchronized output (DEC mode 2026).
	SyncOutputSupported() bool

	// BeginSync writes the DEC mode 2026 enable sequence (\x1b[?2026h)
	// to the terminal, starting a synchronized update block.
	BeginSync()

	// EndSync writes the DEC mode 2026 disable sequence (\x1b[?2026l)
	// to the terminal, ending a synchronized update block. The terminal
	// flushes all buffered output at this point.
	EndSync()
}

SyncOutputWriter is an optional interface for backends that support DEC private mode 2026 (synchronized output). When the terminal supports this mode, wrapping a frame update in BeginSync/EndSync causes the terminal to buffer all output and apply it atomically, eliminating screen tearing.

The runtime detects terminal capabilities at startup and calls SetSyncOutput(true) when the terminal is known to support mode 2026. The render loop then checks SyncOutputSupported before each frame and wraps the update in BeginSync/EndSync.

Usage:

if sw, ok := backend.(SyncOutputWriter); ok && sw.SyncOutputSupported() {
    sw.BeginSync()
    defer sw.EndSync()
}
// ... send frame data ...
backend.Show()

Directories

Path Synopsis
Package sim provides a simulation backend for testing.
Package sim provides a simulation backend for testing.
Package tcell provides a Backend implementation using tcell.
Package tcell provides a Backend implementation using tcell.
Package web provides a backend that renders FluffyUI applications to a web browser via WebSocket.
Package web provides a backend that renders FluffyUI applications to a web browser via WebSocket.

Jump to

Keyboard shortcuts

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