Raxol.Playground.DemoHelpers (Raxol v2.6.1)

View Source

Shared helpers for playground demo TEA apps.

Small utilities that eliminate the most common duplication across demos while keeping demos self-contained and readable.

Summary

Functions

Moves a cursor index down (increment), clamped to max_index.

Returns "> " if index matches selected, else " ".

Moves a cursor index up (decrement), clamped to 0.

Cycles an index forward through a list length, wrapping around.

Returns the effective width for a demo element, clamping desired to the available width injected by the playground app. Falls back to desired when running outside the playground.

Renders the demo's event log as a titled block of dim text lines, newest first. Returns [element] for splicing into a column's children.

Navigate forward through input history.

Navigate backward through input history.

Prepend an entry to the demo's event log (newest-first), trimming to limit.

Renders content (Markdown text) through Raxol.UI.Components.MarkdownRenderer, the canonical Markdown-to-styled-elements renderer.

Renders text through Raxol.UI.Components.Display.Text (canonical wrapping/truncation entry point, docs/core/LAYOUT.md §4).

Functions

cursor_down(current, max_index)

@spec cursor_down(non_neg_integer(), non_neg_integer()) :: non_neg_integer()

Moves a cursor index down (increment), clamped to max_index.

cursor_prefix(index, selected)

@spec cursor_prefix(non_neg_integer(), non_neg_integer()) :: String.t()

Returns "> " if index matches selected, else " ".

cursor_up(current)

@spec cursor_up(non_neg_integer()) :: non_neg_integer()

Moves a cursor index up (decrement), clamped to 0.

cycle_next(current, count)

@spec cycle_next(non_neg_integer(), non_neg_integer()) :: non_neg_integer()

Cycles an index forward through a list length, wrapping around.

effective_width(model, desired)

@spec effective_width(map(), pos_integer()) :: pos_integer()

Returns the effective width for a demo element, clamping desired to the available width injected by the playground app. Falls back to desired when running outside the playground.

event_log_lines(model, opts \\ [])

@spec event_log_lines(
  map(),
  keyword()
) :: [map()]

Renders the demo's event log as a titled block of dim text lines, newest first. Returns [element] for splicing into a column's children.

Options: :title (default "events"), :empty (text shown when no entries yet, default "(no events yet — interact above)").

history_next(model)

@spec history_next(map()) :: map()

Navigate forward through input history.

Expects the model to have :input_history, :history_index, :input, and :cursor fields.

history_prev(model)

@spec history_prev(map()) :: map()

Navigate backward through input history.

Expects the model to have :input_history, :history_index, :input, and :cursor fields.

log_event(model, entry, limit \\ 8)

@spec log_event(map(), String.t(), pos_integer()) :: map()

Prepend an entry to the demo's event log (newest-first), trimming to limit.

The event log is the storybook "actions panel": every demo that mounts a real component records the events it routes and the outcomes it observes, rendered at the bottom of the demo via event_log_lines/2. Entries are plain strings built by the demo (it knows how to summarize its own events), stored in the model's :event_log field.

Examples

model
|> DemoHelpers.log_event("key \"a\" -> len=4 cursor=4")
|> DemoHelpers.log_event("focus -> focused=true")

markdown(content, width)

@spec markdown(String.t(), pos_integer()) :: map()

Renders content (Markdown text) through Raxol.UI.Components.MarkdownRenderer, the canonical Markdown-to-styled-elements renderer.

Examples

markdown("# Title\n\nSome **bold** text.", 40)

rich_text(content, opts \\ [])

@spec rich_text(
  String.t(),
  keyword()
) :: map()

Renders text through Raxol.UI.Components.Display.Text (canonical wrapping/truncation entry point, docs/core/LAYOUT.md §4).

Plain Raxol.Core.Renderer.View.text/2 accepts only fg/bg/style/align/wrap/link.

Examples

rich_text("a very long line", width: 12, white_space: :nowrap, text_overflow: :ellipsis)
rich_text(prose, width: 20, text_wrap: :pretty)
rich_text(prose, width: 20, line_clamp: 2)