surface

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 7 Imported by: 2 modules, 1 package

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

Overview

Package surface describes how a component's edge and body are drawn — a line border, a filled card, a header band, a drop shadow — as data, and derives the colours that data needs from a palette.

A Surface never holds a colour. It names roles (Card, Shadow, EdgeActive, …), and a palette supplies them through Derive, so any surface can be drawn in any palette. Resolve pairs a surface with derived colours and the terminal's capabilities, following the surface's fallback chain until it reaches one the terminal can draw, and that resolved value is what a pane renders.

The package imports nothing from tuilib: palettes, themes and panes all depend on it, and it depends on none of them. See docs/surfaces.md.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Outlined is a line border around an unfilled body. Its overlay form
	// has thick lines, which is what makes a modal read as raised.
	Outlined = OutlinedWith(lipgloss.NormalBorder())

	// Banded is a card under a header band carrying the title. Focus colours
	// the band. It needs no block glyphs, which makes it the fallback for
	// the surfaces that do.
	Banded = Surface{
		Name: "banded",
		Rings: []Ring{
			margin(),
			{
				Top:        Side{Cell: Cell{Glyph: " ", BG: Band, BGFocused: EdgeActive}, Text: true},
				TopLeft:    Cell{Glyph: " ", BG: Band, BGFocused: EdgeActive},
				TopRight:   Cell{Glyph: " ", BG: Band, BGFocused: EdgeActive},
				Bottom:     Side{Cell: Cell{Glyph: " ", BG: Fill}, Text: true},
				Left:       Side{Cell: Cell{Glyph: " ", BG: Fill}},
				Right:      Side{Cell: Cell{Glyph: " ", BG: Fill}},
				BottomLeft: Cell{Glyph: " ", BG: Fill}, BottomRight: Cell{Glyph: " ", BG: Fill},
			},
		},
		Fill: Card, FillFocused: Card, Text: Text,
		Title: Text, TitleFocused: Screen, TitleBold: true, Slot: TextMuted,
		Divider:   DividerStyle{Glyph: "─", FG: Divider, FGActive: EdgeActive, BG: Fill},
		Selection: SelectionStyle{BG: Selection, BGBlurred: SelectionDim, FG: Text},
		Bar:       Screen,
		Needs:     Needs{Profile: termenv.ANSI256},
		Fallback:  &Outlined,
	}

	// Filled is a solid card whose half-block rows are its own edge. The
	// lips are its vertical margin too: the lower half of one card's ▀ and
	// the upper half of the next card's ▄ leave exactly one row of screen
	// between them, so it keeps a margin on the right only.
	Filled = Surface{
		Name: "filled",
		Rings: []Ring{
			{Right: Side{Cell: Cell{Glyph: " ", BG: Parent}}},
			{
				Top:    Side{Cell: Cell{Glyph: "▄", FG: Fill, BG: Parent}},
				Bottom: Side{Cell: Cell{Glyph: "▀", FG: Fill, BG: Parent}},
			},
			textRows(),
		},
		Fill: Card, FillFocused: CardRaised, Text: Text,
		Title: Text, TitleFocused: Accent, TitleBold: true, Slot: TextMuted,
		Divider:   DividerStyle{Glyph: "─", FG: Divider, FGActive: EdgeActive, BG: Fill},
		Selection: SelectionStyle{BG: Selection, BGBlurred: SelectionDim, FG: Text},
		Bar:       Screen,
		Needs:     Needs{Profile: termenv.ANSI256, BlockGlyphs: true},
		Fallback:  &Banded,
	}

	// Elevated is a card with a drop shadow below and to the right, and an
	// accent stripe down its left edge while focused. The shadow is its
	// margin.
	Elevated = Surface{
		Name: "elevated",
		Rings: []Ring{
			{
				Right:       Side{Cell: Cell{Glyph: "▌", FG: Shadow, BG: Parent}, First: Cell{Glyph: "▖", FG: Shadow, BG: Parent}},
				Bottom:      Side{Cell: Cell{Glyph: "▀", FG: Shadow, BG: Parent}, First: Cell{Glyph: " ", BG: Parent}},
				BottomRight: Cell{Glyph: "▘", FG: Shadow, BG: Parent},
			},
			{
				Top:         Side{Cell: Cell{Glyph: " ", BG: Fill}, Text: true},
				Bottom:      Side{Cell: Cell{Glyph: " ", BG: Fill}, Text: true},
				Left:        Side{Cell: Cell{Glyph: "▎", FG: Fill, FGFocused: Accent, BG: Fill}},
				Right:       Side{Cell: Cell{Glyph: " ", BG: Fill}},
				TopLeft:     Cell{Glyph: "▎", FG: Fill, FGFocused: Accent, BG: Fill},
				BottomLeft:  Cell{Glyph: "▎", FG: Fill, FGFocused: Accent, BG: Fill},
				TopRight:    Cell{Glyph: " ", BG: Fill},
				BottomRight: Cell{Glyph: " ", BG: Fill},
			},
		},
		Fill: Card, FillFocused: CardRaised, Text: Text,
		Title: Text, TitleFocused: Accent, TitleBold: true, Slot: TextMuted,
		Divider:   DividerStyle{Glyph: "─", FG: Divider, FGActive: EdgeActive, BG: Fill},
		Selection: SelectionStyle{BG: Selection, BGBlurred: SelectionDim, FG: Text},
		Bar:       Screen,
		Needs:     Needs{Profile: termenv.ANSI256, BlockGlyphs: true},
		Fallback:  &Banded,
	}

	// Accented is an unfilled body with a bar down its left edge, coloured
	// while focused.
	Accented = accented("▌", true)
)

