diff options
author | Robin Jarry <robin@jarry.cc> | 2023-02-21 16:18:54 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-03-02 23:56:13 +0100 |
commit | b63c93563c622e70cda7006c1816dc6b59e75844 (patch) | |
tree | 821bd7636cf676640e7b391d8ce015bb33f2843e /config/templates.go | |
parent | d9a8edd8e9269aa1189d55c8d13caa05084435f5 (diff) | |
download | aerc-b63c93563c622e70cda7006c1816dc6b59e75844.tar.gz |
config: use reflection to map ini keys to struct fields
The default ini.Section.MapTo() function only handles basic types.
Implement a more complete mapping solution that allows:
* parsing templates, regexps, email addresses
* defining a custom parsing method via the `parse:"MethodName"` tag
* defining default values via the `default:"value"` tag
* parsing rune values with the `type:"rune"` tag
The field name must be specified in the `ini:"field-name"` tag as it was
before. It is no longer optional.
The `delim:"<separator>"` tag remains but can only be used to parse
string arrays.
It is now possible to override default values with "zero" values. For
example:
[ui]
dirlist-delay = 0
Will override the default "200ms" value. Also:
[statusline]
status-columns =
Will override the default "left<*,center>=,right>*" value.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'config/templates.go')
-rw-r--r-- | config/templates.go | 28 |
1 files changed, 6 insertions, 22 deletions
diff --git a/config/templates.go b/config/templates.go index f618d365..d3e7cfb0 100644 --- a/config/templates.go +++ b/config/templates.go @@ -2,7 +2,6 @@ package config import ( "path" - "strings" "time" "git.sr.ht/~rjarry/aerc/lib/templates" @@ -13,31 +12,16 @@ import ( type TemplateConfig struct { TemplateDirs []string `ini:"template-dirs" delim:":"` - NewMessage string `ini:"new-message"` - QuotedReply string `ini:"quoted-reply"` - Forwards string `ini:"forwards"` + NewMessage string `ini:"new-message" default:"new_message"` + QuotedReply string `ini:"quoted-reply" default:"quoted_reply"` + Forwards string `ini:"forwards" default:"forward_as_body"` } -func defaultTemplatesConfig() *TemplateConfig { - return &TemplateConfig{ - TemplateDirs: []string{}, - NewMessage: "new_message", - QuotedReply: "quoted_reply", - Forwards: "forward_as_body", - } -} - -var Templates = defaultTemplatesConfig() +var Templates = new(TemplateConfig) func parseTemplates(file *ini.File) error { - if templatesSec, err := file.GetSection("templates"); err == nil { - if err := templatesSec.MapTo(&Templates); err != nil { - return err - } - templateDirs := templatesSec.Key("template-dirs").String() - if templateDirs != "" { - Templates.TemplateDirs = strings.Split(templateDirs, ":") - } + if err := MapToStruct(file.Section("templates"), Templates, true); err != nil { + return err } // append default paths to template-dirs |