diff --git a/docs/docs/segment-os.md b/docs/docs/segment-os.md index 18a29394ed0f..25e43b64013f 100644 --- a/docs/docs/segment-os.md +++ b/docs/docs/segment-os.md @@ -30,6 +30,7 @@ Display OS specific info. Defaults to Icon. - windows: `string` - the icon to use for Windows - defaults to Windows icon - defaults to `\uE62A` - wsl: `string` - the string/icon to use for WSL - defaults to `WSL` - wsl_separator: `string` - the string to use for separating WSL from Linux - defaults to ` - ` +- display_distro_name: `boolean` - display the distro name or icon (for WSL and Linux) - defaults to `false` - alpine: `string` - the icon to use for Alpine - defaults to Alpine icon - defaults to `\uF300` - aosc: `string` - the icon to use for Aosc - defaults to Aosc icon - defaults to `\uF301` - arch: `string` - the icon to use for Arch - defaults to Arch icon - defaults to `\uF303` diff --git a/src/segment_os.go b/src/segment_os.go index 00afe790e552..eaeb822b3930 100644 --- a/src/segment_os.go +++ b/src/segment_os.go @@ -58,6 +58,8 @@ const ( Slackware Property = "slackware" // Ubuntu the string/icon to use for Ubuntu Ubuntu Property = "ubuntu" + // DisplayDistroName display the distro name or not + DisplayDistroName Property = "display_distro_name" ) func (n *osInfo) enabled() bool { @@ -74,20 +76,27 @@ func (n *osInfo) string() string { case "linux": wsl := n.env.getenv("WSL_DISTRO_NAME") p := n.env.getPlatform() - if wsl != "" { - return fmt.Sprintf("%s%s%s", - n.props.getString(WSL, "WSL"), - n.props.getString(WSLSeparator, " - "), - getLinuxIcon(n, p)) + if len(wsl) == 0 { + return n.getDistroName(p, "") } - return getLinuxIcon(n, p) + return fmt.Sprintf("%s%s%s", + n.props.getString(WSL, "WSL"), + n.props.getString(WSLSeparator, " - "), + n.getDistroName(p, wsl)) default: - return "" + return goos } } -func getLinuxIcon(n *osInfo, p string) string { - switch p { +func (n *osInfo) getDistroName(distro, defaultName string) string { + displayDistroName := n.props.getBool(DisplayDistroName, false) + if displayDistroName && len(defaultName) > 0 { + return defaultName + } + if displayDistroName { + return distro + } + switch distro { case "alpine": return n.props.getString(Alpine, "\uF300") case "aosc": diff --git a/src/segment_os_test.go b/src/segment_os_test.go index fe26546e0066..32a7d2ee1d48 100644 --- a/src/segment_os_test.go +++ b/src/segment_os_test.go @@ -6,39 +6,77 @@ import ( "github.com/stretchr/testify/assert" ) -func TestOsInfo(t *testing.T) { - env := new(MockedEnvironment) - env.On("getRuntimeGOOS", nil).Return("windows") - props := &properties{ - values: map[Property]interface{}{Windows: "win"}, - foreground: "#fff", - background: "#000", - } - osInfo := &osInfo{ - env: env, - props: props, - } - want := "win" - got := osInfo.string() - assert.Equal(t, want, got) -} - -func TestWSL(t *testing.T) { - env := new(MockedEnvironment) - env.On("getRuntimeGOOS", nil).Return("linux") - env.On("getenv", "WSL_DISTRO_NAME").Return("debian") - env.On("getPlatform", nil).Return("debian") - props := &properties{ - values: map[Property]interface{}{ - WSL: "WSL TEST", - WSLSeparator: " @ ", +func TestOSInfo(t *testing.T) { + cases := []struct { + Case string + ExpectedString string + GOOS string + WSLDistro string + Platform string + DisplayDistroName bool + }{ + { + Case: "WSL debian - icon", + ExpectedString: "WSL at \uf306", + GOOS: "linux", + WSLDistro: "debian", + Platform: "debian", + }, + { + Case: "WSL debian - name", + ExpectedString: "WSL at burps", + GOOS: "linux", + WSLDistro: "burps", + Platform: "debian", + DisplayDistroName: true, + }, + { + Case: "plain linux - icon", + ExpectedString: "\uf306", + GOOS: "linux", + Platform: "debian", + }, + { + Case: "plain linux - name", + ExpectedString: "debian", + GOOS: "linux", + Platform: "debian", + DisplayDistroName: true, + }, + { + Case: "windows", + ExpectedString: "windows", + GOOS: "windows", + }, + { + Case: "darwin", + ExpectedString: "darwin", + GOOS: "darwin", + }, + { + Case: "unknown", + ExpectedString: "unknown", + GOOS: "unknown", }, } - osInfo := &osInfo{ - env: env, - props: props, + for _, tc := range cases { + env := new(MockedEnvironment) + env.On("getRuntimeGOOS", nil).Return(tc.GOOS) + env.On("getenv", "WSL_DISTRO_NAME").Return(tc.WSLDistro) + env.On("getPlatform", nil).Return(tc.Platform) + props := &properties{ + values: map[Property]interface{}{ + WSL: "WSL", + WSLSeparator: " at ", + DisplayDistroName: tc.DisplayDistroName, + Windows: "windows", + MacOS: "darwin", + }, + } + osInfo := &osInfo{ + env: env, + props: props, + } + assert.Equal(t, tc.ExpectedString, osInfo.string(), tc.Case) } - want := "WSL TEST @ \uF306" - got := osInfo.string() - assert.Equal(t, want, got) } diff --git a/themes/schema.json b/themes/schema.json index c9891e1c41e5..8fa315ca2ac1 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -836,6 +836,12 @@ "description": "Icon/text to use for separating WSL from Linux", "default": " - " }, + "display_distro_name": { + "type": "boolean", + "title": "Display Distro Name", + "description": "Display the distro name or icon or not", + "default": false + }, "alpine": { "type": "string", "title": "Alpine Icon",