TermUI.Layout.Constraint (TermUI v0.2.0)

View Source

Constraint 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

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

fill()

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.

fixed?(arg1)

@spec fixed?(t()) :: boolean()

Checks if a constraint is fixed (length or bounded length).

Fixed constraints are allocated first during solving.

flexible?(arg1)

@spec flexible?(t()) :: boolean()

Checks if a constraint uses remaining space (ratio or fill).

get_max(arg1)

@spec get_max(t()) :: non_neg_integer() | nil

Gets the maximum value from a constraint, if bounded.

get_min(arg1)

@spec get_min(t()) :: non_neg_integer() | nil

Gets the minimum value from a constraint, if bounded.

length(n)

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.

max(n)

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.

min(n)

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.

min_max(min_val, max_val)

Creates combined min/max bounds.

Parameters

  • min_val - Minimum size in cells
  • max_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.

percentage(p)

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.

ratio(r)

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.

resolve(constraint, available, opts \\ [])

@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 resolve
  • available - Available space in cells
  • opts - Options including :remaining for 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

type(arg1)

@spec type(t()) :: atom()

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

unwrap(constraint)

@spec unwrap(t()) :: t()

Gets the inner constraint, unwrapping bounds.

with_max(constraint, max_val)

Adds a maximum bound to a constraint.

Parameters

  • constraint - The constraint to bound
  • max_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)

with_min(constraint, min_val)

Adds a minimum bound to a constraint.

Parameters

  • constraint - The constraint to bound
  • min_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)