feat: add wsl capability to os segment

This commit is contained in:
Nathan Wykes 2020-10-21 20:49:14 -06:00 committed by Jan De Dobbeleer
parent 1c6944b1db
commit 9ccf376d61
7 changed files with 165 additions and 1 deletions

View file

@ -28,3 +28,24 @@ Display OS specific info. Defaults to Icon.
- macos: `string` - the string to use for macOS - defaults to macOS icon - defaults to `\uF179` - macos: `string` - the string to use for macOS - defaults to macOS icon - defaults to `\uF179`
- linux: `string` - the icon to use for Linux - defaults to Linux icon - defaults to `\uF17C` - linux: `string` - the icon to use for Linux - defaults to Linux icon - defaults to `\uF17C`
- windows: `string` - the icon to use for Windows - defaults to Windows icon - defaults to `\uE62A` - 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 ` - `
- 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`
- centos: `string` - the icon to use for Centos - defaults to Centos icon - defaults to `\uF304`
- coreos: `string` - the icon to use for Coreos - defaults to Coreos icon - defaults to `\uF305`
- debian: `string` - the icon to use for Debian - defaults to Debian icon - defaults to `\uF306`
- devuan: `string` - the icon to use for Devuan - defaults to Devuan icon - defaults to `\uF307`
- raspbian: `string` - the icon to use for Raspbian - defaults to Raspbian icon - defaults to `\uF315`
- elementary: `string` - the icon to use for Elementary - defaults to Elementary icon - defaults to `\uF309`
- fedora: `string` - the icon to use for Fedora - defaults to Fedora icon - defaults to `\uF30a`
- gentoo: `string` - the icon to use for Gentoo - defaults to Gentoo icon - defaults to `\uF30d`
- mageia: `string` - the icon to use for Mageia - defaults to Mageia icon - defaults to `\uF310`
- manjaro: `string` - the icon to use for Manjaro - defaults to Manjaro icon - defaults to `\uF312`
- mint: `string` - the icon to use for Mint - defaults to Mint icon - defaults to `\uF30e`
- nixos: `string` - the icon to use for Nixos - defaults to Nixos icon - defaults to `\uF313`
- opensuse: `string` - the icon to use for Opensuse - defaults to Opensuse icon - defaults to `\uF314`
- 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`

11
environment.go Executable file → Normal file
View file

