fix(accent): get correct accent color on Windows

This commit is contained in:
Jan De Dobbeleer 2022-05-29 00:28:51 +02:00 committed by Jan De Dobbeleer
parent 8b21a67d7a
commit 42be6ab608
6 changed files with 29 additions and 32 deletions

View file

@ -43,7 +43,7 @@ This command is used to get the value of the following variables:
case "shell":
fmt.Println(env.Shell())
case "accent":
rgb, err := color.GetAccentColor()
rgb, err := color.GetAccentColor(env)
if err != nil {
fmt.Println("error getting accent color:", err.Error())
return

View file

@ -2,13 +2,14 @@ package color
import (
"fmt"
"oh-my-posh/environment"
"github.com/gookit/color"
)
func MakeColors(palette Palette, cacheEnabled bool, accentColor string) (colors AnsiColors) {
func MakeColors(palette Palette, cacheEnabled bool, accentColor string, env environment.Environment) (colors AnsiColors) {
defaultColors := &DefaultColors{}
defaultColors.SetAccentColor(accentColor)
defaultColors.SetAccentColor(env, accentColor)
colors = defaultColors
if palette != nil {
colors = &PaletteColors{ansiColors: colors, palette: palette}

View file

@ -30,18 +30,18 @@ func TestGetAnsiFromColorString(t *testing.T) {
}
func TestMakeColors(t *testing.T) {
colors := MakeColors(nil, false, "")
colors := MakeColors(nil, false, "", nil)
assert.IsType(t, &DefaultColors{}, colors)
colors = MakeColors(nil, true, "")
colors = MakeColors(nil, true, "", nil)
assert.IsType(t, &CachedColors{}, colors)
assert.IsType(t, &DefaultColors{}, colors.(*CachedColors).ansiColors)
colors = MakeColors(testPalette, false, "")
colors = MakeColors(testPalette, false, "", nil)
assert.IsType(t, &PaletteColors{}, colors)
assert.IsType(t, &DefaultColors{}, colors.(*PaletteColors).ansiColors)
colors = MakeColors(testPalette, true, "")
colors = MakeColors(testPalette, true, "", nil)
assert.IsType(t, &CachedColors{}, colors)
assert.IsType(t, &PaletteColors{}, colors.(*CachedColors).ansiColors)
assert.IsType(t, &DefaultColors{}, colors.(*CachedColors).ansiColors.(*PaletteColors).ansiColors)

View file

@ -2,13 +2,16 @@
package color
import "errors"
import (
"errors"
"oh-my-posh/environment"
)
func GetAccentColor() (*RGB, error) {
func GetAccentColor(env environment.Environment) (*RGB, error) {
return nil, errors.New("not implemented")
}
func (d *DefaultColors) SetAccentColor(defaultColor string) {
func (d *DefaultColors) SetAccentColor(env environment.Environment, defaultColor string) {
if len(defaultColor) == 0 {
return
}

View file

@ -2,36 +2,29 @@ package color
import (
"errors"
"syscall"
"unsafe"
"oh-my-posh/environment"
"github.com/gookit/color"
"golang.org/x/sys/windows"
)
var (
dwmapi = syscall.NewLazyDLL("dwmapi.dll")
procDwmGetColorizationColor = dwmapi.NewProc("DwmGetColorizationColor")
)
func GetAccentColor() (*RGB, error) {
var accentColor uint32
var pfOpaqueBlend bool
_, _, e := procDwmGetColorizationColor.Call(
uintptr(unsafe.Pointer(&accentColor)),
uintptr(unsafe.Pointer(&pfOpaqueBlend)))
if e != windows.ERROR_SUCCESS {
return nil, errors.New("unable to get accent color")
func GetAccentColor(env environment.Environment) (*RGB, error) {
if env == nil {
return nil, errors.New("unable to get color without environment")
}
// see https://stackoverflow.com/questions/3560890/vista-7-how-to-get-glass-color
value, err := env.WindowsRegistryKeyValue(`HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM\ColorizationColor`)
if err != nil {
return nil, err
}
return &RGB{
R: byte(accentColor >> 16),
G: byte(accentColor >> 8),
B: byte(accentColor),
R: byte(value.Dword >> 16),
G: byte(value.Dword >> 8),
B: byte(value.Dword),
}, nil
}
func (d *DefaultColors) SetAccentColor(defaultColor string) {
rgb, err := GetAccentColor()
func (d *DefaultColors) SetAccentColor(env environment.Environment, defaultColor string) {
rgb, err := GetAccentColor(env)
if err != nil {
d.accent = &Color{
Foreground: string(d.AnsiColorFromString(defaultColor, false)),

View file

@ -57,7 +57,7 @@ type Config struct {
// environment and configuration.
func (cfg *Config) MakeColors(env environment.Environment) color.AnsiColors {
cacheDisabled := env.Getenv("OMP_CACHE_DISABLED") == "1"
return color.MakeColors(cfg.Palette, !cacheDisabled, cfg.AccentColor)
return color.MakeColors(cfg.Palette, !cacheDisabled, cfg.AccentColor, env)
}
func (cfg *Config) print(message string) {