Alaja.Helpers (Alaja v1.0.0)

Copy Markdown View Source

High-level drawing helpers for TUI applications.

Provides functions for common UI patterns like:

  • Braille sparklines
  • Progress bars with color gradients
  • Box drawing (single and double border)
  • Color interpolation utilities

Note: Basic ANSI escape functions have been moved to Alaja.ANSI. This module now focuses on high-level helpers only.

Summary

Functions

Draw a box with single-line border.

Generate a braille sparkline from a list of values (0-100).

Linear interpolation between two RGB colors.

Generate a progress bar with color gradient.

Safely converts a string to an existing atom.

Functions

box(x, y, w, h, title \\ "", color \\ {100, 140, 200})

Draw a box with single-line border.

Parameters

  • x, y: Starting position (1-indexed)
  • w, h: Width and height
  • title: Optional title (default: "")
  • color: RGB color as {r, g, b} (default: {100, 140, 200})

Returns

A list of {x, y, text} tuples for rendering.

Example

iex> Helpers.box(1, 1, 40, 10, "My Box", {100, 140, 200})
[{1, 1, "╭──────────────────────────────────────╮"}, ...]

braille_spark(values, width)

Generate a braille sparkline from a list of values (0-100).

Parameters

  • values: List of numbers (0-100)
  • width: Maximum number of braille characters to display

Returns

A string of braille characters representing the data.

Example

iex> Helpers.braille_spark([10, 50, 90, 30], 4)
"⣤ ⣿ ⣤ ⣀"

double_box(x, y, w, h, title \\ "", color \\ {180, 130, 80})

Draw a box with double-line border.

Parameters

  • x, y: Starting position (1-indexed)
  • w, h: Width and height
  • title: Optional title (default: "")
  • color: RGB color as {r, g, b} (default: {180, 130, 80})

Returns

A list of {x, y, text} tuples for rendering.

Example

iex> Helpers.double_box(1, 1, 40, 10, "Workers", {180, 130, 80})
[{1, 1, "╔══════════════════════════════════════╗"}, ...]

lerp(arg1, arg2, t)

Linear interpolation between two RGB colors.

Parameters

  • c1: Starting color {r, g, b}
  • c2: Ending color {r, g, b}
  • t: Interpolation factor (0.0 to 1.0)

Returns

Interpolated color as {r, g, b}.

Example

iex> Helpers.lerp({0, 0, 0}, {255, 255, 255}, 0.5)
{127, 127, 127}

progress_bar(pct, width, color_start, color_end)

Generate a progress bar with color gradient.

Parameters

  • pct: Percentage (0-100)
  • width: Width of the bar in characters
  • color_start: Starting color as {r, g, b}
  • color_end: Ending color as {r, g, b}

Returns

A string containing the progress bar with percentage display.

Example

iex> Helpers.progress_bar(75, 20, {80, 140, 255}, {200, 100, 255})
"███████████████████░░░ 75%"

safe_string_to_atom(s)

@spec safe_string_to_atom(String.t()) :: {:ok, atom()} | {:error, String.t()}

Safely converts a string to an existing atom.

Wraps String.to_existing_atom/1 in a try/rescue to prevent atom table exhaustion from untrusted input.

Returns {:ok, atom} on success, {:error, message} if the atom does not exist.