oh-my-posh/src/segments/winreg.go

50 lines
1 KiB
Go
Raw Normal View History

2022-01-26 06:54:36 -08:00
package segments
2021-11-24 04:47:30 -08:00
import (
"oh-my-posh/environment"
"oh-my-posh/properties"
)
2022-01-26 05:10:18 -08:00
type WindowsRegistry struct {
props properties.Properties
env environment.Environment
2021-11-24 04:47:30 -08:00
Value string
}
const (
// full path to the key; if ends in \, gets "(Default)" key in that path
RegistryPath properties.Property = "path"
// Fallback is the text to display if the key is not found
Fallback properties.Property = "fallback"
2021-11-24 04:47:30 -08:00
)
func (wr *WindowsRegistry) Template() string {
2022-02-01 05:07:58 -08:00
return " {{ .Value }} "
}
func (wr *WindowsRegistry) Init(props properties.Properties, env environment.Environment) {
2021-11-24 04:47:30 -08:00
wr.props = props
wr.env = env
}
func (wr *WindowsRegistry) Enabled() bool {
if wr.env.GOOS() != environment.WindowsPlatform {
2021-11-24 04:47:30 -08:00
return false
}
2022-01-26 04:09:21 -08:00
registryPath := wr.props.GetString(RegistryPath, "")
2022-06-01 00:50:20 -07:00
wr.Value = wr.props.GetString(Fallback, "")
2021-11-24 04:47:30 -08:00
2022-06-01 00:50:20 -07:00
regValue, err := wr.env.WindowsRegistryKeyValue(registryPath)
if err == nil {
wr.Value = regValue.String
return true
}
2022-06-01 00:50:20 -07:00
if len(wr.Value) > 0 {
// we have fallback value
return true
}
return false
2021-11-24 04:47:30 -08:00
}