oh-my-posh/src/segment_winreg.go

119 lines
2.4 KiB
Go
Raw Normal View History

2021-11-24 04:47:30 -08:00
package main
import (
"errors"
"fmt"
)
2021-11-24 04:47:30 -08:00
type winreg struct {
2021-11-26 01:37:33 -08:00
props properties
2021-11-24 04:47:30 -08:00
env environmentInfo
Value string
}
const (
// full path to the key; if ends in \, gets "(Default)" key in that path
2021-11-24 04:47:30 -08:00
RegistryPath Property = "path"
// Fallback is the text to display if the key is not found
Fallback Property = "fallback"
2021-11-24 04:47:30 -08:00
)
2021-11-26 01:37:33 -08:00
func (wr *winreg) init(props properties, env environmentInfo) {
2021-11-24 04:47:30 -08:00
wr.props = props
wr.env = env
}
func (wr *winreg) enabled() bool {
if wr.env.getRuntimeGOOS() != windowsPlatform {
return false
}
registryPath := wr.props.getString(RegistryPath, "")
fallback := wr.props.getString(Fallback, "")
2021-11-24 04:47:30 -08:00
var regValue *windowsRegistryValue
regValue, _ = wr.env.getWindowsRegistryKeyValue(registryPath)
2021-11-15 13:21:41 -08:00
if regValue != nil {
switch regValue.valueType {
case regString:
wr.Value = regValue.str
return true
case regDword:
wr.Value = fmt.Sprintf("0x%08X", regValue.dword)
return true
case 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
}
func (wr *winreg) string() string {
segmentTemplate := wr.props.getString(SegmentTemplate, "{{ .Value }}")
return wr.templateString(segmentTemplate)
}
func (wr *winreg) templateString(segmentTemplate string) string {
template := &textTemplate{
Template: segmentTemplate,
Context: wr,
Env: wr.env,
}
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}
func (wr winreg) GetRegistryString(path string) (string, error) {
regValue, err := wr.env.getWindowsRegistryKeyValue(path)
if regValue == nil {
return "", err
}
if regValue.valueType != regString {
return "", errors.New("type mismatch, registry value is not a string")
}
return regValue.str, nil
}
func (wr winreg) GetRegistryDword(path string) (uint32, error) {
regValue, err := wr.env.getWindowsRegistryKeyValue(path)
if regValue == nil {
return 0, err
}
if regValue.valueType != regDword {
return 0, errors.New("type mismatch, registry value is not a dword")
}
return regValue.dword, nil
}
func (wr winreg) GetRegistryQword(path string) (uint64, error) {
regValue, err := wr.env.getWindowsRegistryKeyValue(path)
if regValue == nil {
return 0, err
}
if regValue.valueType != regQword {
return 0, errors.New("type mismatch, registry value is not a qword")
}
return regValue.qword, nil
}