2022-01-26 06:54:36 -08:00
|
|
|
package segments
|
2021-11-24 04:47:30 -08:00
|
|
|
|
2021-12-04 13:11:25 -08:00
|
|
|
import (
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/environment"
|
2022-01-26 04:53:35 -08:00
|
|
|
"oh-my-posh/properties"
|
2021-12-04 13:11:25 -08:00
|
|
|
)
|
|
|
|
|
2022-01-26 05:10:18 -08:00
|
|
|
type WindowsRegistry struct {
|
2022-01-26 04:53:35 -08:00
|
|
|
props properties.Properties
|
2022-01-26 01:23:18 -08:00
|
|
|
env environment.Environment
|
2021-11-24 04:47:30 -08:00
|
|
|
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-12-04 13:11:25 -08:00
|
|
|
// full path to the key; if ends in \, gets "(Default)" key in that path
|
2022-01-26 04:53:35 -08:00
|
|
|
RegistryPath properties.Property = "path"
|
2021-11-25 00:15:10 -08:00
|
|
|
// Fallback is the text to display if the key is not found
|
2022-01-26 04:53:35 -08:00
|
|
|
Fallback properties.Property = "fallback"
|
2021-11-24 04:47:30 -08:00
|
|
|
)
|
|
|
|
|
2022-01-26 05:26:56 -08:00
|
|
|
func (wr *WindowsRegistry) Template() string {
|
2022-02-01 05:07:58 -08:00
|
|
|
return " {{ .Value }} "
|
2022-01-23 12:37:51 -08:00
|
|
|
}
|
|
|
|
|
2022-01-26 05:26:56 -08:00
|
|
|
func (wr *WindowsRegistry) Init(props properties.Properties, env environment.Environment) {
|
2021-11-24 04:47:30 -08:00
|
|
|
wr.props = props
|
|
|
|
wr.env = env
|
|
|
|
}
|
|
|
|
|
2022-01-26 05:26:56 -08:00
|
|
|
func (wr *WindowsRegistry) Enabled() bool {
|
2022-01-26 01:23:18 -08:00
|
|
|
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
|
2021-11-29 02:17:10 -08:00
|
|
|
}
|
2022-06-01 00:50:20 -07:00
|
|
|
if len(wr.Value) > 0 {
|
|
|
|
// we have fallback value
|
2021-11-29 02:17:10 -08:00
|
|
|
return true
|
2021-11-25 00:15:10 -08:00
|
|
|
}
|
2021-11-29 02:17:10 -08:00
|
|
|
return false
|
2021-11-24 04:47:30 -08:00
|
|
|
}
|