TermUI.Input.Selector (TermUI v1.0.0)
View SourceSelects the appropriate input handler based on the active backend mode.
This module bridges the gap between backend selection and input handling, providing a way to choose the correct input handler for the current terminal mode.
The runtime calls select/1 after it has selected a backend. select/0 is a
standalone convenience that calls Backend.Selector.select/0; on a capable
system that call may activate OTP's Raw shell, so do not use it as a
read-only query. Use TermUI.Runtime.backend_mode/0 to query published local
runtime context.
Relationship with Backend.Selector
The TermUI.Backend.Selector determines which terminal backend to use
(Raw or TTY). This module, TermUI.Input.Selector, then selects the
corresponding input handler:
| Backend Mode | Backend Module | Input Handler |
|---|---|---|
:raw | TermUI.Backend.Raw | TermUI.Input.Raw |
:tty | TermUI.Backend.TTY | TermUI.Input.TTY |
Usage
There are two ways to select an input handler:
Explicit Selection
When you know which mode you want, use select/1:
# Get the Raw input handler
handler = TermUI.Input.Selector.select(:raw)
# => TermUI.Input.Raw
# Get the TTY input handler
handler = TermUI.Input.Selector.select(:tty)
# => TermUI.Input.TTYAuto-Detection
When you want to match the current backend, use select/0:
# Automatically select based on current backend
handler = TermUI.Input.Selector.select()
# => TermUI.Input.Raw or TermUI.Input.TTYState-Based Selection
For runtime code that already has a Backend.State struct, you can
extract the mode and pass it directly:
backend_state = %TermUI.Backend.State{backend_mode: :tty, ...}
handler = TermUI.Input.Selector.select(backend_state.backend_mode)Input Handler Contract
Both TermUI.Input.Raw and TermUI.Input.TTY implement the TermUI.Input
behaviour, providing a consistent interface:
new/0- Create initial handler statepoll/2- Poll for input with timeoutmode/1- Return the handler's mode (:rawor:tty)
Example Integration
# Typical usage in runtime initialization
case TermUI.Backend.Selector.select() do
{:raw, backend_state} ->
input_handler = TermUI.Input.Selector.select(:raw)
input_state = input_handler.new()
# ...
{:tty, capabilities} ->
input_handler = TermUI.Input.Selector.select(:tty)
input_state = input_handler.new()
# ...
endNote on LineReader
TermUI.Input.LineReader is not included in the selector. LineReader
is a specialized module for line-based input (used by TextInput.Line)
and does not implement the TermUI.Input behaviour. Use LineReader
directly when you need line-based input with shell editing.
Summary
Functions
Selects the appropriate input handler based on the current backend mode.
Selects the input handler for the specified mode.
Types
@type handler() :: module()
Input handler module that implements the TermUI.Input behaviour.
@type mode() :: :raw | :tty
Valid input mode atoms.
:raw- SelectTermUI.Input.Rawfor raw mode input:tty- SelectTermUI.Input.TTYfor TTY mode input
Functions
@spec select() :: handler()
Selects the appropriate input handler based on the current backend mode.
This function auto-detects the current backend mode by attempting to determine whether raw mode is active. If detection cannot determine the mode, it defaults to TTY mode as the safer fallback.
Returns
TermUI.Input.Rawif raw mode is activeTermUI.Input.TTYif TTY mode is active or mode cannot be determined
Examples
handler = TermUI.Input.Selector.select()
state = handler.new()
{result, state} = handler.poll(state, 100)Implementation Note
This function uses TermUI.Backend.Selector.select/0 to determine the
current mode. This means it will attempt raw mode detection each time
it's called. For performance, prefer using select/1 with an explicit
mode when the mode is already known from backend initialization.
Selects the input handler for the specified mode.
This function provides explicit selection when the mode is already known, avoiding the overhead of backend detection.
Arguments
mode- The input mode::rawor:tty
Returns
TermUI.Input.Rawfor:rawmodeTermUI.Input.TTYfor:ttymode
Raises
ArgumentErrorif an invalid mode is provided
Examples
# Select Raw input handler
handler = TermUI.Input.Selector.select(:raw)
# => TermUI.Input.Raw
# Select TTY input handler
handler = TermUI.Input.Selector.select(:tty)
# => TermUI.Input.TTY
# Using with Backend.State
backend_state = %TermUI.Backend.State{backend_mode: :tty, ...}
handler = TermUI.Input.Selector.select(backend_state.backend_mode)
# Invalid mode raises
TermUI.Input.Selector.select(:invalid)
# ** (ArgumentError) invalid input mode: :invalid, expected :raw or :tty