The built-in surfaces. Filled is the default. Outlined draws the look tuilib had before surfaces byte for byte; the rest fill their body, except Accented.

Functions

func Contrast

func Contrast(a, b lipgloss.TerminalColor) float64

Contrast is the WCAG contrast ratio between two colours, from 1 to 21. It is 0 when either has no known RGB.

func EnsureContrast

func EnsureContrast(fg, bg lipgloss.TerminalColor, ratio float64) lipgloss.TerminalColor

EnsureContrast returns fg moved away from bg in lightness, keeping its hue, until their contrast ratio reaches at least ratio. When the hue runs out — a tinted colour can't get as bright as white — it gives up tint, blending toward white or black only as far as the ratio needs; past pure white or black there is nowhere left to go. Colours with no known RGB are returned unchanged.

func Legible

func Legible(fg, bg lipgloss.TerminalColor) bool

Legible reports whether text in fg reads on bg: their OKLab lightness differs by at least the text floor. Colours with no known RGB (ANSI indices) are the terminal's to render and count as legible.

Types

type Brackets

type Brackets int

Brackets is how slot text meets the line it is set into.

const (
	// BracketsNone runs the line straight through, so text sits inline on
	// it: ── text ──.
	BracketsNone Brackets = iota
	// BracketsCorners flanks the text with inverted corners, like a tab
	// dipping into the pane: ┐ text ┌.
	BracketsCorners
	// BracketsTees flanks it with junction tees: ─┤ text ├─.
	BracketsTees
)

type Caps

type Caps struct {
	// Profile is the terminal's colour profile.
	Profile termenv.Profile
	// BlockGlyphs reports whether block elements draw without seams.
	BlockGlyphs bool
}

Caps is what the terminal offers.

type Cell

type Cell struct {
	Glyph             string
	FG, FGFocused     Role
	BG, BGFocused     Role
	Bold, BoldFocused bool
}

Cell is a glyph and its colours. FG and BG are roles: Fill for the body's current tone, Parent for whatever contains the surface. The Focused variants apply while the pane has focus; None means "same as blurred".

func (Cell) Drawn

func (c Cell) Drawn() bool

Drawn reports whether the cell has a glyph.

type Colours

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

Colours is every role resolved to a concrete colour for one palette and one colour profile.

