flex

package
v0.0.0-alpha.15 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 1 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 flex shares a fixed number of terminal cells between tracks, the rows or columns of a container, using integer arithmetic only.

Split decides sizes and Place decides positions. Neither allocates when given a destination slice with enough capacity, so both are safe to call on every frame.

See the documentation for guides and examples. It is also available for LLMs and agents as llms.txt.

Index

Constants

View Source
const Collapsed = -1

Collapsed is the size Split reports for a track it dropped because the minimum sizes of all tracks did not fit the budget.

Variables

This section is empty.

Functions

func Fit

func Fit(dst []int, tracks []Track, wants []int) int

Fit returns the least space in which Split, given no gap, sizes every track at least as big as its want, which is clamped to the track's Min and Max: how much room growing tracks need for each to fit its content. dst is scratch storage for Split, as for its own dst.

func Place

func Place(dst, sizes []int, budget, gap int, justify Justify) []int

Place writes the offset of each track into dst, reusing its storage, and returns it. sizes are the result of Split; tracks that are collapsed or have no cells get the offset of the track after them and take no gap, so a track animating to zero leaves no gap behind.

func Split

func Split(dst []int, budget, gap int, tracks []Track, shrink Shrinking) []int

Split writes the size of each track into dst, reusing its storage, and returns it. Sizes add up to budget, minus the gaps between tracks that are neither collapsed nor empty (see Place), whenever the tracks allow: leftover space goes to growing tracks in proportion to their weights, and a deficit is taken from shrinking ones as shrink says. Shares are rounded to whole cells so that they add up, which spreads any remainder across the tracks. If the minimum sizes still do not fit (or with ShrinkFromEnd, the sizes before shrinking), collapsible tracks are dropped and reported as Collapsed; if the minimum sizes still do not fit, they overflow the budget.

Types

type Justify

type Justify uint8

Justify is where tracks go on the main axis when they do not fill it.

const (
	// Start packs tracks at the start.
	Start Justify = iota
	// Center packs tracks in the middle.
	Center
	// End packs tracks at the end.
	End
	// SpaceBetween puts the free space between tracks, none at the edges.
	SpaceBetween
	// SpaceAround puts equal space around each track, so the edges get half
	// as much as the space between tracks.
	SpaceAround
	// SpaceEvenly puts equal space between tracks and at both edges.
	SpaceEvenly
)

type Shrinking

type Shrinking uint8

Shrinking is how Split takes a deficit from the tracks that can shrink.

const (
	// ShrinkByBasis takes it from every shrinking track at once, in
	// proportion to its Shrink weight times its basis, so that each keeps
	// some of its size for as long as it can: as a row shares its width
	// between text that is cut short to fit. While it can, it leaves each
	// track at least one cell, rather than rounding a small one away while
	// the tracks after it keep theirs; when the tracks cannot each keep one,
	// each keeps no more than that, and the last give up even that first.
	ShrinkByBasis Shrinking = iota
	// ShrinkFromEnd takes it from the last shrinking track back, each down
	// to its Min before the one before it gives up any, so that the tracks
	// before keep their size and the last are cut off: as a column too short
	// for its rows shows the first of them. Shrink weights only say which
	// tracks can shrink. As cutting a track short loses some of it,
	// collapsible tracks are dropped first, while the tracks at their basis
	// do not fit, rather than only once their minimum sizes do not.
	ShrinkFromEnd
)

type Track

type Track struct {
	// Basis is the size before growing or shrinking.
	Basis int
	// Min and Max clamp the final size. A Max of zero or less is unbounded.
	Min, Max int
	// Grow is this track's share of leftover space, relative to the Grow of
	// the other tracks.
	Grow int
	// Shrink is this track's share of any deficit, weighted by its basis. A
	// track with zero Shrink never goes below its basis.
	Shrink int
	// Collapse makes the track collapsible. When even the minimum sizes do
	// not fit (or the sizes before shrinking, with [ShrinkFromEnd]; see
	// [Split]), collapsible tracks are dropped lowest Collapse first, and the
	// last one when tied.
	Collapse int
}

Track describes how one child takes part in sharing the main axis.

func Auto

func Auto(n int) Track

Auto is a track that starts at its content size n and can shrink.

func Fill

func Fill(weight int) Track

Fill is a track that starts empty and takes a weighted share of the leftover space.

func Fixed

func Fixed(n int) Track

Fixed is a track of exactly n cells.

func (Track) Empty

func (t Track) Empty() bool

Empty reports whether the track can never be given a cell, such as a fixed track of zero. Like a collapsed track, it takes no gap.

Jump to

Keyboard shortcuts

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