Repository: JanDeDobbeleer/oh-my-posh
Pull request #684 (2021-04-23 21:59:57)

feat: add OS template property
### Prerequisites

- [x] I have read and understand 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

Your code looked really clean and easy to follow so I gave #683 a shot. The following example results in a nice [Debian rubine red](https://wiki.debian.org/DebianLogo#The_red_color) OS logo:

```jsonc
        {
          "type": "os",
          "style": "plain",
          "foreground": "#ffffff",
          "background_templates": [
            "{{if contains \"debian\" .OS}}#CE0056{{end}}",
            "#333333"
          ]
        },
```

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary


gitolicious: Potential extensions I could think of are
* a `.WSL` template property to define different colors for WSL and native consoles
* a `"color_background": true` configuration property which automatically sets pre-defined background colors for each OS
gitolicious: > @lnu that's solvable by using templates imho. Our go-to should be that, if the template can handle it, we don't extend the base properties. Ideally we can move 100% to templates for every segment one day.

A predefined  setting would save the users looking up the appropriate colors for the 20+ distros, though finding nice defaults would be quite some effort even for the repo maintainers.
JanDeDobbeleer: @gitolicious true, I'd like to meet the user who has them all set up though...

Patch:
diff --git a/docs/docs/segment-os.md b/docs/docs/segment-os.md
index 25e43b64..489b6171 100644
--- a/docs/docs/segment-os.md
+++ b/docs/docs/segment-os.md
@@ -50,3 +50,7 @@ Display OS specific info. Defaults to Icon.
 - sabayon: `string` - the icon to use for Sabayon - defaults to Sabayon icon - defaults to `\uF317`
 - slackware: `string` - the icon to use for Slackware - defaults to Slackware icon - defaults to `\uF319`
 - ubuntu: `string` - the icon to use for Ubuntu - defaults to Ubuntu icon - defaults to `\uF31b`
+
+## Template Properties
+
+- `.OS`: `string` - the OS platform
diff --git a/src/environment.go b/src/environment.go
index 9b1bb941..fc8e19d0 100644
--- a/src/environment.go
+++ b/src/environment.go
@@ -21,6 +21,8 @@ import (
 const (
 	unknown         = "unknown"
 	windowsPlatform = "windows"
+	darwinPlatform  = "darwin"
+	linuxPlatform   = "linux"
 )
 
 type commandError struct {
diff --git a/src/segment_os.go b/src/segment_os.go
index eaeb822b..5161a52c 100644
--- a/src/segment_os.go
+++ b/src/segment_os.go
@@ -7,6 +7,7 @@ import (
 type osInfo struct {
 	props *properties
 	env   environmentInfo
+	OS    string
 }
 
 const (
@@ -70,20 +71,25 @@ func (n *osInfo) string() string {
 	goos := n.env.getRuntimeGOOS()
 	switch goos {
 	case windowsPlatform:
+		n.OS = windowsPlatform
 		return n.props.getString(Windows, "\uE62A")
-	case "darwin":
+	case darwinPlatform:
+		n.OS = darwinPlatform
 		return n.props.getString(MacOS, "\uF179")
-	case "linux":
+	case linuxPlatform:
 		wsl := n.env.getenv("WSL_DISTRO_NAME")
 		p := n.env.getPlatform()
 		if len(wsl) == 0 {
+			n.OS = p
 			return n.getDistroName(p, "")
 		}
+		n.OS = wsl
 		return fmt.Sprintf("%s%s%s",
 			n.props.getString(WSL, "WSL"),
 			n.props.getString(WSLSeparator, " - "),
 			n.getDistroName(p, wsl))
 	default:
+		n.OS = goos
 		return goos
 	}
 }


Test patch:
diff --git a/src/segment_os_test.go b/src/segment_os_test.go
index 32a7d2ee..0cd54756 100644
--- a/src/segment_os_test.go
+++ b/src/segment_os_test.go
@@ -78,5 +78,12 @@ func TestOSInfo(t *testing.T) {
 			props: props,
 		}
 		assert.Equal(t, tc.ExpectedString, osInfo.string(), tc.Case)
+		if tc.WSLDistro != "" {
+			assert.Equal(t, tc.WSLDistro, osInfo.OS, tc.Case)
+		} else if tc.Platform != "" {
+			assert.Equal(t, tc.Platform, osInfo.OS, tc.Case)
+		} else {
+			assert.Equal(t, tc.GOOS, osInfo.OS, tc.Case)
+		}
 	}
 }