Module terminal.utils

Support functions.

Functions

class ([baseclass]) Creates a (sub)class.
invalid_constant (value, constants[, prefix="Invalid value: "]) Returns an error message for an invalid lookup constant.
make_lookup ([value_type="value"], t) Converts a lookup table to a constant table with user friendly error reporting.
resolve_index (index, max_value[, min_value=1]) Resolve indices.
throw_invalid_constant (value, constants[, prefix="Invalid value: "[, err_lvl=1]]) Throws an error message for an invalid lookup constant.
utf8sub (str, i, j) Like string:sub(), Returns the substring of the string that starts from i and go until j inclusive, but operators on utf8 characters Preserves utf8.len()


Functions

class ([baseclass])

Creates a (sub)class. This function creates a new class, which is a subclass of the given baseclass. An instance can be created by calling on the class, any parameteres passed in will be passed on to the init method. The init method (if present), will be called upon instantiation.

Every instance will:

  • have a super property, which points to the class itself.
  • upon creation call the init method (if present) with the parameters passed when calling on the Class.

Parameters:

  • baseclass class The base-class to inherit from. (optional)

Returns:

    table The new class.

Usage:

    local Cat = utils.class()
    function Cat:init(value)
      self.value = value or 42
    end
    
    local Lion = utils.class(Cat)
    function Lion:init(value)
      assert(self.super == Cat, "Superclass is not a Cat")
      Cat.init(self, value)   -- call ancestor initializer
      self.value = self.value * 2
    end
    
    local instance1 = Lion()
    print(instance1.value)      --> 84
    local instance2 = Lion(10)
    print(instance2.value)      --> 20
invalid_constant (value, constants[, prefix="Invalid value: "])
Returns an error message for an invalid lookup constant. This function is used to generate error messages for invalid arguments.

Parameters:

  • value number or string The value that wasn't found.
  • constants table The valid values for the constant.
  • prefix string the prefix for the message. (default "Invalid value: ")

Returns:

    string The error message.
make_lookup ([value_type="value"], t)
Converts a lookup table to a constant table with user friendly error reporting. The constant table is modified in-place, a metatable with an __index metamethod is added to the table. This metamethod throws an error when an invalid key is accessed.

Parameters:

  • value_type string The type of value looked up, use a singular, eg. "cursor shape", or "foreground color". (default "value")
  • t table The lookup table.

Returns:

    table The same constant table t, with a metatable added.

Usage:

    local cursor_shape = M.make_lookup("cursor shape", {
      block = 0,
      underline = 1,
      bar = 2,
    })
    
    local value = cursor_shape["bad-shape"] -- throws an error;
    -- Invalid cursor shape: "bad-shape". Expected one of: "block", "underline", "bar"
resolve_index (index, max_value[, min_value=1])
Resolve indices. This function resolves negative indices to positive indices. The result will be capped into the range [min_value, max_value].

Parameters:

  • index number The index to resolve.
  • max_value number The maximum value for the index.
  • min_value number The minimum value for the index. (default 1)
throw_invalid_constant (value, constants[, prefix="Invalid value: "[, err_lvl=1]])
Throws an error message for an invalid lookup constant. This function is used to generate error messages for invalid arguments.

Parameters:

  • value number or string The value that wasn't found.
  • constants table The valid values for the constant.
  • prefix string the prefix for the message. (default "Invalid value: ")
  • err_lvl number the error level when throwing the error. (default 1)

Returns:

    nothing, throws an error.
utf8sub (str, i, j)
Like string:sub(), Returns the substring of the string that starts from i and go until j inclusive, but operators on utf8 characters Preserves utf8.len()

Parameters:

  • str string the string to take the substring of
  • i number the starting index of the substring
  • j number the ending index of the substring

Returns:

    string the substring
generated by LDoc 1.5.0