mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-31 13:57:26 -08:00
feat: add OS to template functionality
This commit is contained in:
parent
0404976b5c
commit
a200ee73ff
|
@ -90,6 +90,7 @@ type wifiInfo struct {
|
||||||
|
|
||||||
type Environment interface {
|
type Environment interface {
|
||||||
getenv(key string) string
|
getenv(key string) string
|
||||||
|
environ() map[string]string
|
||||||
getcwd() string
|
getcwd() string
|
||||||
homeDir() string
|
homeDir() string
|
||||||
hasFiles(pattern string) bool
|
hasFiles(pattern string) bool
|
||||||
|
@ -154,12 +155,13 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type environment struct {
|
type environment struct {
|
||||||
args *args
|
args *args
|
||||||
cwd string
|
cwd string
|
||||||
cmdCache *commandCache
|
cmdCache *commandCache
|
||||||
fileCache *fileCache
|
fileCache *fileCache
|
||||||
logBuilder strings.Builder
|
environCache map[string]string
|
||||||
debug bool
|
logBuilder strings.Builder
|
||||||
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *environment) init(args *args) {
|
func (env *environment) init(args *args) {
|
||||||
|
@ -222,6 +224,25 @@ func (env *environment) getenv(key string) string {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *environment) environ() map[string]string {
|
||||||
|
defer env.trace(time.Now(), "environ")
|
||||||
|
if env.environCache != nil {
|
||||||
|
return env.environCache
|
||||||
|
}
|
||||||
|
const separator = "="
|
||||||
|
values := os.Environ()
|
||||||
|
for value := range values {
|
||||||
|
splitted := strings.Split(values[value], separator)
|
||||||
|
if len(splitted) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
key := splitted[0]
|
||||||
|
val := splitted[1:]
|
||||||
|
env.environCache[key] = strings.Join(val, separator)
|
||||||
|
}
|
||||||
|
return env.environCache
|
||||||
|
}
|
||||||
|
|
||||||
func (env *environment) getcwd() string {
|
func (env *environment) getcwd() string {
|
||||||
defer env.trace(time.Now(), "getcwd")
|
defer env.trace(time.Now(), "getcwd")
|
||||||
if env.cwd != "" {
|
if env.cwd != "" {
|
||||||
|
|
|
@ -7,7 +7,8 @@ import (
|
||||||
type osInfo struct {
|
type osInfo struct {
|
||||||
props Properties
|
props Properties
|
||||||
env Environment
|
env Environment
|
||||||
OS string
|
|
||||||
|
os string
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -71,25 +72,25 @@ func (n *osInfo) string() string {
|
||||||
goos := n.env.getRuntimeGOOS()
|
goos := n.env.getRuntimeGOOS()
|
||||||
switch goos {
|
switch goos {
|
||||||
case windowsPlatform:
|
case windowsPlatform:
|
||||||
n.OS = windowsPlatform
|
n.os = windowsPlatform
|
||||||
return n.props.getString(Windows, "\uE62A")
|
return n.props.getString(Windows, "\uE62A")
|
||||||
case darwinPlatform:
|
case darwinPlatform:
|
||||||
n.OS = darwinPlatform
|
n.os = darwinPlatform
|
||||||
return n.props.getString(MacOS, "\uF179")
|
return n.props.getString(MacOS, "\uF179")
|
||||||
case linuxPlatform:
|
case linuxPlatform:
|
||||||
wsl := n.env.getenv("WSL_DISTRO_NAME")
|
wsl := n.env.getenv("WSL_DISTRO_NAME")
|
||||||
p := n.env.getPlatform()
|
p := n.env.getPlatform()
|
||||||
if len(wsl) == 0 {
|
if len(wsl) == 0 {
|
||||||
n.OS = p
|
n.os = p
|
||||||
return n.getDistroName(p, "")
|
return n.getDistroName(p, "")
|
||||||
}
|
}
|
||||||
n.OS = wsl
|
n.os = wsl
|
||||||
return fmt.Sprintf("%s%s%s",
|
return fmt.Sprintf("%s%s%s",
|
||||||
n.props.getString(WSL, "WSL"),
|
n.props.getString(WSL, "WSL"),
|
||||||
n.props.getString(WSLSeparator, " - "),
|
n.props.getString(WSLSeparator, " - "),
|
||||||
n.getDistroName(p, wsl))
|
n.getDistroName(p, wsl))
|
||||||
default:
|
default:
|
||||||
n.OS = goos
|
n.os = goos
|
||||||
return goos
|
return goos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,11 +76,11 @@ func TestOSInfo(t *testing.T) {
|
||||||
}
|
}
|
||||||
assert.Equal(t, tc.ExpectedString, osInfo.string(), tc.Case)
|
assert.Equal(t, tc.ExpectedString, osInfo.string(), tc.Case)
|
||||||
if tc.WSLDistro != "" {
|
if tc.WSLDistro != "" {
|
||||||
assert.Equal(t, tc.WSLDistro, osInfo.OS, tc.Case)
|
assert.Equal(t, tc.WSLDistro, osInfo.os, tc.Case)
|
||||||
} else if tc.Platform != "" {
|
} else if tc.Platform != "" {
|
||||||
assert.Equal(t, tc.Platform, osInfo.OS, tc.Case)
|
assert.Equal(t, tc.Platform, osInfo.os, tc.Case)
|
||||||
} else {
|
} else {
|
||||||
assert.Equal(t, tc.GOOS, osInfo.OS, tc.Case)
|
assert.Equal(t, tc.GOOS, osInfo.os, tc.Case)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,11 @@ func (env *MockedEnvironment) getenv(key string) string {
|
||||||
return args.String(0)
|
return args.String(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *MockedEnvironment) environ() map[string]string {
|
||||||
|
args := env.Called()
|
||||||
|
return args.Get(0).(map[string]string)
|
||||||
|
}
|
||||||
|
|
||||||
func (env *MockedEnvironment) getcwd() string {
|
func (env *MockedEnvironment) getcwd() string {
|
||||||
args := env.Called()
|
args := env.Called()
|
||||||
return args.String(0)
|
return args.String(0)
|
||||||
|
@ -212,6 +217,15 @@ func (env *MockedEnvironment) onTemplate() {
|
||||||
}
|
}
|
||||||
env.On(method).Return(returnArguments...)
|
env.On(method).Return(returnArguments...)
|
||||||
}
|
}
|
||||||
|
patchEnvVars := func() map[string]string {
|
||||||
|
keyValueArray := make(map[string]string)
|
||||||
|
for _, call := range env.Mock.ExpectedCalls {
|
||||||
|
if call.Method == "getenv" {
|
||||||
|
keyValueArray[call.Arguments.String(0)] = call.ReturnArguments.String(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keyValueArray
|
||||||
|
}
|
||||||
patchMethodIfNotSpecified("isRunningAsRoot", false)
|
patchMethodIfNotSpecified("isRunningAsRoot", false)
|
||||||
patchMethodIfNotSpecified("getcwd", "/usr/home/dev/my-app")
|
patchMethodIfNotSpecified("getcwd", "/usr/home/dev/my-app")
|
||||||
patchMethodIfNotSpecified("homeDir", "/usr/home/dev")
|
patchMethodIfNotSpecified("homeDir", "/usr/home/dev")
|
||||||
|
@ -220,6 +234,12 @@ func (env *MockedEnvironment) onTemplate() {
|
||||||
patchMethodIfNotSpecified("getCurrentUser", "dev")
|
patchMethodIfNotSpecified("getCurrentUser", "dev")
|
||||||
patchMethodIfNotSpecified("getHostName", "laptop", nil)
|
patchMethodIfNotSpecified("getHostName", "laptop", nil)
|
||||||
patchMethodIfNotSpecified("lastErrorCode", 0)
|
patchMethodIfNotSpecified("lastErrorCode", 0)
|
||||||
|
patchMethodIfNotSpecified("getRuntimeGOOS", darwinPlatform)
|
||||||
|
if env.getRuntimeGOOS() == linuxPlatform {
|
||||||
|
env.On("getenv", "WSL_DISTRO_NAME").Return("ubuntu")
|
||||||
|
env.On("getPlatform").Return("ubuntu")
|
||||||
|
}
|
||||||
|
patchMethodIfNotSpecified("environ", patchEnvVars())
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -34,6 +34,7 @@ type Context struct {
|
||||||
Host string
|
Host string
|
||||||
Code int
|
Code int
|
||||||
Env map[string]string
|
Env map[string]string
|
||||||
|
OS string
|
||||||
|
|
||||||
// Simple container to hold ANY object
|
// Simple container to hold ANY object
|
||||||
Data
|
Data
|
||||||
|
@ -55,13 +56,16 @@ func (c *Context) init(t *textTemplate) {
|
||||||
c.Host = host
|
c.Host = host
|
||||||
}
|
}
|
||||||
c.Code = t.Env.lastErrorCode()
|
c.Code = t.Env.lastErrorCode()
|
||||||
if strings.Contains(t.Template, ".Env.") {
|
c.Env = t.Env.environ()
|
||||||
c.Env = map[string]string{}
|
goos := t.Env.getRuntimeGOOS()
|
||||||
matches := findAllNamedRegexMatch(templateEnvRegex, t.Template)
|
if goos == linuxPlatform {
|
||||||
for _, match := range matches {
|
wsl := t.Env.getenv("WSL_DISTRO_NAME")
|
||||||
c.Env[match["ENV"]] = t.Env.getenv(match["ENV"])
|
goos = t.Env.getPlatform()
|
||||||
|
if len(wsl) != 0 {
|
||||||
|
goos = wsl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.OS = goos
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *textTemplate) render() (string, error) {
|
func (t *textTemplate) render() (string, error) {
|
||||||
|
@ -100,7 +104,7 @@ func (t *textTemplate) cleanTemplate() {
|
||||||
*knownVariables = append(*knownVariables, splitted[0])
|
*knownVariables = append(*knownVariables, splitted[0])
|
||||||
return splitted[0], true
|
return splitted[0], true
|
||||||
}
|
}
|
||||||
knownVariables := []string{"Root", "PWD", "Folder", "Shell", "User", "Host", "Env", "Data", "Code"}
|
knownVariables := []string{"Root", "PWD", "Folder", "Shell", "User", "Host", "Env", "Data", "Code", "OS"}
|
||||||
matches := findAllNamedRegexMatch(`(?: |{)(?P<var>(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template)
|
matches := findAllNamedRegexMatch(`(?: |{)(?P<var>(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template)
|
||||||
for _, match := range matches {
|
for _, match := range matches {
|
||||||
if variable, OK := unknownVariable(match["var"], &knownVariables); OK {
|
if variable, OK := unknownVariable(match["var"], &knownVariables); OK {
|
||||||
|
|
Loading…
Reference in a new issue