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/compose.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/compose.go')
-rw-r--r-- | config/compose.go | 54 |
1 files changed, 14 insertions, 40 deletions
diff --git a/config/compose.go b/config/compose.go index 14a3087b..70533453 100644 --- a/config/compose.go +++ b/config/compose.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "regexp" "git.sr.ht/~rjarry/aerc/log" @@ -10,54 +9,29 @@ import ( type ComposeConfig struct { Editor string `ini:"editor"` - HeaderLayout [][]string `ini:"-"` + HeaderLayout [][]string `ini:"header-layout" parse:"ParseLayout" default:"To|From,Subject"` AddressBookCmd string `ini:"address-book-cmd"` - ReplyToSelf bool `ini:"reply-to-self"` - NoAttachmentWarning *regexp.Regexp `ini:"-"` + ReplyToSelf bool `ini:"reply-to-self" default:"true"` + NoAttachmentWarning *regexp.Regexp `ini:"no-attachment-warning" parse:"ParseNoAttachmentWarning"` FilePickerCmd string `ini:"file-picker-cmd"` FormatFlowed bool `ini:"format-flowed"` } -func defaultComposeConfig() *ComposeConfig { - return &ComposeConfig{ - HeaderLayout: [][]string{ - {"To", "From"}, - {"Subject"}, - }, - ReplyToSelf: true, - } -} - -var Compose = defaultComposeConfig() +var Compose = new(ComposeConfig) func parseCompose(file *ini.File) error { - compose, err := file.GetSection("compose") - if err != nil { - goto end - } - - if err := compose.MapTo(&Compose); err != nil { + if err := MapToStruct(file.Section("compose"), Compose, true); err != nil { return err } - for key, val := range compose.KeysHash() { - if key == "header-layout" { - Compose.HeaderLayout = parseLayout(val) - } - - if key == "no-attachment-warning" && len(val) > 0 { - re, err := regexp.Compile("(?im)" + val) - if err != nil { - return fmt.Errorf( - "Invalid no-attachment-warning '%s': %w", - val, err, - ) - } - - Compose.NoAttachmentWarning = re - } - } - -end: log.Debugf("aerc.conf: [compose] %#v", Compose) return nil } + +func (c *ComposeConfig) ParseLayout(sec *ini.Section, key *ini.Key) ([][]string, error) { + layout := parseLayout(key.String()) + return layout, nil +} + +func (c *ComposeConfig) ParseNoAttachmentWarning(sec *ini.Section, key *ini.Key) (*regexp.Regexp, error) { + return regexp.Compile(`(?im)` + key.String()) +} |