Documentation
¶
Overview ¶
Package stain is a high-performance terminal styling library that consumes lipgloss.Style configurations and renders them to bytes.
Overview ¶
Stain compiles a lipgloss.Style into a *stain.Style for a given color profile, precomputing every ANSI sequence and static layout string. Subsequent calls to Render write the precomputed pieces into a Block, the measured-content type that carries width, height, and line offsets alongside the rendered bytes. Layout primitives (JoinHorizontal, JoinVertical, Place) operate on Blocks without re-measuring.
The hot path of stain.Style.Render makes no color decisions, builds no SGR sequences at runtime, and emits all bytes via direct []byte append. In steady-state hot loops with reused Blocks, Render is allocation-free.
Lifecycle ¶
lipgloss.Style ── Compile(profile) ──> *stain.Style ── Render ──> Block
Configuration, inheritance, and the fluent builder all live in lipgloss. Stain is purely the rendering layer.
Gaps from lipgloss ¶
The following lipgloss features are intentionally NOT supported:
- lipgloss.Style.Transform — call your transform on the input string before passing it to Render.
- lipgloss.Style.SetString — Render always takes content as an argument; there's no default content carried on the Style.
- StyleRanges — out of scope for v1.
Profile pinning ¶
A *stain.Style is pinned to the Profile passed to Compile. To target multiple profiles (e.g., user toggles a setting), Compile each style once per profile and look up the right one at render time. A built-in cache is deferred to a future version; callers manage their own map[string]*stain.Style for now.
Index ¶
- func JoinHorizontalTo(out *Block, pos lipgloss.Position, blocks ...Block)
- func JoinStringHorizontalTo(out *Block, pos lipgloss.Position, lines ...string)
- func JoinStringVerticalTo(out *Block, pos lipgloss.Position, lines ...string)
- func JoinVerticalTo(out *Block, pos lipgloss.Position, blocks ...Block)
- func PlaceTo(out *Block, w, h int, hPos, vPos lipgloss.Position, b Block)
- type Block
- func JoinHorizontal(pos lipgloss.Position, blocks ...Block) Block
- func JoinStringHorizontal(pos lipgloss.Position, lines ...string) Block
- func JoinStringVertical(pos lipgloss.Position, lines ...string) Block
- func JoinVertical(pos lipgloss.Position, blocks ...Block) Block
- func Place(w, h int, hPos, vPos lipgloss.Position, b Block) Block
- type Profile
- type Style
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JoinHorizontalTo ¶
JoinHorizontalTo is the output-parameter form of JoinHorizontal. It resets out before writing, reusing its buffer capacity.
func JoinStringHorizontalTo ¶
JoinStringHorizontalTo is the output-parameter form of JoinStringHorizontal.
func JoinStringVerticalTo ¶
JoinStringVerticalTo is the output-parameter form of JoinStringVertical.
func JoinVerticalTo ¶
JoinVerticalTo is the output-parameter form of JoinVertical. It resets out before writing, reusing its buffer capacity.
Types ¶
type Block ¶
type Block struct {
// contains filtered or unexported fields
}
Block is rendered, measured terminal content. The zero value is a valid empty block (width 0, height 0, no bytes).
Block carries the rendered bytes alongside its measured dimensions and per-line byte offsets, so layout primitives can compose multiple Blocks without re-measuring.
A Block's underlying buffer is owned by the Block; callers must not retain slices returned by Bytes across mutations.
func JoinHorizontal ¶
JoinHorizontal places blocks side by side, aligning rows according to pos (Top, Center, or Bottom). Shorter blocks are padded with spaces.
func JoinStringHorizontal ¶
JoinStringHorizontal is a convenience wrapper around JoinHorizontal that accepts raw strings instead of pre-measured Blocks.
func JoinStringVertical ¶
JoinStringVertical is a convenience wrapper around JoinVertical that accepts raw strings instead of pre-measured Blocks.
func JoinVertical ¶
JoinVertical stacks blocks top-to-bottom, aligning columns according to pos (Left, Center, or Right). Narrower blocks are padded with spaces.
func Place ¶
Place centers b inside a w-by-h cell, using hPos and vPos to control horizontal and vertical alignment respectively.
func (Block) Bytes ¶
Bytes returns the rendered bytes. The returned slice aliases the Block's internal buffer; callers must not modify it and must not retain it past the next Reset or render into the same Block.
func (*Block) Reset ¶
func (b *Block) Reset()
Reset clears b but retains its underlying buffer capacity for reuse. Use Reset between renders in a hot loop to avoid allocation.
type Profile ¶
type Profile int
Profile selects the color encoding used when compiling a Style. The zero value is NoColor.
type Style ¶
type Style struct {
// contains filtered or unexported fields
}
Style is the runtime form of a lipgloss.Style. It is profile-pinned and immutable after construction; safe for concurrent use.
Build a Style with Compile. The hot path is Render, which writes precomputed bytes into a Block.
func Compile ¶
Compile reads properties from s and produces a frozen, profile-pinned Style. Compile is one-shot per (style, profile); the returned *Style is reusable across many Render calls.
func (*Style) Render ¶
Render encodes content using s and returns a Block describing the result. content may contain ANSI escape sequences; their display width is computed via ansi.StringWidth.
func (*Style) RenderBlock ¶
RenderBlock wraps a pre-measured Block inside the frame described by s and returns the resulting Block. The input Block must have lineOffsets populated (i.e. it must have been produced by Render or RenderTo).
func (*Style) RenderBlockTo ¶
RenderBlockTo writes the frame-wrapped output into out, resetting it first. It skips text normalization, word-wrap, and per-line measurement — the input Block's dimensions are taken as ground truth.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Basic stain example: compile a lipgloss style and render some content.
|
Basic stain example: compile a lipgloss style and render some content. |
|
blending/linear-1d
command
Linear 1D blending example: render a series of color gradient bars using lipgloss.Blend1D and stain to render each block.
|
Linear 1D blending example: render a series of color gradient bars using lipgloss.Blend1D and stain to render each block. |
|
blending/linear-2d
command
Linear 2D blending example: render named gradient boxes using lipgloss.Blend2D and stain.
|
Linear 2D blending example: render named gradient boxes using lipgloss.Blend2D and stain. |
|
brightness
command
Brightness example: progressive Lighten and Darken variations of base colors, rendered as block bars.
|
Brightness example: progressive Lighten and Darken variations of base colors, rendered as block bars. |
|
color
command
Color example: a bordered "moderately ripe banana?" dialog with styled text and active/inactive buttons.
|
Color example: a bordered "moderately ripe banana?" dialog with styled text and active/inactive buttons. |
|
dashboard
command
Dashboard example: a TUI-style layout combining multiple stain Blocks via JoinHorizontal, JoinVertical, and Place.
|
Dashboard example: a TUI-style layout combining multiple stain Blocks via JoinHorizontal, JoinVertical, and Place. |
|
grid
command
Grid example: render a 5×3 grid of styled cells using JoinHorizontal and JoinVertical.
|
Grid example: render a 5×3 grid of styled cells using JoinHorizontal and JoinVertical. |
|
layout
command
Layout example: a full-page TUI layout demonstrating tabs, title block with gradient background, dialog box, list columns, color grid, marmalade history paragraphs, and a status bar.
|
Layout example: a full-page TUI layout demonstrating tabs, title block with gradient background, dialog box, list columns, color grid, marmalade history paragraphs, and a status bar. |
|
internal
|
|
|
encode
Package encode produces ANSI byte sequences for color and SGR attributes.
|
Package encode produces ANSI byte sequences for color and SGR attributes. |