TermUI.Layout.Alignment (TermUI v0.2.0)
View SourceFlexbox-inspired alignment for positioning components within allocated space.
Alignment Model
- Main axis: Direction of layout (X for horizontal, Y for vertical)
- Cross axis: Perpendicular to main axis
Justify Content (Main Axis)
:start- Pack at beginning:center- Center in space:end- Pack at end:space_between- Equal space between components:space_around- Equal space around each component
Align Items (Cross Axis)
:start- Position at cross-axis start:center- Center on cross-axis:end- Position at cross-axis end:stretch- Expand to fill cross-axis
Examples
# Apply alignment to solved rects
rects = Solver.solve_to_rects(constraints, area)
aligned = Alignment.apply(rects, area,
direction: :horizontal,
justify: :space_between,
align: :center
)
# With margins
aligned = Alignment.apply_with_spacing(rects, area,
direction: :horizontal,
margin: %{top: 5, right: 5, bottom: 5, left: 5}
)
Summary
Functions
Applies alignment to a list of rectangles within a container area.
Applies margin to rectangles, shrinking them.
Applies padding to a rectangle, shrinking the content area.
Parses spacing shorthand into a spacing map.
Types
@type align() :: :start | :center | :end | :stretch
@type direction() :: :horizontal | :vertical
@type justify() :: :start | :center | :end | :space_between | :space_around
Functions
Applies alignment to a list of rectangles within a container area.
Parameters
rects- list of rectangles from solverarea- container bounding rectangleopts- alignment options:direction-:horizontal(default) or:vertical:justify- main axis alignment (default:start):align- cross axis alignment (default:start):align_self- per-component cross axis overrides
Returns
List of aligned rectangles.
Applies margin to rectangles, shrinking them.
Parameters
rects- list of rectanglesmargins- list of margin maps (one per rect) or single margin for all
Returns
List of rectangles with margins applied.
Applies padding to a rectangle, shrinking the content area.
Parameters
rect- rectangle to padpadding- padding map
Returns
Rectangle with padding applied (position adjusted, size reduced).
@spec parse_spacing( integer() | {integer(), integer()} | {integer(), integer(), integer(), integer()} ) :: spacing()
Parses spacing shorthand into a spacing map.
Examples
iex> Alignment.parse_spacing(10)
%{top: 10, right: 10, bottom: 10, left: 10}
iex> Alignment.parse_spacing({5, 10})
%{top: 5, right: 10, bottom: 5, left: 10}
iex> Alignment.parse_spacing({1, 2, 3, 4})
%{top: 1, right: 2, bottom: 3, left: 4}