finally add config stuff

This commit is contained in:
garret 2021-10-09 15:19:59 +01:00
parent 04a61982e8
commit 52f3a086f2
2 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,51 @@
---
title: simpleconf.lua's Fine Manual
lang: en-GB
...
# simpleconf.lua's Fine Manual
read it
## Usage
### Loading config files
Synopsis: `config = simpleconf.get_config([config_file, defaults])`
`@config_file` (`string`)
Path of the file to load.
`@defaults` (`table`)
A table containing your default settings.
`config` (table)
Contains config values.
Both values are optional.
If the file and the defaults are present, it loads the defaults, then overwrites their values with those of the file.
If the file is present, but not the defaults, it just loads the contents of the file.
If the file isn't present, but the defaults are, it just loads the defaults, and you're using it wrong.
If nothing is present, it returns an empty table.
| File | Defaults | Result |
| ---- | -------- | ------ |
| 1 | 1 | file overwrites defaults |
| 1 | 0 | just the file |
| 0 | 1 | just the defaults |
| 0 | 0 | nothing |
## Config file format
```
bool = true
number = 123
string = the quick brown fox jumps over the lazy dog
I am a comment!
```
(don't tell anyone, but it's all just a pattern nicked from Programming in Lua (page 82, 4th edition))

View File

@ -0,0 +1,46 @@
-- primitive config handler
local function tobool(value)
if value == "true" then
return true
elseif value == "false" then
return false
else
return nil
end
end
local function cast(value)
return tonumber(value) or tobool(value) or value
end
local function get_config(config_file, defaults)
local conf = defaults or {}
local ok, lines = pcall(io.lines, config_file)
if ok then
for line in lines do
local key, value = string.match(line, "(%a+)%s*=%s*(.+)")
conf[key] = cast(value)
end
end
return conf
end
local simpleconf = {get_config = get_config}
local have_depctrl, depctrl = pcall(require, "l0.DependencyControl")
if have_depctrl then
local version = depctrl{
name = "Simple (bad) Config",
version = "0.1.0",
description = "primitive config handler",
author = "garret",
url = "http://github.com/garret1317/aegisub-scripts",
moduleName = "garret.simpleconf"}
simpleconf.version = version
return version:register(simpleconf)
else
return simpleconf
end