Skip to content

Instantly share code, notes, and snippets.

@dakyskye
Last active February 18, 2022 19:20
Show Gist options
  • Save dakyskye/ff1232e5032e1d1ce4ddd2ea71f68703 to your computer and use it in GitHub Desktop.
Save dakyskye/ff1232e5032e1d1ce4ddd2ea71f68703 to your computer and use it in GitHub Desktop.
package main
import (
"log"
)
const (
regKey = `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`
regName = `AppsUseLightTheme`
)
func main() {
regNotifyChangeKeyValue, err := getRegNotifyChangeKeyValueProc()
if err != nil {
log.Fatal(err)
}
changed := monitor(regNotifyChangeKeyValue)
for {
<-changed
if !isSpotifyRunning() {
continue
}
err := apply()
if err != nil {
log.Println(err)
}
}
}
package main
import (
"log"
"syscall"
"golang.org/x/sys/windows/registry"
)
func getRegNotifyChangeKeyValueProc() (*syscall.Proc, error) {
advapi32, err := syscall.LoadDLL("Advapi32.dll")
if err != nil {
return nil, err
}
proc, err := advapi32.FindProc("RegNotifyChangeKeyValue")
if err != nil {
return nil, err
}
return proc, nil
}
func monitor(regNotifyChangeKeyValue *syscall.Proc) <-chan struct{} {
notifier := make(chan struct{})
go func() {
k, err := registry.OpenKey(registry.CURRENT_USER, regKey, syscall.KEY_NOTIFY|registry.QUERY_VALUE)
if err != nil {
log.Fatal(err)
}
getVal := func() uint64 {
val, _, err := k.GetIntegerValue(regName)
if err != nil {
log.Fatal(err)
}
return val
}
old := getVal()
for {
regNotifyChangeKeyValue.Call(uintptr(k), 0, 0x00000001|0x00000004, 0, 0) //nolint:errcheck
val := getVal()
if val != old {
old = val
notifier <- struct{}{}
}
}
}()
return notifier
}
diff --git a/go.mod b/go.mod
index 3203f5c..a2a2054 100644
--- a/go.mod
+++ b/go.mod
@@ -7,5 +7,6 @@ require (
github.com/mattn/go-colorable v0.1.8
github.com/smartystreets/goconvey v1.6.4 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
+ golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
)
diff --git a/go.sum b/go.sum
index dd6ab0b..f5ae308 100644
--- a/go.sum
+++ b/go.sum
@@ -21,6 +21,8 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
+golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
diff --git a/src/cmd/cmd.go b/src/cmd/cmd.go
index 29c2c04..b6cabb4 100644
--- a/src/cmd/cmd.go
+++ b/src/cmd/cmd.go
@@ -10,6 +10,7 @@ import (
"github.com/go-ini/ini"
"github.com/khanhas/spicetify-cli/src/utils"
+ "golang.org/x/sys/windows/registry"
)
var (
@@ -110,21 +111,29 @@ func InitPaths() {
utils.CheckExistAndCreate(appDestPath)
}
+func isOsThemeDark() bool {
+ k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Themes\Personalize`, registry.QUERY_VALUE)
+ if err != nil {
+ utils.PrintError("could not open registry key: " + err.Error())
+ os.Exit(1)
+ }
+ val, _, err := k.GetIntegerValue("SystemUsesLightTheme")
+ if err != nil {
+ utils.PrintError("could not read SystemUsesLightTheme value: " + err.Error())
+ os.Exit(1)
+ }
+ k.Close()
+
+ return val == 0
+}
+
// InitSetting parses theme settings and gets color section.
func InitSetting() {
replaceColors = settingSection.Key("replace_colors").MustBool(false)
injectCSS = settingSection.Key("inject_css").MustBool(false)
overwriteAssets = settingSection.Key("overwrite_assets").MustBool(false)
- themeName := settingSection.Key("current_theme").String()
-
- if len(themeName) == 0 {
- injectCSS = false
- replaceColors = false
- overwriteAssets = false
- return
- }
-
+ themeName := "Ziro"
themeFolder = getThemeFolder(themeName)
colorPath := filepath.Join(themeFolder, "color.ini")
@@ -165,7 +174,10 @@ func InitSetting() {
return
}
- schemeName := settingSection.Key("color_scheme").String()
+ schemeName := "purple-light"
+ if isOsThemeDark() {
+ schemeName = "purple-dark"
+ }
if len(schemeName) == 0 {
colorSection = sections[1]
return
package main
import (
"os/exec"
)
func isSpotifyRunning() bool {
ps, _ := exec.LookPath("powershell.exe")
ret := exec.Command(ps,
"-NoProfile",
"-NonInteractive",
`$spotify = Get-Process Spotify; if ($spotify) { exit 0 } else { exit 1}`,
).Run()
return ret == nil
}
func apply() error {
return exec.Command(`C:\Users\dakyskye\spicetify-cli\spicetify.exe`, "apply").Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment