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": case "shell":
fmt.Println(env.Shell()) fmt.Println(env.Shell())
case "accent": case "accent":
rgb, err := color.GetAccentColor() rgb, err := color.GetAccentColor(env)
if err != nil { if err != nil {
fmt.Println("error getting accent color:", err.Error()) fmt.Println("error getting accent color:", err.Error())
return return

View file

@ -2,13 +2,14 @@ package color
import ( import (
"fmt" "fmt"
"oh-my-posh/environment"
"github.com/gookit/color" "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 := &DefaultColors{}
defaultColors.SetAccentColor(accentColor) defaultColors.SetAccentColor(env, accentColor)
colors = defaultColors colors = defaultColors
if palette != nil { if palette != nil {
colors = &PaletteColors{ansiColors: colors, palette: palette} colors = &PaletteColors{ansiColors: colors, palette: palette}

View file

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

View file

@ -2,13 +2,16 @@
package color 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") 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 { if len(defaultColor) == 0 {
return return
} }

View file

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

View file

@ -57,7 +57,7 @@ type Config struct {
// environment and configuration. // environment and configuration.
func (cfg *Config) MakeColors(env environment.Environment) color.AnsiColors { func (cfg *Config) MakeColors(env environment.Environment) color.AnsiColors {
cacheDisabled := env.Getenv("OMP_CACHE_DISABLED") == "1" 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) { func (cfg *Config) print(message string) {