feat(cli): get accent color

This commit is contained in:
Jan De Dobbeleer 2022-05-22 19:26:46 +02:00 committed by Jan De Dobbeleer
parent eb11aa9081
commit 3279821d0b
5 changed files with 51 additions and 8 deletions

11
.vscode/launch.json vendored
View file

@ -109,6 +109,17 @@
"--config=${workspaceRoot}/themes/default.omp.json" "--config=${workspaceRoot}/themes/default.omp.json"
] ]
}, },
{
"name": "Get value",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/src",
"args": [
"get",
"accent"
]
},
{ {
"type": "node", "type": "node",
"request": "launch", "request": "launch",

View file

@ -2,25 +2,29 @@ package cli
import ( import (
"fmt" "fmt"
"oh-my-posh/color"
"oh-my-posh/environment" "oh-my-posh/environment"
"time" "time"
color2 "github.com/gookit/color"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// getCmd represents the get command // getCmd represents the get command
var getCmd = &cobra.Command{ var getCmd = &cobra.Command{
Use: "get [shell|millis]", Use: "get [shell|millis|accent]",
Short: "Get a value from oh-my-posh", Short: "Get a value from oh-my-posh",
Long: `Get a value from oh-my-posh. Long: `Get a value from oh-my-posh.
This command is used to get the value of the following variables: This command is used to get the value of the following variables:
- shell - shell
- millis`, - millis
- accent`,
ValidArgs: []string{ ValidArgs: []string{
"millis", "millis",
"shell", "shell",
"accent",
}, },
Args: NoArgsOrOneValidArg, Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -38,6 +42,14 @@ This command is used to get the value of the following variables:
fmt.Print(time.Now().UnixNano() / 1000000) fmt.Print(time.Now().UnixNano() / 1000000)
case "shell": case "shell":
fmt.Println(env.Shell()) fmt.Println(env.Shell())
case "accent":
rgb, err := color.GetAccentColor()
if err != nil {
fmt.Println("error getting accent color:", err.Error())
return
}
accent := color2.RGB(rgb.R, rgb.G, rgb.B)
fmt.Println("#" + accent.Hex())
default: default:
_ = cmd.Help() _ = cmd.Help()
} }

View file

@ -19,6 +19,10 @@ func MakeColors(palette Palette, cacheEnabled bool, accentColor string) (colors
return return
} }
type RGB struct {
R, G, B uint8
}
// DefaultColors is the default AnsiColors implementation. // DefaultColors is the default AnsiColors implementation.
type DefaultColors struct { type DefaultColors struct {
accent *Color accent *Color

View file

@ -2,6 +2,12 @@
package color package color
import "errors"
func GetAccentColor() (*RGB, error) {
return nil, errors.New("not implemented")
}
func (d *DefaultColors) SetAccentColor(defaultColor string) { func (d *DefaultColors) SetAccentColor(defaultColor string) {
if len(defaultColor) == 0 { if len(defaultColor) == 0 {
return return

View file

@ -1,6 +1,7 @@
package color package color
import ( import (
"errors"
"syscall" "syscall"
"unsafe" "unsafe"
@ -13,24 +14,33 @@ var (
procDwmGetColorizationColor = dwmapi.NewProc("DwmGetColorizationColor") procDwmGetColorizationColor = dwmapi.NewProc("DwmGetColorizationColor")
) )
func (d *DefaultColors) SetAccentColor(defaultColor string) { func GetAccentColor() (*RGB, error) {
var accentColor uint32 var accentColor uint32
var pfOpaqueBlend bool var pfOpaqueBlend bool
_, _, e := procDwmGetColorizationColor.Call( _, _, e := procDwmGetColorizationColor.Call(
uintptr(unsafe.Pointer(&accentColor)), uintptr(unsafe.Pointer(&accentColor)),
uintptr(unsafe.Pointer(&pfOpaqueBlend))) uintptr(unsafe.Pointer(&pfOpaqueBlend)))
if e != windows.ERROR_SUCCESS { if e != windows.ERROR_SUCCESS {
return nil, errors.New("unable to get accent color")
}
return &RGB{
R: byte(accentColor >> 16),
G: byte(accentColor >> 8),
B: byte(accentColor),
}, nil
}
func (d *DefaultColors) SetAccentColor(defaultColor string) {
rgb, err := GetAccentColor()
if err != nil {
d.accent = &Color{ d.accent = &Color{
Foreground: string(d.AnsiColorFromString(defaultColor, false)), Foreground: string(d.AnsiColorFromString(defaultColor, false)),
Background: string(d.AnsiColorFromString(defaultColor, true)), Background: string(d.AnsiColorFromString(defaultColor, true)),
} }
return return
} }
r := byte(accentColor >> 16) foreground := color.RGB(rgb.R, rgb.G, rgb.B, false)
g := byte(accentColor >> 8) background := color.RGB(rgb.R, rgb.G, rgb.B, true)
b := byte(accentColor)
foreground := color.RGB(r, g, b, false)
background := color.RGB(r, g, b, true)
d.accent = &Color{ d.accent = &Color{
Foreground: foreground.String(), Foreground: foreground.String(),
Background: background.String(), Background: background.String(),