fix #1: add cache of latest command to not run wallpaper update when it's already up to date
Some checks are pending
ci/woodpecker/push/build Pipeline is pending approval
ci/woodpecker/push/test Pipeline is pending approval

This commit is contained in:
Simon Vieille 2024-08-11 19:16:00 +02:00
commit 3e3f4445a4
Signed by: deblan
GPG key ID: 579388D585F70417
4 changed files with 20 additions and 9 deletions

View file

@ -5,7 +5,7 @@ import (
"os/exec" "os/exec"
) )
func CallbackUpdateWallpapers(callback string, files, screens []string) (*exec.Cmd, error) { func CallbackUpdateWallpapers(callback string, files, screens []string) *exec.Cmd {
args := []string{} args := []string{}
for key, file := range files { for key, file := range files {
@ -15,5 +15,5 @@ func CallbackUpdateWallpapers(callback string, files, screens []string) (*exec.C
cmd := exec.Command(callback, args...) cmd := exec.Command(callback, args...)
cmd.Env = os.Environ() cmd.Env = os.Environ()
return cmd, cmd.Run() return cmd
} }

4
feh.go
View file

@ -5,7 +5,7 @@ import (
"os/exec" "os/exec"
) )
func FehUpdateWallpapers(files []string) (*exec.Cmd, error) { func FehUpdateWallpapers(files []string) *exec.Cmd {
args := []string{} args := []string{}
for _, file := range files { for _, file := range files {
@ -15,5 +15,5 @@ func FehUpdateWallpapers(files []string) (*exec.Cmd, error) {
cmd := exec.Command("feh", args...) cmd := exec.Command("feh", args...)
cmd.Env = os.Environ() cmd.Env = os.Environ()
return cmd, cmd.Run() return cmd
} }

View file

@ -16,18 +16,21 @@ func main() {
} }
config, err := LoadConfiguration(os.Args[1]) config, err := LoadConfiguration(os.Args[1])
if err != nil { if err != nil {
log.Printf("[ERROR] %s", err.Error()) log.Printf("[ERROR] %s", err.Error())
os.Exit(1) os.Exit(1)
} }
UpdateWallapers(config) var lastCommand string
UpdateWallapers(config, &lastCommand)
for recv.Next() { for recv.Next() {
event := recv.Event().(*i3.WorkspaceEvent) event := recv.Event().(*i3.WorkspaceEvent)
if event.Change == "focus" { if event.Change == "focus" {
UpdateWallapers(config) UpdateWallapers(config, &lastCommand)
} }
} }
} }

View file

@ -40,7 +40,7 @@ func GetOutputsWallpapers(config Config) ([]string, []string, error) {
return files, screens, nil return files, screens, nil
} }
func UpdateWallapers(config Config) { func UpdateWallapers(config Config, lastCommand *string) {
files, screens, err := GetOutputsWallpapers(config) files, screens, err := GetOutputsWallpapers(config)
if err != nil { if err != nil {
log.Printf("[ERROR] %s", err.Error()) log.Printf("[ERROR] %s", err.Error())
@ -50,14 +50,22 @@ func UpdateWallapers(config Config) {
var cmd *exec.Cmd var cmd *exec.Cmd
if config.Callback == "" { if config.Callback == "" {
cmd, err = FehUpdateWallpapers(files) cmd = FehUpdateWallpapers(files)
} else { } 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 { if err != nil {
log.Printf("[ERROR] cmd=%s error=%s", cmd.String(), err.Error()) log.Printf("[ERROR] cmd=%s error=%s", cmd.String(), err.Error())
} else { } else {
log.Printf("[SUCCESS] cmd=%s", cmd.String()) log.Printf("[SUCCESS] cmd=%s", cmd.String())
*lastCommand = cmd.String()
} }
} }