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 (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
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, "")
|
|
|
|
fallback := wr.props.GetString(Fallback, "")
|
2021-11-24 04:47:30 -08:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
var regValue *environment.WindowsRegistryValue
|
2022-01-23 12:37:51 -08:00
|
|
|
regValue, _ = wr.env.WindowsRegistryKeyValue(registryPath)
|
2021-11-15 13:21:41 -08:00
|
|
|
|
2021-12-04 13:11:25 -08:00
|
|
|
if regValue != nil {
|
2022-01-26 01:23:18 -08:00
|
|
|
switch regValue.ValueType {
|
|
|
|
case environment.RegString:
|
|
|
|
wr.Value = regValue.Str
|
2021-12-04 13:11:25 -08:00
|
|
|
return true
|
2022-01-26 01:23:18 -08:00
|
|
|
case environment.RegDword:
|
|
|
|
wr.Value = fmt.Sprintf("0x%08X", regValue.Dword)
|
2021-12-04 13:11:25 -08:00
|
|
|
return true
|
2022-01-26 01:23:18 -08:00
|
|
|
case environment.RegQword:
|
|
|
|
wr.Value = fmt.Sprintf("0x%016X", regValue.Qword)
|
2021-12-04 13:11:25 -08:00
|
|
|
return true
|
|
|
|
}
|
2021-11-29 02:17:10 -08:00
|
|
|
}
|
2021-11-15 13:21:41 -08:00
|
|
|
|
2021-11-29 02:17:10 -08:00
|
|
|
if len(fallback) > 0 {
|
|
|
|
wr.Value = fallback
|
|
|
|
return true
|
2021-11-25 00:15:10 -08:00
|
|
|
}
|
2021-11-15 13:21:41 -08:00
|
|
|
|
2021-11-29 02:17:10 -08:00
|
|
|
return false
|
2021-11-24 04:47:30 -08:00
|
|
|
}
|
|
|
|
|
2022-01-26 05:10:18 -08:00
|
|
|
func (wr WindowsRegistry) GetRegistryString(path string) (string, error) {
|
2022-01-23 12:37:51 -08:00
|
|
|
regValue, err := wr.env.WindowsRegistryKeyValue(path)
|
2021-12-04 13:11:25 -08:00
|
|
|
|
|
|
|
if regValue == nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
if regValue.ValueType != environment.RegString {
|
2021-12-04 13:11:25 -08:00
|
|
|
return "", errors.New("type mismatch, registry value is not a string")
|
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
return regValue.Str, nil
|
2021-12-04 13:11:25 -08:00
|
|
|
}
|
|
|
|
|
2022-01-26 05:10:18 -08:00
|
|
|
func (wr WindowsRegistry) GetRegistryDword(path string) (uint32, error) {
|
2022-01-23 12:37:51 -08:00
|
|
|
regValue, err := wr.env.WindowsRegistryKeyValue(path)
|
2021-12-04 13:11:25 -08:00
|
|
|
|
|
|
|
if regValue == nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
if regValue.ValueType != environment.RegDword {
|
2021-12-04 13:11:25 -08:00
|
|
|
return 0, errors.New("type mismatch, registry value is not a dword")
|
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
return regValue.Dword, nil
|
2021-12-04 13:11:25 -08:00
|
|
|
}
|
|
|
|
|
2022-01-26 05:10:18 -08:00
|
|
|
func (wr WindowsRegistry) GetRegistryQword(path string) (uint64, error) {
|
2022-01-23 12:37:51 -08:00
|
|
|
regValue, err := wr.env.WindowsRegistryKeyValue(path)
|
2021-12-04 13:11:25 -08:00
|
|
|
|
|
|
|
if regValue == nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
if regValue.ValueType != environment.RegQword {
|
2021-12-04 13:11:25 -08:00
|
|
|
return 0, errors.New("type mismatch, registry value is not a qword")
|
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
return regValue.Qword, nil
|
2021-12-04 13:11:25 -08:00
|
|
|
}
|