Alaja.Printer.Interactive (Alaja v2.4.0)

Copy Markdown View Source

Interactive user-input functions for terminal CLI applications.

Provides prompts for questions, yes/no confirmation, predefined-option selection, and bullet-list menus.

Usage

answer = Alaja.Printer.Interactive.question("What's your name?")
:yes  = Alaja.Printer.Interactive.yesno("Continue?")
Alaja.Printer.Interactive.menu("Select option:", [{"A", :a}, {"B", :b}])

Summary

Functions

Asks a question to the user and returns their answer.

Question with predefined options.

Yes or no question.

Functions

question(text, opts \\ [])

@spec question(
  String.t(),
  keyword()
) :: String.t()

Asks a question to the user and returns their answer.

Options

  • :color - Text color (default: :white)
  • :align - Text alignment (default: :left)

Examples

iex> question("What is your name?")
"John"

iex> question("Enter value:", color: :cyan)
"42"

question_with_options(text, options, opts \\ [])

@spec question_with_options(String.t(), list(), keyword()) :: any() | :error

Question with predefined options.

The options list can take three shapes:

  1. [{label, value}, ...] with string labels — the user types the label (or a prefix of it, case-insensitive).
  2. [{label, value}, ...] where value is an atom — the user can type the atom name (e.g. :llmllm).
  3. A 1-based index (1, 2, 3) typed by the user.

Before reading the answer, the function lists the options under the prompt so the user always knows what they can type. The :default option chooses a pre-selected index that gets used if the user just presses Enter.

Returns the value for the selected option, or :error if the input does not match anything.

Examples

iex> question_with_options("Choose:", [{"Yes", :yes}, {"No", :no}])
:yes

iex> question_with_options("Continue?", [{"Y", :yes}, {"N", :no}], default: 1)
:yes

yesno(text, opts \\ [])

@spec yesno(
  String.t(),
  keyword()
) :: :yes | :no

Yes or no question.

Returns :yes or :no.

Accepts y, yes, n, no, plus the index 1 for yes and 2 for no. The :default option (:yes or :no) is used when the user just presses Enter.

Examples

iex> yesno("Do you want to continue?")
:yes

iex> yesno("Are you sure?", default: :yes)
:yes