i3-wallpaper-manager/wallpaper.go
Simon Vieille 3e3f4445a4
Some checks are pending
ci/woodpecker/push/build Pipeline is pending approval
ci/woodpecker/push/test Pipeline is pending approval
fix #1: add cache of latest command to not run wallpaper update when it's already up to date
2024-08-11 19:16:00 +02:00

71 lines
1.4 KiB
Go

package main
import (
"log"
"os/exec"
"go.i3wm.org/i3"
)
func GetWorkspaceWallpaper(workspace string, config Config) string {
file := config.Default
workspaceFile := config.Workspaces[workspace]
if workspaceFile != "" {
file = workspaceFile
}
return file
}
func GetOutputsWallpapers(config Config) ([]string, []string, error) {
files := []string{}
screens := []string{}
outputs, err := i3.GetOutputs()
if err != nil {
return files, screens, err
}
for _, output := range outputs {
if output.CurrentWorkspace != "" {
file := GetWorkspaceWallpaper(output.CurrentWorkspace, config)
if file != "" {
files = append(files, file)
screens = append(screens, output.Name)
}
}
}
return files, screens, nil
}
func UpdateWallapers(config Config, lastCommand *string) {
files, screens, err := GetOutputsWallpapers(config)
if err != nil {
log.Printf("[ERROR] %s", err.Error())
return
}
var cmd *exec.Cmd
if config.Callback == "" {
cmd = FehUpdateWallpapers(files)
} else {
cmd = CallbackUpdateWallpapers(config.Callback, files, screens)
}
if cmd.String() == *lastCommand {
log.Printf("[INFO] wallapaper(s) already up to date")
return
}
err = cmd.Run()
if err != nil {
log.Printf("[ERROR] cmd=%s error=%s", cmd.String(), err.Error())
} else {
log.Printf("[SUCCESS] cmd=%s", cmd.String())
*lastCommand = cmd.String()
}
}