stutter
a shared Vile configuration reader for V applications.
TL;DR
a config is an ordinary Vile program:
gap := 4
font := "mono"
enabled := #t
names := ["one", "two"]
read it at startup, including from a global initializer:
@[has_globals]
module main
import stutter
__global config &stutter.Reader = stutter.Config{
name: 'demo'
required: true
}.read()
give the bindings a V type with decode:
struct Settings {
gap int = 8
font string
enabled bool
names []string
}
fn main() {
settings := config.decode(Settings{}) or { panic(err) }
println(settings.gap) // 4 from the file, or 8 if the binding is absent
}
with stutter and vile on your V module path, put both V snippets in main.v, save the config at the path below, and run v run main.v.
file paths
stutter selects the first applicable path:
path, when your application supplies an explicit file.default_path, when your application supplies its own default location.$XDG_CONFIG_HOME/<name>/config.vile, or~/.config/<name>/config.vilewhenXDG_CONFIG_HOMEis empty or unset.
for the example above, save the Vile program in ~/.config/demo/config.vile if you use the standard location. an explicit path or required: true makes a missing file an error. read() prints load errors to stderr and exits with status 1. use read_checked()! to handle the error in your application.
defaults and overrides
use defaults for settings embedded in the application. the default mode, .fallback, runs them only when the optional file is absent or skip_file is true. use .overlay to run them before the file so the user can refer to them or override individual bindings.
reader := stutter.Config{
name: 'demo'
defaults: 'base_gap := 4'
defaults_mode: .overlay
evals: ['gap := base_gap * 2']
}.read_checked()!
evals run in order after the defaults and file, in the same scope. here, a user file containing base_gap := 6 gives a final gap of 12. an error in the file stops the load.
reading values
reader.decode(Settings{})! matches bindings to fields and keeps the supplied struct's values for missing bindings. for a field named border_width, the reader looks for border-width first, then border_width. supported field types are string, bool, int, f64, and []string. a binding with an incompatible type produces a decode error.
to read one binding directly, use reader.value('gap')!.as_integer()!. to evaluate a string on its own, use stutter.Config{ name: 'demo' }.parse(source)!. inline programs use Vile by default, or Scheme with syntax: .scheme. files use Vile for the .vile extension and Scheme for other extensions.