feat(os): show/hide distro name

resolves #443
This commit is contained in:
Jan De Dobbeleer 2021-02-26 06:51:03 +01:00 committed by Jan De Dobbeleer
parent ae8e6d6ccb
commit be1ec7bea3
4 changed files with 95 additions and 41 deletions

View file

@ -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`

View file

@ -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 != "" {
if len(wsl) == 0 {
return n.getDistroName(p, "")
}
return fmt.Sprintf("%s%s%s",
n.props.getString(WSL, "WSL"),
n.props.getString(WSLSeparator, " - "),
getLinuxIcon(n, p))
}
return getLinuxIcon(n, p)
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":

View file

@ -6,39 +6,77 @@ import (
"github.com/stretchr/testify/assert"
)
func TestOsInfo(t *testing.T) {
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",
},
}
for _, tc := range cases {
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")
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 TEST",
WSLSeparator: " @ ",
WSL: "WSL",
WSLSeparator: " at ",
DisplayDistroName: tc.DisplayDistroName,
Windows: "windows",
MacOS: "darwin",
},
}
osInfo := &osInfo{
env: env,
props: props,
}
want := "WSL TEST @ \uF306"
got := osInfo.string()
assert.Equal(t, want, got)
assert.Equal(t, tc.ExpectedString, osInfo.string(), tc.Case)
}
}

View file

@ -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",