Module luasettings

This module handles generic settings as well as KOReader's global settings system.

Functions

LuaSettings:open (file_path) Opens a settings file.
LuaSettings:child (key) Reads child settings.
LuaSettings:readSetting (key, default) Reads a setting, optionally initializing it to a default.
LuaSettings:saveSetting (key, value) Saves a setting.
LuaSettings:delSetting (key) Deletes a setting.
LuaSettings:has (key) Checks if setting exists.
LuaSettings:hasNot (key) Checks if setting does not exist.
LuaSettings:isTrue (key) Checks if setting is true (boolean).
LuaSettings:isFalse (key) Checks if setting is false (boolean).
LuaSettings:nilOrTrue (key) Checks if setting is nil or true.
LuaSettings:nilOrFalse (key) Checks if setting is nil or false.
LuaSettings:flipNilOrTrue (key) Flips nil or true to false, and false to nil.
LuaSettings:flipNilOrFalse (key) Flips nil or false to true, and true to nil.
LuaSettings:flipTrue (key) Flips a setting between true and nil.
LuaSettings:flipFalse (key) Flips a setting between false and nil.
LuaSettings:toggle (key) Toggles a boolean setting
LuaSettings:addTableItem (key, value) Adds item to table.
LuaSettings:removeTableItem (key, index) Removes index from table.
LuaSettings:reset (table) Replaces existing settings with table.
LuaSettings:flush () Writes settings to disk.
LuaSettings:close () Closes settings file.
LuaSettings:purge () Purges settings file.


Functions

LuaSettings:open (file_path)
Opens a settings file.

Parameters:

  • file_path
LuaSettings:child (key)
Reads child settings.

Parameters:

  • key

Usage:

    Settings:saveSetting("key", {
        a = "b",
        c = true,
        d = false,
    })
    
    local child = Settings:child("key")
    
    child:readSetting("a")
    -- result "b"
LuaSettings:readSetting (key, default)
Reads a setting, optionally initializing it to a default.

If default is provided, and the key doesn't exist yet, it is initialized to default first. This ensures both that the defaults are actually set if necessary, and that the returned reference actually belongs to the LuaSettings object straight away, without requiring further interaction (e.g., saveSetting) from the caller.

This is mainly useful if the data type you want to retrieve/store is assigned/returned/passed by reference (e.g., a table), and you never actually break that reference by assigning another one to the same variable, (by e.g., assigning it a new object). c.f., https://www.lua.org/manual/5.1/manual.html#2.2.

Parameters:

  • key The setting's key
  • default Initialization data (Optional)
LuaSettings:saveSetting (key, value)
Saves a setting.

Parameters:

  • key
  • value
LuaSettings:delSetting (key)
Deletes a setting.

Parameters:

  • key
LuaSettings:has (key)
Checks if setting exists.

Parameters:

  • key
LuaSettings:hasNot (key)
Checks if setting does not exist.

Parameters:

  • key
LuaSettings:isTrue (key)
Checks if setting is true (boolean).

Parameters:

  • key
LuaSettings:isFalse (key)
Checks if setting is false (boolean).

Parameters:

  • key
LuaSettings:nilOrTrue (key)
Checks if setting is nil or true.

Parameters:

  • key
LuaSettings:nilOrFalse (key)
Checks if setting is nil or false.

Parameters:

  • key
LuaSettings:flipNilOrTrue (key)
Flips nil or true to false, and false to nil. e.g., a setting that defaults to true.

Parameters:

  • key
LuaSettings:flipNilOrFalse (key)
Flips nil or false to true, and true to nil. e.g., a setting that defaults to false.

Parameters:

  • key
LuaSettings:flipTrue (key)
Flips a setting between true and nil.

Parameters:

  • key
LuaSettings:flipFalse (key)
Flips a setting between false and nil.

Parameters:

  • key
LuaSettings:toggle (key)
Toggles a boolean setting

Parameters:

  • key
LuaSettings:addTableItem (key, value)
Adds item to table.

Parameters:

  • key
  • value
LuaSettings:removeTableItem (key, index)
Removes index from table.

Parameters:

  • key
  • index
LuaSettings:reset (table)
Replaces existing settings with table.

Parameters:

  • table
LuaSettings:flush ()
Writes settings to disk.
LuaSettings:close ()
Closes settings file.
LuaSettings:purge ()
Purges settings file.
generated by LDoc 1.5.0 Last updated 2024-11-21 07:57:34