@ -13,6 +13,7 @@ import (
"github.com/distatus/battery" "github.com/distatus/battery"
ps "github.com/mitchellh/go-ps" ps "github.com/mitchellh/go-ps"
"github.com/shirou/gopsutil/host"
) )
type environmentInfo interface { type environmentInfo interface {
@ -27,6 +28,7 @@ type environmentInfo interface {
isRunningAsRoot() bool isRunningAsRoot() bool
getHostName() (string, error) getHostName() (string, error)
getRuntimeGOOS() string getRuntimeGOOS() string
getPlatform() string
hasCommand(command string) bool hasCommand(command string) bool
runCommand(command string, args ...string) (string, error) runCommand(command string, args ...string) (string, error)
runShellCommand(shell string, command string) string runShellCommand(shell string, command string) string
@ -120,6 +122,15 @@ func (env *environment) getRuntimeGOOS() string {
return runtime.GOOS return runtime.GOOS
} }
func (env *environment) getPlatform() string {
if runtime.GOOS == "windows" {
return "windows"
}
p, _, _, _ := host.PlatformInformation()
return p
}
func (env *environment) runCommand(command string, args ...string) (string, error) { func (env *environment) runCommand(command string, args ...string) (string, error) {
out, err := exec.Command(command, args...).Output() out, err := exec.Command(command, args...).Output()

3
go.mod
View file

@ -3,9 +3,12 @@ module github.com/jandedobbeleer/oh-my-posh3
go 1.15 go 1.15
require ( require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/distatus/battery v0.10.1-0.20200722221337-7e1bf2bbb15c github.com/distatus/battery v0.10.1-0.20200722221337-7e1bf2bbb15c
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/gookit/color v1.3.1 github.com/gookit/color v1.3.1
github.com/mitchellh/go-ps v1.0.0 github.com/mitchellh/go-ps v1.0.0
github.com/shirou/gopsutil v2.20.9+incompatible
github.com/stretchr/objx v0.3.0 // indirect github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.6.1 github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211

6
go.sum
View file

@ -4,9 +4,13 @@ github.com/JanDeDobbeleer/color v1.3.1-0.20201014085303-5ffcdf66388a h1:0gU8YI1Z
github.com/JanDeDobbeleer/color v1.3.1-0.20201014085303-5ffcdf66388a/go.mod h1:R3ogXq2B9rTbXoSHJ1HyUVAZ3poOJHpd9nQmyGZsfvQ= github.com/JanDeDobbeleer/color v1.3.1-0.20201014085303-5ffcdf66388a/go.mod h1:R3ogXq2B9rTbXoSHJ1HyUVAZ3poOJHpd9nQmyGZsfvQ=
github.com/JanDeDobbeleer/go-ps v1.0.0 h1:3nqwUNAIzjrjQoEdZDVcS5OCm+KIoShKnANJ5xfkgBY= github.com/JanDeDobbeleer/go-ps v1.0.0 h1:3nqwUNAIzjrjQoEdZDVcS5OCm+KIoShKnANJ5xfkgBY=
github.com/JanDeDobbeleer/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg= github.com/JanDeDobbeleer/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@ -15,6 +19,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shirou/gopsutil v2.20.9+incompatible h1:msXs2frUV+O/JLva9EDLpuJ84PrFsdCTCQex8PUdtkQ=
github.com/shirou/gopsutil v2.20.9+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As= github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=

View file

@ -1,5 +1,9 @@
package main package main
import (
"fmt"
)
type osInfo struct { type osInfo struct {
props *properties props *properties
env environmentInfo env environmentInfo
@ -12,6 +16,48 @@ const (
Linux Property = "linux" Linux Property = "linux"
//Windows the string/icon to use for windows //Windows the string/icon to use for windows
Windows Property = "windows" Windows Property = "windows"
//WSL the string/icon to use for WSL
WSL Property = "wsl"
//WSLSeparator shows between WSL and Linux properties when WSL is detected
WSLSeparator Property = "wsl_separator"
//Alpine the string/icon to use for Alpine
Alpine Property = "alpine"
//Aosc the string/icon to use for Aosc
Aosc Property = "aosc"
//Arch the string/icon to use for Arch
Arch Property = "arch"
//Centos the string/icon to use for Centos
Centos Property = "centos"
//Coreos the string/icon to use for Coreos
Coreos Property = "coreos"
//Debian the string/icon to use for Debian
Debian Property = "debian"
//Devuan the string/icon to use for Devuan
Devuan Property = "devuan"
//Raspbian the string/icon to use for Raspbian
Raspbian Property = "raspbian"
//Elementary the string/icon to use for Elementary
Elementary Property = "elementary"
//Fedora the string/icon to use for Fedora
Fedora Property = "fedora"
//Gentoo the string/icon to use for Gentoo
Gentoo Property = "gentoo"
//Mageia the string/icon to use for Mageia
Mageia Property = "mageia"
//Manjaro the string/icon to use for Manjaro
Manjaro Property = "manjaro"
//Mint the string/icon to use for Mint
Mint Property = "mint"
//Nixos the string/icon to use for Nixos
Nixos Property = "nixos"
//Opensuse the string/icon to use for Opensuse
Opensuse Property = "opensuse"
//Sabayon the string/icon to use for Sabayon
Sabayon Property = "sabayon"
//Slackware the string/icon to use for Slackware
Slackware Property = "slackware"
//Ubuntu the string/icon to use for Ubuntu
Ubuntu Property = "ubuntu"
) )
func (n *osInfo) enabled() bool { func (n *osInfo) enabled() bool {
@ -26,12 +72,64 @@ func (n *osInfo) string() string {
case "darwin": case "darwin":
return n.props.getString(MacOS, "\uF179") return n.props.getString(MacOS, "\uF179")
case "linux": case "linux":
return n.props.getString(Linux, "\uF17C") 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))
}
return getLinuxIcon(n, p)
default: default:
return "" return ""
} }
} }
func getLinuxIcon(n *osInfo, p string) string {
switch p {
case "alpine":
return n.props.getString(Alpine, "\uF300")
case "aosc":
return n.props.getString(Aosc, "\uF301")
case "arch":
return n.props.getString(Arch, "\uF303")
case "centos":
return n.props.getString(Centos, "\uF304")
case "coreos":
return n.props.getString(Coreos, "\uF305")
case "debian":
return n.props.getString(Debian, "\uF306")
case "devuan":
return n.props.getString(Devuan, "\uF307")
case "raspbian":
return n.props.getString(Raspbian, "\uF315")
case "elementary":
return n.props.getString(Elementary, "\uF309")
case "fedora":
return n.props.getString(Fedora, "\uF30a")
case "gentoo":
return n.props.getString(Gentoo, "\uF30d")
case "mageia":
return n.props.getString(Mageia, "\uF310")
case "manjaro":
return n.props.getString(Manjaro, "\uF312")
case "mint":
return n.props.getString(Mint, "\uF30e")
case "nixos":
return n.props.getString(Nixos, "\uF313")
case "opensuse":
return n.props.getString(Opensuse, "\uF314")
case "sabayon":
return n.props.getString(Sabayon, "\uF317")
case "slackware":
return n.props.getString(Slackware, "\uF319")
case "ubuntu":
return n.props.getString(Ubuntu, "\uF31b")
}
return n.props.getString(Linux, "\uF17C")
}
func (n *osInfo) init(props *properties, env environmentInfo) { func (n *osInfo) init(props *properties, env environmentInfo) {
n.props = props n.props = props
n.env = env n.env = env

View file

@ -22,3 +22,23 @@ func TestOsInfo(t *testing.T) {
got := osInfo.string() got := osInfo.string()
assert.Equal(t, want, got) 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: " @ ",
},
}
osInfo := &osInfo{
env: env,
props: props,
}
want := "WSL TEST @ \uF306"
got := osInfo.string()
assert.Equal(t, want, got)
}

View file

@ -63,6 +63,11 @@ func (env *MockedEnvironment) getRuntimeGOOS() string {
return args.String(0) return args.String(0)
} }
func (env *MockedEnvironment) getPlatform() string {
args := env.Called(nil)
return args.String(0)
}
func (env *MockedEnvironment) hasCommand(command string) bool { func (env *MockedEnvironment) hasCommand(command string) bool {
args := env.Called(command) args := env.Called(command)
return args.Bool(0) return args.Bool(0)