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"
]
},
{
"name": "Get value",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/src",
"args": [
"get",
"accent"
]
},
{
"type": "node",
"request": "launch",

View file

@ -2,25 +2,29 @@ package cli
import (
"fmt"
"oh-my-posh/color"
"oh-my-posh/environment"
"time"
color2 "github.com/gookit/color"
"github.com/spf13/cobra"
)
// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get [shell|millis]",
Use: "get [shell|millis|accent]",
Short: "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:
- shell
- millis`,
- millis
- accent`,
ValidArgs: []string{
"millis",
"shell",
"accent",
},
Args: NoArgsOrOneValidArg,
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)
case "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:
_ = cmd.Help()
}

View file

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

View file

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

View file

@ -1,6 +1,7 @@
package color
import (
"errors"
"syscall"
"unsafe"
@ -13,24 +14,33 @@ var (
procDwmGetColorizationColor = dwmapi.NewProc("DwmGetColorizationColor")
)
func (d *DefaultColors) SetAccentColor(defaultColor string) {
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")
}
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{
Foreground: string(d.AnsiColorFromString(defaultColor, false)),
Background: string(d.AnsiColorFromString(defaultColor, true)),
}
return
}
r := byte(accentColor >> 16)
g := byte(accentColor >> 8)
b := byte(accentColor)
foreground := color.RGB(r, g, b, false)
background := color.RGB(r, g, b, true)
foreground := color.RGB(rgb.R, rgb.G, rgb.B, false)
background := color.RGB(rgb.R, rgb.G, rgb.B, true)
d.accent = &Color{
Foreground: foreground.String(),
Background: background.String(),