ExRatatui.Layout (ExRatatui v0.7.1)

Copy Markdown View Source

Layout system for splitting areas into sub-regions.

Uses ratatui's constraint-based layout engine to divide a Rect into multiple sub-regions along a direction.

Constraints

  • {:percentage, n} - percentage of the total space
  • {:length, n} - exact number of cells
  • {:min, n} - minimum number of cells
  • {:max, n} - maximum number of cells
  • {:ratio, numerator, denominator} - fractional ratio

Example

area = %Rect{x: 0, y: 0, width: 80, height: 24}

[header, body, footer] = Layout.split(area, :vertical, [
  {:length, 3},
  {:min, 0},
  {:length, 1}
])

[sidebar, main] = Layout.split(body, :horizontal, [
  {:percentage, 30},
  {:percentage, 70}
])

Summary

Functions

Splits a Rect into sub-regions based on direction and constraints.

Types

constraint()

@type constraint() ::
  {:percentage, non_neg_integer()}
  | {:length, non_neg_integer()}
  | {:min, non_neg_integer()}
  | {:max, non_neg_integer()}
  | {:ratio, non_neg_integer(), non_neg_integer()}

direction()

@type direction() :: :horizontal | :vertical

Functions

split(area, direction, constraints)

@spec split(ExRatatui.Layout.Rect.t(), direction(), [constraint()]) ::
  [ExRatatui.Layout.Rect.t()] | {:error, term()}

Splits a Rect into sub-regions based on direction and constraints.

Returns a list of %Rect{} structs or {:error, reason} on failure.

Examples

iex> alias ExRatatui.Layout
iex> alias ExRatatui.Layout.Rect
iex> area = %Rect{x: 0, y: 0, width: 80, height: 24}
iex> [top, bottom] = Layout.split(area, :vertical, [{:percentage, 50}, {:percentage, 50}])
iex> top
%Rect{x: 0, y: 0, width: 80, height: 12}
iex> bottom
%Rect{x: 0, y: 12, width: 80, height: 12}

iex> alias ExRatatui.Layout
iex> alias ExRatatui.Layout.Rect
iex> area = %Rect{x: 0, y: 0, width: 100, height: 1}
iex> [left, right] = Layout.split(area, :horizontal, [{:length, 20}, {:min, 0}])
iex> left
%Rect{x: 0, y: 0, width: 20, height: 1}
iex> right
%Rect{x: 20, y: 0, width: 80, height: 1}