From 3e3f4445a4e9d1289e470d8ca95dd12729fc9a22 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 11 Aug 2024 19:16:00 +0200 Subject: [PATCH] fix #1: add cache of latest command to not run wallpaper update when it's already up to date --- callback.go | 4 ++-- feh.go | 4 ++-- main.go | 7 +++++-- wallpaper.go | 14 +++++++++++--- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/callback.go b/callback.go index 63a9fc0..270c942 100644 --- a/callback.go +++ b/callback.go @@ -5,7 +5,7 @@ import ( "os/exec" ) -func CallbackUpdateWallpapers(callback string, files, screens []string) (*exec.Cmd, error) { +func CallbackUpdateWallpapers(callback string, files, screens []string) *exec.Cmd { args := []string{} for key, file := range files { @@ -15,5 +15,5 @@ func CallbackUpdateWallpapers(callback string, files, screens []string) (*exec.C cmd := exec.Command(callback, args...) cmd.Env = os.Environ() - return cmd, cmd.Run() + return cmd } diff --git a/feh.go b/feh.go index 4884960..9e8b562 100644 --- a/feh.go +++ b/feh.go @@ -5,7 +5,7 @@ import ( "os/exec" ) -func FehUpdateWallpapers(files []string) (*exec.Cmd, error) { +func FehUpdateWallpapers(files []string) *exec.Cmd { args := []string{} for _, file := range files { @@ -15,5 +15,5 @@ func FehUpdateWallpapers(files []string) (*exec.Cmd, error) { cmd := exec.Command("feh", args...) cmd.Env = os.Environ() - return cmd, cmd.Run() + return cmd } diff --git a/main.go b/main.go index ad7c01a..81c8d3e 100644 --- a/main.go +++ b/main.go @@ -16,18 +16,21 @@ func main() { } config, err := LoadConfiguration(os.Args[1]) + if err != nil { log.Printf("[ERROR] %s", err.Error()) os.Exit(1) } - UpdateWallapers(config) + var lastCommand string + + UpdateWallapers(config, &lastCommand) for recv.Next() { event := recv.Event().(*i3.WorkspaceEvent) if event.Change == "focus" { - UpdateWallapers(config) + UpdateWallapers(config, &lastCommand) } } } diff --git a/wallpaper.go b/wallpaper.go index 1919652..bec24d4 100644 --- a/wallpaper.go +++ b/wallpaper.go @@ -40,7 +40,7 @@ func GetOutputsWallpapers(config Config) ([]string, []string, error) { return files, screens, nil } -func UpdateWallapers(config Config) { +func UpdateWallapers(config Config, lastCommand *string) { files, screens, err := GetOutputsWallpapers(config) if err != nil { log.Printf("[ERROR] %s", err.Error()) @@ -50,14 +50,22 @@ func UpdateWallapers(config Config) { var cmd *exec.Cmd if config.Callback == "" { - cmd, err = FehUpdateWallpapers(files) + cmd = FehUpdateWallpapers(files) } else { - cmd, err = CallbackUpdateWallpapers(config.Callback, files, screens) + 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() } }