oh-my-posh/src/segment_winreg.go

107 lines
2.3 KiB
Go
Raw Normal View History

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