Ask for a choice with instant single-key acceptance

SolidGroundUX DocumentationSolidGroundUX
NL

fn: ask_choose_immediate

Purpose

Prompt for a choice while allowing configured single-key choices to be accepted immediately.

Behavior

  • Reads one character at a time from /dev/tty.
  • Recognizes left and right arrow escape sequences.
  • Maps left/right arrows to < and > when those are configured as instant choices.
  • Maps Ctrl+R to the symbolic instant choice CTRL-R.
  • Accepts instant choices without requiring Enter.
  • Allows longer typed values to be completed with Enter.
  • Supports backspace editing for the typed buffer.
  • Validates the final response against the configured choice list when supplied.

Options

  • --label TEXT — Prompt label shown to the user.
  • --var NAME — Shell variable that receives the selected value. Defaults to choice.
  • --choices SPEC — Full accepted choice list, e.g. "A-D,Q".
  • --instantchoices SPEC — Choices accepted immediately after one key press.
  • --displaychoices 0|1 — Controls whether choices are appended to the label.
  • --keepasking 0|1 — Controls whether invalid choices are re-prompted. Defaults to 1.
  • --preservecase 0|1 — Preserves the exact case of instant single-key choices. Defaults to 0.
  • --labelwidth WIDTH — Visible width used to align the prompt label.
  • --pad COUNT — Number of spaces inserted before the prompt label.
  • --labelclr ANSI — ANSI sequence used for the label.
  • --inputclr ANSI — ANSI sequence used for the input.
  • --colorize MODE — Colorize mode passed to the prompt builder.

Outputs (globals)

Variable named by --var.

Returns

  • 0 after storing a valid selected value.
  • 1 when reading from /dev/tty fails.

Usage

  # Accept menu digits and Q immediately, without waiting for Enter.
  ask_choose_immediate --label "Menu" --choices "1-9,Q" \
      --instantchoices "1-9,Q" --var menu_choice

  # Accept left/right arrow keys as the symbolic choices < and >.
  ask_choose_immediate --label "Page" --choices "<,>,Q" \
      --instantchoices "<,>,Q" --var navigation