Skip to content

Commit 52b66d1

Browse files
authored
feat: add OS template property
1 parent 608b486 commit 52b66d1

4 files changed

Lines changed: 21 additions & 2 deletions

File tree

docs/docs/segment-os.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ Display OS specific info. Defaults to Icon.
5050
- sabayon: `string` - the icon to use for Sabayon - defaults to Sabayon icon - defaults to `\uF317`
5151
- slackware: `string` - the icon to use for Slackware - defaults to Slackware icon - defaults to `\uF319`
5252
- ubuntu: `string` - the icon to use for Ubuntu - defaults to Ubuntu icon - defaults to `\uF31b`
53+
54+
## Template Properties
55+
56+
- `.OS`: `string` - the OS platform

src/environment.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
const (
2222
unknown = "unknown"
2323
windowsPlatform = "windows"
24+
darwinPlatform = "darwin"
25+
linuxPlatform = "linux"
2426
)
2527

2628
type commandError struct {

src/segment_os.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
type osInfo struct {
88
props *properties
99
env environmentInfo
10+
OS string
1011
}
1112

1213
const (
@@ -70,20 +71,25 @@ func (n *osInfo) string() string {
7071
goos := n.env.getRuntimeGOOS()
7172
switch goos {
7273
case windowsPlatform:
74+
n.OS = windowsPlatform
7375
return n.props.getString(Windows, "\uE62A")
74-
case "darwin":
76+
case darwinPlatform:
77+
n.OS = darwinPlatform
7578
return n.props.getString(MacOS, "\uF179")
76-
case "linux":
79+
case linuxPlatform:
7780
wsl := n.env.getenv("WSL_DISTRO_NAME")
7881
p := n.env.getPlatform()
7982
if len(wsl) == 0 {
83+
n.OS = p
8084
return n.getDistroName(p, "")
8185
}
86+
n.OS = wsl
8287
return fmt.Sprintf("%s%s%s",
8388
n.props.getString(WSL, "WSL"),
8489
n.props.getString(WSLSeparator, " - "),
8590
n.getDistroName(p, wsl))
8691
default:
92+
n.OS = goos
8793
return goos
8894
}
8995
}

src/segment_os_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,12 @@ func TestOSInfo(t *testing.T) {
7878
props: props,
7979
}
8080
assert.Equal(t, tc.ExpectedString, osInfo.string(), tc.Case)
81+
if tc.WSLDistro != "" {
82+
assert.Equal(t, tc.WSLDistro, osInfo.OS, tc.Case)
83+
} else if tc.Platform != "" {
84+
assert.Equal(t, tc.Platform, osInfo.OS, tc.Case)
85+
} else {
86+
assert.Equal(t, tc.GOOS, osInfo.OS, tc.Case)
87+
}
8188
}
8289
}

0 commit comments

Comments
 (0)