func Derive

func Derive(s Seeds, overrides map[Role]lipgloss.TerminalColor, profile termenv.Profile) Colours

Derive resolves every role for a palette's seeds. Surface tones are the Background raised in OKLab lightness toward the Foreground's, in the background's own hue (on a light palette, cards lighten toward white instead), kept a minimum lightness apart from the tone beneath them, and rounded to profile. An override replaces a role outright: it is rounded to profile but never pushed, because an author's choice is theirs.

A role mixed from a colour with no known RGB (an ANSI index below 16) uses a recipe from the seeds alone, so a palette written in ANSI indices still gets every tone it can.

func (Colours) Of

Of returns the colour for r, or nil when the role is unset (None, Parent, or a tone the palette couldn't derive).

func (Colours) Profile

func (c Colours) Profile() termenv.Profile

Profile is the colour profile the colours were rounded to.

func (Colours) Tonal

func (c Colours) Tonal() bool

Tonal reports whether the surface tones (Screen, Card, Shadow, …) could be derived. They can't when the background has no known RGB — an ANSI index, whose colour is whatever the terminal's scheme says — and a surface that fills its body then falls back to one that doesn't.

type DividerStyle

type DividerStyle struct {
	Glyph        string
	FG, FGActive Role
	BG           Role
}

DividerStyle is the row under a pinned header. FGActive applies while the header has input, which is how the filter shows it holds the keys.

type Insets

type Insets struct{ Top, Bottom, Left, Right int }

Insets is how many cells a surface's edge takes on each side.

type Needs

type Needs struct {
	// Profile is the poorest colour profile the surface still reads in.
	// termenv orders profiles richest first, so TrueColor (the zero value)
	// demands the most; ANSI256 is the usual floor for tonal surfaces and
	// Ascii accepts anything.
	Profile termenv.Profile
	// BlockGlyphs is set when the surface draws with block elements
	// (▄ ▀ ▌), which font-drawn terminals render with seams.
	BlockGlyphs bool
}

Needs is what a terminal must offer to draw a surface.

type Resolved

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

Resolved is a surface paired with concrete colours, ready to draw. It is built only by Resolve, so a pane can never be handed roles it would have to guess at. The zero value is Outlined with no colours, which draws exactly what a pane drew before surfaces existed.

func Resolve

func Resolve(s Surface, c Colours, caps Caps) Resolved

Resolve picks the surface to draw for s: s itself, or the first surface along its Fallback chain whose needs caps meets. A surface that fills its body also needs tonal colours, so a palette whose background has no RGB falls back too. The chain always ends at Outlined.

func (Resolved) Colour

func (r Resolved) Colour(role Role, focused bool) lipgloss.TerminalColor

Colour resolves role for the given focus state. Fill becomes the body's tone in that state; Parent and None resolve to nil, meaning "emit no colour".

func (Resolved) Colours

func (r Resolved) Colours() Colours

Colours are the colours the surface was resolved against.

func (Resolved) FellBack

func (r Resolved) FellBack() (asked, why string, ok bool)

FellBack reports the surface that was asked for and why it couldn't be drawn, when Resolve settled on a fallback.

func (Resolved) Field

func (r Resolved) Field() Resolved

Field is the drawn surface's form for single-line components, resolved against the same colours and capabilities.

func (Resolved) Insets

func (r Resolved) Insets() Insets

Insets is how many cells the drawn surface's edge takes on each side.

func (Resolved) IsZero

func (r Resolved) IsZero() bool

IsZero reports whether r was never resolved — a pane given no surface.

func (Resolved) Name

func (r Resolved) Name() string

Name is the drawn surface's name.

func (Resolved) Overlay

func (r Resolved) Overlay() Resolved

Overlay is the drawn surface's overlay form, resolved against the same colours and capabilities.

func (Resolved) OverlayField added in v0.43.0

func (r Resolved) OverlayField() Resolved

OverlayField is the drawn surface's field form on its overlay form — the fields of a modal form — resolved against the same colours and capabilities.

func (Resolved) Surface

func (r Resolved) Surface() Surface

Surface is the surface that will be drawn.

func (Resolved) With

func (r Resolved) With(s Surface) Resolved

With resolves s against the same colours and capabilities as r — how a screen pins one component to a different surface from the app's.

type Ring

type Ring struct {
	Top, Bottom, Left, Right Side

	TopLeft, TopRight, BottomLeft, BottomRight Cell
}

Ring is one cell's depth of edge around a surface. A side with an empty glyph isn't drawn and takes no space. Where two drawn sides meet, the corner cell is used; a side whose neighbours aren't drawn runs the full length of the ring.

type Role

type Role int

Role is a named colour slot. Surfaces refer to colours only by role.

const (
	// None leaves a colour unset: the component's own default foreground, or
	// no background at all.
	None Role = iota

	// Screen is behind and between surfaces: the palette's background.
	Screen
	// Card is a filled surface's body.
	Card
	// CardRaised is a filled surface's body while it has focus.
	CardRaised
	// Band is Banded's header and a filled surface's divider step.
	Band
	// Overlay is the body of a surface's overlay form — modals.
	Overlay
	// OverlayCard is a card drawn on an overlay: a field in a modal form.
	OverlayCard
	// OverlayCardRaised is an OverlayCard while it has focus.
	OverlayCardRaised
	// Button is a button drawn as a chip on a filled surface: a toggle's
	// side, a modal's choice, a form's submit. The chosen one takes Accent.
	Button
	// Shadow is Elevated's drop shadow.
	Shadow
	// Selection is a focused pane's selected row.
	Selection
	// SelectionDim is a blurred pane's selected row.
	SelectionDim
	// Bar is the breadcrumb and statusbar band under Outlined.
	Bar
	// Text is body text on a filled surface.
	Text
	// TextMuted is secondary text: slots, counters, hints.
	TextMuted
	// Edge is a line or bar on a blurred surface.
	Edge
	// EdgeActive is a line or bar on a focused surface, and the active band.
	EdgeActive
	// Divider is the rule under a pinned header.
	Divider
	// Accent is emphasis: a focused title, Accented's bar.
	Accent

	// Fill stands for whichever role the surface's body is filled with in its
	// current focus state — Card, CardRaised, Overlay. A lip or a stripe
	// drawn in Fill always matches the body it borders.
	Fill
	// Parent stands for whatever contains the surface. A cell on Parent is
	// emitted with no background, so the container paints it: an outer
	// pane's fill, or the screen. Margins and shadows sit on Parent.
	Parent
)

func (Role) String

func (r Role) String() string

type Seeds

type Seeds struct {
	// Background is what the screen is. Unset, Bar stands in for it.
	Background lipgloss.TerminalColor
	// Foreground is body text on a filled surface. Unset, BarText stands in.
	Foreground lipgloss.TerminalColor

	Bar, BarText, Muted, Subtle, Edge, EdgeActive, Accent lipgloss.TerminalColor
}

Seeds are the palette colours every role is derived from. Background and Foreground are the palette's canonical pair; the rest pass straight through to the roles of the same meaning.

type SelectionStyle

type SelectionStyle struct {
	// FromTheme keeps each component's own selected style, as built by the
	// theme. Outlined does this, so it stays byte-identical to the look
	// before surfaces.
	FromTheme bool
	// BG is a focused pane's selected row, BGBlurred a blurred one's, and FG
	// the text on either.
	BG, BGBlurred, FG Role
}

SelectionStyle is how a selected row is painted.

type Side

type Side struct {
	Cell
	// First, when its glyph is set, replaces the side's first cell (the top
	// of a vertical side, the left of a horizontal one) — where a shadow
	// starts half a cell in, say.
	First Cell
	// Text marks a row that carries the pane's slots: the title on top, and
	// counters, hints and errors along the bottom. Every surface has one
	// text row on each.
	Text bool
}

Side is one side of a ring.

type Surface

type Surface struct {
	// Name identifies the surface in config files and pickers.
	Name string

	// Rings are the edge, outermost first. Each ring takes one cell on each
	// side it draws, so a surface's inset on a side is the number of rings
	// drawing there. Margins and shadows are rings too: a surface draws only
	// inside the rect it is given.
	Rings []Ring

	// Fill and FillFocused are the body's background, blurred and focused.
	// None leaves the body unfilled, as Outlined and Accented do.
	Fill, FillFocused Role
	// Text is the body's foreground on a filled surface. None keeps the
	// terminal's own.
	Text Role

	// Title and TitleFocused colour the title slot. TitleBold makes it bold.
	Title, TitleFocused Role
	TitleBold           bool
	// Slot colours the other slots: counters, hints, errors.
	Slot Role
	// Brackets controls how slot text set into a line meets it — inline,
	// "┐ title ┌" or "┤ title ├". Only a lined surface has a line to set
	// text into; the rest ignore it.
	Brackets Brackets

	// Divider is the row the pane draws under a pinned header.
	Divider DividerStyle
	// Selection is how a selected row is painted.
	Selection SelectionStyle
	// Bar is the role the breadcrumb and statusbar sit on.
	Bar Role

	// Overlay is the form this surface takes for modals. Nil derives it: the
	// same surface with its body filled in the Overlay tone — filled even
	// when the surface itself isn't, since a modal must cover what is under
	// it.
	Overlay *Surface
	// Field is the form this surface takes for single-line components —
	// input, filter, toggle — which screens place in three-row slots. It
	// must take at most one row of edge above and below, and one cell on the
	// left; the right may take a margin besides. Nil derives it: a lined
	// surface is its own field, and anything else keeps its inner rings, as
	// many as fit one cell per side — a card's text rows and focus effects,
	// without its margins, lips or shadow. A margined surface's field has a
	// margin of its own: a cell to the right, so it lines up with the cards
	// around it, and its bottom text row moved onto the parent, so stacked
	// fields don't run together. A surface whose inner rings carry no text
	// rows draws an outlined field.
	Field *Surface

	// Needs is what the terminal must offer to draw this surface, and
	// Fallback is drawn instead when it doesn't. Nil ends the chain.
	Needs    Needs
	Fallback *Surface
}

Surface is how a component's edge and body are drawn. It is a value, like lipgloss.Border: the pane interprets it, so a surface declares geometry and colour roles and never draws anything itself. A custom surface is a struct literal; start from a built-in and change what differs.

func Builtins

func Builtins() []Surface

Builtins are the surfaces the library ships, in picker order: Filled, the default, first.

func OutlinedWith

func OutlinedWith(border lipgloss.Border) Surface

OutlinedWith is Outlined drawn with border's lines — rounded, thick, double.

func (Surface) Filled

func (s Surface) Filled() bool

Filled reports whether the surface paints its body.

func (Surface) Insets

func (s Surface) Insets() Insets

Insets counts the drawn sides of every ring.

func (Surface) Lined

func (s Surface) Lined() (lipgloss.Border, bool)

Lined reports whether s is a single ring of lines around an unfilled body — Outlined and its variants — and returns that ring as a lipgloss.Border. A pane draws a lined surface as the line border it always drew, so Outlined stays byte-identical to the look before surfaces existed.

func (Surface) Margined

func (s Surface) Margined() bool

Margined reports whether s keeps a margin on its right — an outermost ring drawn on the parent there. The app shell pads the screen's left edge to match when the current surface does.

func (Surface) TopGap

func (s Surface) TopGap() bool

TopGap reports whether s wants a row of the parent above it. A surface whose top edge starts with a solid row — a title, a band — reads as jammed against whatever is above it; one whose top edge is already a lip or a line (Filled's ▄, Outlined's border) brings its own gap. The app shell adds the row under the breadcrumb when the current surface asks.

Jump to

Keyboard shortcuts

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