Raxol.Terminal.Commands.ParameterValidation (Raxol v0.4.0)
View SourceProvides parameter validation functions for CSI command handlers.
This module contains helper functions for validating and extracting parameters from CSI command sequences. Each function takes a list of parameters, an index, and a default value, returning either the validated parameter value or the default value if the parameter is invalid.
Summary
Functions
Gets a parameter value with validation for boolean values (0 or 1). Returns the parameter value if valid, or the default value if invalid.
Gets a parameter value with validation for non-negative integers. Returns the parameter value if valid, or the default value if invalid.
Gets a parameter value with validation. Returns the parameter value if valid, or the default value if invalid.
Gets a parameter value with validation for positive integers. Returns the parameter value if valid, or the default value if invalid.
Normalizes parameters to expected length, padding with nil or truncating as needed.
Validates boolean, returns true for 1, false for 0. Defaults to true if invalid.
Validates color, clamping between 0 and 255. Defaults to 0 if invalid.
Validates coordinates, clamping to emulator bounds. Defaults to {0, 0} if invalid.
Validates count, clamping between 1 and 10. Defaults to 1 if invalid.
Validates mode, must be 0, 1, or 2. Defaults to 0 if invalid.
Validates a value in params[0], clamping between min and max. Defaults to min if invalid.
Functions
@spec get_valid_bool_param([integer() | nil], non_neg_integer(), 0..1) :: 0..1
Gets a parameter value with validation for boolean values (0 or 1). Returns the parameter value if valid, or the default value if invalid.
Parameters
params
- List of parameter valuesindex
- Index of the parameter to getdefault
- Default value to use if parameter is invalid (must be 0 or 1)
Examples
iex> ParameterValidation.get_valid_bool_param([1, 2, 3], 0, 0)
1
iex> ParameterValidation.get_valid_bool_param([nil, 2, 3], 0, 0)
0
iex> ParameterValidation.get_valid_bool_param([2, 2, 3], 0, 0)
0
@spec get_valid_non_neg_param( [integer() | nil], non_neg_integer(), non_neg_integer() ) :: non_neg_integer()
Gets a parameter value with validation for non-negative integers. Returns the parameter value if valid, or the default value if invalid.
Parameters
params
- List of parameter valuesindex
- Index of the parameter to getdefault
- Default value to use if parameter is invalid
Examples
iex> ParameterValidation.get_valid_non_neg_param([1, 2, 3], 0, 0)
1
iex> ParameterValidation.get_valid_non_neg_param([nil, 2, 3], 0, 0)
0
iex> ParameterValidation.get_valid_non_neg_param([-1, 2, 3], 0, 0)
0
@spec get_valid_param( [integer() | nil], non_neg_integer(), integer(), integer(), integer() ) :: integer()
Gets a parameter value with validation. Returns the parameter value if valid, or the default value if invalid.
Parameters
params
- List of parameter valuesindex
- Index of the parameter to getdefault
- Default value to use if parameter is invalidmin
- Minimum allowed valuemax
- Maximum allowed value
Examples
iex> ParameterValidation.get_valid_param([1, 2, 3], 0, 0, 0, 9999)
1
iex> ParameterValidation.get_valid_param([nil, 2, 3], 0, 0, 0, 9999)
0
iex> ParameterValidation.get_valid_param([-1, 2, 3], 0, 0, 0, 9999)
0
@spec get_valid_pos_param( [integer() | nil], non_neg_integer(), pos_integer() ) :: pos_integer()
Gets a parameter value with validation for positive integers. Returns the parameter value if valid, or the default value if invalid.
Parameters
params
- List of parameter valuesindex
- Index of the parameter to getdefault
- Default value to use if parameter is invalid
Examples
iex> ParameterValidation.get_valid_pos_param([1, 2, 3], 0, 1)
1
iex> ParameterValidation.get_valid_pos_param([nil, 2, 3], 0, 1)
1
iex> ParameterValidation.get_valid_pos_param([0, 2, 3], 0, 1)
1
Normalizes parameters to expected length, padding with nil or truncating as needed.
Validates boolean, returns true for 1, false for 0. Defaults to true if invalid.
Validates color, clamping between 0 and 255. Defaults to 0 if invalid.
Validates coordinates, clamping to emulator bounds. Defaults to {0, 0} if invalid.
Validates count, clamping between 1 and 10. Defaults to 1 if invalid.
Validates mode, must be 0, 1, or 2. Defaults to 0 if invalid.
Validates a value in params[0], clamping between min and max. Defaults to min if invalid.