From 1aa4fb9bff14e6857362f22e668369a47e43bc24 Mon Sep 17 00:00:00 2001
From: Robin Jarry <robin@jarry.cc>
Date: Sun, 4 Sep 2022 22:04:40 +0200
Subject: [PATCH] config: move ui config parse in function

Avoid repetition. Also, commit de24d2d5909a ("config: fix setting of
zero-value time.Duration config values") did not apply to contextual ui
config sections. Fix that.

Fixes: de24d2d5909a ("config: fix setting of zero-value time.Duration config values")
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
---
 config/config.go | 82 +++++++++++++++++++++++-------------------------
 1 file changed, 39 insertions(+), 43 deletions(-)

diff --git a/config/config.go b/config/config.go
index ead5456..4d4bfe8 100644
--- a/config/config.go
+++ b/config/config.go
@@ -499,31 +499,9 @@ func (config *AercConfig) LoadConfig(file *ini.File) error {
 	}
 
 	if ui, err := file.GetSection("ui"); err == nil {
-		if err := ui.MapTo(&config.Ui); err != nil {
+		if err := parseUiConfig(ui, &config.Ui); err != nil {
 			return err
 		}
-		if err := validateBorderChars(ui, &config.Ui); err != nil {
-			return err
-		}
-		// Values with type=time.Duration must be explicitly set. If these
-		// values are given a default in the struct passed to ui.MapTo, which
-		// they are, a zero-value in the config won't overwrite the default.
-		for key, val := range ui.KeysHash() {
-			switch key {
-			case "dirlist-delay":
-				dur, err := time.ParseDuration(val)
-				if err != nil {
-					return err
-				}
-				config.Ui.DirListDelay = dur
-			case "completion-delay":
-				dur, err := time.ParseDuration(val)
-				if err != nil {
-					return err
-				}
-				config.Ui.CompletionDelay = dur
-			}
-		}
 	}
 
 	for _, sectionName := range file.SectionStrings() {
@@ -536,10 +514,7 @@ func (config *AercConfig) LoadConfig(file *ini.File) error {
 			return err
 		}
 		uiSubConfig := UIConfig{}
-		if err := uiSection.MapTo(&uiSubConfig); err != nil {
-			return err
-		}
-		if err := validateBorderChars(uiSection, &uiSubConfig); err != nil {
+		if err := parseUiConfig(uiSection, &uiSubConfig); err != nil {
 			return err
 		}
 		contextualUi := UIConfigContext{
@@ -641,23 +616,44 @@ func (config *AercConfig) LoadConfig(file *ini.File) error {
 	return nil
 }
 
-func validateBorderChars(section *ini.Section, config *UIConfig) error {
-	for key, val := range section.KeysHash() {
-		switch key {
-		case "border-char-vertical":
-			char := []rune(val)
-			if len(char) != 1 {
-				return fmt.Errorf("%v must be one and only one character", key)
-			}
-			config.BorderCharVertical = char[0]
-		case "border-char-horizontal":
-			char := []rune(val)
-			if len(char) != 1 {
-				return fmt.Errorf("%v must be one and only one character", key)
-			}
-			config.BorderCharHorizontal = char[0]
-		}
+func parseUiConfig(section *ini.Section, config *UIConfig) error {
+	if err := section.MapTo(config); err != nil {
+		return err
 	}
+
+	if key, err := section.GetKey("border-char-vertical"); err == nil {
+		chars := []rune(key.String())
+		if len(chars) != 1 {
+			return fmt.Errorf("%v must be one and only one character", key)
+		}
+		config.BorderCharVertical = chars[0]
+	}
+	if key, err := section.GetKey("border-char-horizontal"); err == nil {
+		chars := []rune(key.String())
+		if len(chars) != 1 {
+			return fmt.Errorf("%v must be one and only one character", key)
+		}
+		config.BorderCharHorizontal = chars[0]
+	}
+
+	// Values with type=time.Duration must be explicitly set. If these
+	// values are given a default in the struct passed to ui.MapTo, which
+	// they are, a zero-value in the config won't overwrite the default.
+	if key, err := section.GetKey("dirlist-delay"); err == nil {
+		dur, err := key.Duration()
+		if err != nil {
+			return err
+		}
+		config.DirListDelay = dur
+	}
+	if key, err := section.GetKey("completion-delay"); err == nil {
+		dur, err := key.Duration()
+		if err != nil {
+			return err
+		}
+		config.CompletionDelay = dur
+	}
+
 	return nil
 }