From 098b04f5a599c852913a5a8c68ee1099ed4b944a Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 31 Jul 2024 12:38:34 +0200 Subject: [PATCH] refactoring: move logic in different files --- config.go | 29 ++++++++++++++++++++++++++ event.go | 15 ++++++++++++++ feh.go | 19 ++++++++++++++++++ main.go | 57 ++++++++-------------------------------------------- wallpaper.go | 37 ++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 49 deletions(-) create mode 100644 config.go create mode 100644 event.go create mode 100644 feh.go create mode 100644 wallpaper.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..9e657cf --- /dev/null +++ b/config.go @@ -0,0 +1,29 @@ +package main + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +type Config struct { + Default string `yaml:"default"` + Workspaces map[string]string `yaml:"workspaces"` +} + +func LoadConfiguration(file string) (Config, error) { + data, err := os.ReadFile(os.Args[1]) + config := Config{} + + if err != nil { + return config, err + } + + err = yaml.Unmarshal(data, &config) + + if err != nil { + return config, err + } + + return config, nil +} diff --git a/event.go b/event.go new file mode 100644 index 0000000..1fdd9f3 --- /dev/null +++ b/event.go @@ -0,0 +1,15 @@ +package main + +import ( + "os/exec" +) + +func HandleFocusEvent(config Config) (*exec.Cmd, error) { + files, err := GetOutputsWallpapers(config) + + if err != nil { + return nil, err + } + + return FehUpdateWallpapers(files) +} diff --git a/feh.go b/feh.go new file mode 100644 index 0000000..4884960 --- /dev/null +++ b/feh.go @@ -0,0 +1,19 @@ +package main + +import ( + "os" + "os/exec" +) + +func FehUpdateWallpapers(files []string) (*exec.Cmd, error) { + args := []string{} + + for _, file := range files { + args = append(args, "--bg-fill", file) + } + + cmd := exec.Command("feh", args...) + cmd.Env = os.Environ() + + return cmd, cmd.Run() +} diff --git a/main.go b/main.go index 38fbeba..e165275 100644 --- a/main.go +++ b/main.go @@ -3,28 +3,10 @@ package main import ( "log" "os" - "os/exec" "go.i3wm.org/i3" - "gopkg.in/yaml.v3" ) -type Config struct { - Default string `yaml:"default"` - Workspaces map[string]string `yaml:"workspaces"` -} - -func getWallpaper(workspace string, config Config) string { - file := config.Default - workspaceFile := config.Workspaces[workspace] - - if workspaceFile != "" { - file = workspaceFile - } - - return file -} - func main() { recv := i3.Subscribe(i3.WorkspaceEventType) @@ -33,47 +15,24 @@ func main() { os.Exit(1) } - data, err := os.ReadFile(os.Args[1]) - config := Config{} + config, err := LoadConfiguration(os.Args[1]) if err != nil { - log.Printf("[error] %s", err.Error()) - os.Exit(1) - } - - err = yaml.Unmarshal(data, &config) - - if err != nil { - log.Printf("[error] %s", err.Error()) + log.Printf("[ERROR] %s", err.Error()) os.Exit(1) } for recv.Next() { event := recv.Event().(*i3.WorkspaceEvent) - if event.Change != "focus" { - continue - } + if event.Change == "focus" { + cmd, err := HandleFocusEvent(config) - outputs, err := i3.GetOutputs() - args := []string{} - - if err == nil { - for _, output := range outputs { - if output.CurrentWorkspace != "" { - args = append(args, "--bg-fill", getWallpaper(output.CurrentWorkspace, config)) - } + if err != nil { + log.Printf("[ERROR] cmd=%s error=%s", cmd.String(), err.Error()) + } else { + log.Printf("[SUCCESS] cmd=%s", cmd.String()) } - - cmd := exec.Command("feh", args...) - cmd.Env = os.Environ() - log.Printf("[UPDATE] %s", cmd.String()) - - if err := cmd.Run(); err != nil { - log.Printf("[ERROR] %s", err.Error()) - } - } else { - log.Printf("[ERROR] %s", err.Error()) } } } diff --git a/wallpaper.go b/wallpaper.go new file mode 100644 index 0000000..1675ee0 --- /dev/null +++ b/wallpaper.go @@ -0,0 +1,37 @@ +package main + +import ( + "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, error) { + files := []string{} + outputs, err := i3.GetOutputs() + + if err != nil { + return files, err + } + + for _, output := range outputs { + if output.CurrentWorkspace != "" { + file := GetWorkspaceWallpaper(output.CurrentWorkspace, config) + + if file != "" { + files = append(files, file) + } + } + } + + return files, nil +}