TermUI.Layout.Constraint (TermUI v0.2.0)
View SourceConstraint types for the layout system.
Constraints express how components request space from their parent container. They are declarative—describing desired outcome, not how to achieve it.
Constraint Types
length/1- Exact size in terminal cellspercentage/1- Fraction of parent size (0-100)ratio/1- Proportional share of remaining spacemin/1,max/1- Bounds on sizefill/0- Take all remaining space
Examples
# Fixed 20 cells
Constraint.length(20)
# 50% of parent
Constraint.percentage(50)
# 50% but at least 10 cells
Constraint.percentage(50) |> Constraint.with_min(10)
# Fill remaining space
Constraint.fill()
# 2:1 ratio distribution
[Constraint.ratio(2), Constraint.ratio(1)]Composition
Constraints can be composed with bounds using with_min/2 and with_max/2:
Constraint.percentage(50) |> Constraint.with_min(10) |> Constraint.with_max(100)This creates a constraint that requests 50% of parent, but at least 10 and at most 100 cells.
Summary
Functions
Creates a fill constraint that takes all remaining space.
Checks if a constraint is fixed (length or bounded length).
Checks if a constraint uses remaining space (ratio or fill).
Gets the maximum value from a constraint, if bounded.
Gets the minimum value from a constraint, if bounded.
Creates a length constraint for exactly n cells.
Creates a maximum size constraint.
Creates a minimum size constraint.
Creates combined min/max bounds.
Creates a percentage constraint for p% of parent size.
Creates a ratio constraint for proportional space distribution.
Resolves a constraint to a concrete size given available space.
Returns the constraint type as an atom.
Gets the inner constraint, unwrapping bounds.
Adds a maximum bound to a constraint.
Adds a minimum bound to a constraint.
Types
Functions
@spec fill() :: TermUI.Layout.Constraint.Fill.t()
Creates a fill constraint that takes all remaining space.
Fill is equivalent to ratio(1) in calculation but semantically distinct—
it means "take whatever is left" rather than "share proportionally".
Returns
A fill constraint struct.
Examples
# Main content area fills remaining space
Constraint.fill()Multiple fills distribute space equally among them.
Checks if a constraint is fixed (length or bounded length).
Fixed constraints are allocated first during solving.
Checks if a constraint uses remaining space (ratio or fill).
@spec get_max(t()) :: non_neg_integer() | nil
Gets the maximum value from a constraint, if bounded.
@spec get_min(t()) :: non_neg_integer() | nil
Gets the minimum value from a constraint, if bounded.
@spec length(non_neg_integer()) :: TermUI.Layout.Constraint.Length.t()
Creates a length constraint for exactly n cells.
Parameters
n- Number of cells (non-negative integer)
Returns
A length constraint struct.
Examples
iex> Constraint.length(20)
%TermUI.Layout.Constraint.Length{value: 20}
iex> Constraint.length(0)
%TermUI.Layout.Constraint.Length{value: 0}Errors
Raises ArgumentError if n is negative or not an integer.
@spec max(non_neg_integer()) :: TermUI.Layout.Constraint.Max.t()
Creates a maximum size constraint.
When used alone, acts as a maximum size requirement with fill behavior. When composed with another constraint, acts as an upper bound.
Parameters
n- Maximum size in cells (non-negative integer)
Returns
A max constraint struct with a fill constraint as default inner constraint.
Examples
# At most 100 cells
Constraint.max(100)Errors
Raises ArgumentError if n is negative or not an integer.
@spec min(non_neg_integer()) :: TermUI.Layout.Constraint.Min.t()
Creates a minimum size constraint.
When used alone, acts as a minimum size requirement. When composed with another constraint, acts as a lower bound.
Parameters
n- Minimum size in cells (non-negative integer)
Returns
A min constraint struct with a fill constraint as default inner constraint.
Examples
# At least 10 cells
Constraint.min(10)Errors
Raises ArgumentError if n is negative or not an integer.
@spec min_max(non_neg_integer(), non_neg_integer()) :: TermUI.Layout.Constraint.Min.t()
Creates combined min/max bounds.
Parameters
min_val- Minimum size in cellsmax_val- Maximum size in cells
Returns
A min constraint wrapping a max constraint with fill behavior.
Examples
# Between 10 and 100 cells
Constraint.min_max(10, 100)Errors
Raises ArgumentError if min > max or values are invalid.
@spec percentage(number()) :: TermUI.Layout.Constraint.Percentage.t()
Creates a percentage constraint for p% of parent size.
Parameters
p- Percentage value (0 to 100, can be float)
Returns
A percentage constraint struct.
Examples
iex> Constraint.percentage(50)
%TermUI.Layout.Constraint.Percentage{value: 50}
iex> Constraint.percentage(33.33)
%TermUI.Layout.Constraint.Percentage{value: 33.33}Errors
Raises ArgumentError if p is outside 0-100 range.
@spec ratio(number()) :: TermUI.Layout.Constraint.Ratio.t()
Creates a ratio constraint for proportional space distribution.
Ratio constraints share remaining space (after fixed and percentage allocations) proportionally among siblings with ratio constraints.
Parameters
r- Ratio value (positive number)
Returns
A ratio constraint struct.
Examples
# Two siblings with 2:1 ratio (first gets 2/3, second gets 1/3)
[Constraint.ratio(2), Constraint.ratio(1)]
# Three equal siblings
[Constraint.ratio(1), Constraint.ratio(1), Constraint.ratio(1)]Errors
Raises ArgumentError if r is not positive.
@spec resolve(t(), non_neg_integer(), keyword()) :: non_neg_integer()
Resolves a constraint to a concrete size given available space.
This is used by the constraint solver to calculate final sizes.
Parameters
constraint- The constraint to resolveavailable- Available space in cellsopts- Options including:remainingfor ratio calculations
Returns
The resolved size in cells (non-negative integer).
Examples
iex> Constraint.resolve(Constraint.length(20), 100)
20
iex> Constraint.resolve(Constraint.percentage(50), 100)
50
iex> Constraint.resolve(Constraint.fill(), 100, remaining: 30)
30
Returns the constraint type as an atom.
Useful for categorizing constraints during solving.
Examples
iex> Constraint.type(Constraint.length(20))
:length
iex> Constraint.type(Constraint.percentage(50))
:percentage
Gets the inner constraint, unwrapping bounds.
@spec with_max(t(), non_neg_integer()) :: TermUI.Layout.Constraint.Max.t()
Adds a maximum bound to a constraint.
Parameters
constraint- The constraint to boundmax_val- Maximum size in cells
Returns
The constraint wrapped in a max bound.
Examples
# 50% but at most 100 cells
Constraint.percentage(50) |> Constraint.with_max(100)
@spec with_min(t(), non_neg_integer()) :: TermUI.Layout.Constraint.Min.t()
Adds a minimum bound to a constraint.
Parameters
constraint- The constraint to boundmin_val- Minimum size in cells
Returns
The constraint wrapped in a min bound.
Examples
# 50% but at least 10 cells
Constraint.percentage(50) |> Constraint.with_min(10)