Skip to content

Commit

Permalink
feat(os): show/hide distro name
Browse files Browse the repository at this point in the history
resolves #443
  • Loading branch information
JanDeDobbeleer committed Feb 26, 2021
1 parent ae8e6d6 commit be1ec7b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 41 deletions.
1 change: 1 addition & 0 deletions docs/docs/segment-os.md
Expand Up @@ -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`
Expand Down
27 changes: 18 additions & 9 deletions src/segment_os.go
Expand Up @@ -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 {
Expand All @@ -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":
Expand Down
102 changes: 70 additions & 32 deletions src/segment_os_test.go
Expand Up @@ -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)
}
6 changes: 6 additions & 0 deletions themes/schema.json
Expand Up @@ -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",
Expand Down

0 comments on commit be1ec7b

Please sign in to comment.