Repository: JanDeDobbeleer/oh-my-posh
Pull request #1306 (2021-11-25 08:26:31)

fix(winreg): fallback value on error or empty
### Prerequisites

- [x] I have read and understood the `CONTRIBUTING` guide
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added / updated (for bug fixes/features)

### Description

Allow a fallback value when using the winreg segment.

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/docs/docs/segment-winreg.md b/docs/docs/segment-winreg.md
index 64d6630f..a7a3f4c3 100644
--- a/docs/docs/segment-winreg.md
+++ b/docs/docs/segment-winreg.md
@@ -34,7 +34,8 @@ Supported registry key types:
 ## Properties
 
 - path: `string` - registry path to the desired key using backslashes and with a valid root HKEY name.
-- key: `string` - the key to read from the `path location.  If `""`, will read the default value.
+- key: `string` - the key to read from the `path` location.
+- fallback: `string` - the value to fall back to if no entry is found
 - template: `string` - a go [text/template][go-text-template] template extended
   with [sprig][sprig] utilizing the properties below.
 
diff --git a/src/segment_winreg.go b/src/segment_winreg.go
index adafcdea..b2ebe713 100644
--- a/src/segment_winreg.go
+++ b/src/segment_winreg.go
@@ -12,6 +12,8 @@ const (
 	RegistryPath Property = "path"
 	// key within full reg path formed from two above
 	RegistryKey Property = "key"
+	// Fallback is the text to display if the key is not found
+	Fallback Property = "fallback"
 )
 
 func (wr *winreg) init(props *properties, env environmentInfo) {
@@ -26,10 +28,15 @@ func (wr *winreg) enabled() bool {
 
 	registryPath := wr.props.getString(RegistryPath, "")
 	registryKey := wr.props.getString(RegistryKey, "")
+	fallback := wr.props.getString(Fallback, "")
 
 	var err error
 	wr.Value, err = wr.env.getWindowsRegistryKeyValue(registryPath, registryKey)
-	return err == nil
+	if len(fallback) > 0 && (err != nil || len(wr.Value) == 0) {
+		wr.Value = fallback
+		return true
+	}
+	return err == nil && len(wr.Value) > 0
 }
 
 func (wr *winreg) string() string {


Test patch:
diff --git a/src/segment_winreg_test.go b/src/segment_winreg_test.go
index 85219d99..7021f5a9 100644
--- a/src/segment_winreg_test.go
+++ b/src/segment_winreg_test.go
@@ -12,7 +12,9 @@ func TestRegQueryEnabled(t *testing.T) {
 		CaseDescription string
 		Path            string
 		Key             string
+		Fallback        string
 		ExpectedSuccess bool
+		ExpectedValue   string
 		Output          string
 		Err             error
 	}{
@@ -20,15 +22,43 @@ func TestRegQueryEnabled(t *testing.T) {
 			CaseDescription: "Error",
 			Path:            "HKLLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
 			Key:             "ProductName",
-			ExpectedSuccess: false,
 			Err:             errors.New("No match"),
+			ExpectedSuccess: false,
 		},
 		{
 			CaseDescription: "Value",
 			Path:            "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
 			Key:             "InstallTime",
+			Output:          "xbox",
 			ExpectedSuccess: true,
+			ExpectedValue:   "xbox",
+		},
+		{
+			CaseDescription: "Fallback value",
+			Path:            "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
+			Key:             "InstallTime",
 			Output:          "no formatter",
+			Fallback:        "cortana",
+			Err:             errors.New("No match"),
+			ExpectedSuccess: true,
+			ExpectedValue:   "cortana",
+		},
+		{
+			CaseDescription: "Fallback value on empty",
+			Path:            "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
+			Key:             "InstallTime",
+			Output:          "",
+			Fallback:        "anaconda",
+			ExpectedSuccess: true,
+			ExpectedValue:   "anaconda",
+		},
+		{
+			CaseDescription: "Empty no fallback disabled",
+			Path:            "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
+			Key:             "InstallTime",
+			Output:          "",
+			ExpectedSuccess: false,
+			ExpectedValue:   "",
 		},
 	}
 
@@ -40,6 +70,7 @@ func TestRegQueryEnabled(t *testing.T) {
 			values: map[Property]interface{}{
 				RegistryPath: tc.Path,
 				RegistryKey:  tc.Key,
+				Fallback:     tc.Fallback,
 			},
 		}
 		r := &winreg{
@@ -47,6 +78,7 @@ func TestRegQueryEnabled(t *testing.T) {
 			props: props,
 		}
 
-		assert.Equal(t, r.enabled(), tc.ExpectedSuccess, tc.CaseDescription)
+		assert.Equal(t, tc.ExpectedSuccess, r.enabled(), tc.CaseDescription)
+		assert.Equal(t, tc.ExpectedValue, r.string(), tc.CaseDescription)
 	}
 }