Raxol.Terminal.Commands.ParameterValidation (Raxol v0.3.0)

View Source

Provides 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

get_valid_bool_param(params, index, default)

@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 values
  • index - Index of the parameter to get
  • default - 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

get_valid_non_neg_param(params, index, default)

@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 values
  • index - Index of the parameter to get
  • default - 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

get_valid_param(params, index, default, min, max)

@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 values
  • index - Index of the parameter to get
  • default - Default value to use if parameter is invalid
  • min - Minimum allowed value
  • max - 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

get_valid_pos_param(params, index, default)

@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 values
  • index - Index of the parameter to get
  • default - 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

normalize_parameters(params, expected_length)

Normalizes parameters to expected length, padding with nil or truncating as needed.

validate_boolean(params)

Validates boolean, returns true for 1, false for 0. Defaults to true if invalid.

validate_color(params)

Validates color, clamping between 0 and 255. Defaults to 0 if invalid.

validate_coordinates(emulator, params)

Validates coordinates, clamping to emulator bounds. Defaults to {0, 0} if invalid.

validate_count(emulator, params)

Validates count, clamping between 1 and 10. Defaults to 1 if invalid.

validate_mode(params)

Validates mode, must be 0, 1, or 2. Defaults to 0 if invalid.

validate_range(params, min, max)

Validates a value in params[0], clamping between min and max. Defaults to min if invalid.