From 081624df65c69c6efd9d60aa480b48bfbf0ea9d8 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 30 Jul 2024 22:03:39 +0200 Subject: [PATCH 01/30] update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f3951b6..d07a7ca 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,11 @@ workspaces: Next, you simply need to run `i3-wallpaper-manager` while specify the path to the configuration file. ``` -i3-wallpaper-manager /path/to/config.yaml +DISPLAY=:0 i3-wallpaper-manager /path/to/config.yaml ``` To run it when i3 starts, add this to your i3 configuration: ``` -exec_always i3-wallpaper-manager /path/to/config.yaml +exec_always DISPLAY=:0 i3-wallpaper-manager /path/to/config.yaml ``` From 0ebbd3f0d4c4f73023b48634c927a4e1f91ef0a8 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 30 Jul 2024 22:07:07 +0200 Subject: [PATCH 02/30] update readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index d07a7ca..159855a 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ Pre-compiled versions are available in the [Releases](https://gitnet.fr/deblan/i3-wallpaper-manager/releases). For Debian users, a package is also provided. +Requirements: +- [FEH](https://feh.finalrewind.org/) + If you want to compile the project from source, you will need at least the GO compiler version 1.22. Clone the project and run the make command. The compiled output will be located in the `build` directory. ``` From 8ebc08dc33f202d9e17a3f82e1632b2e8cef3978 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 30 Jul 2024 22:09:35 +0200 Subject: [PATCH 03/30] update readme --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 159855a..c47c794 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,15 @@ ## 📗 How to install the project +### Dependencies + +- [Feh](https://feh.finalrewind.org/) + +### Pre-compiled versions + Pre-compiled versions are available in the [Releases](https://gitnet.fr/deblan/i3-wallpaper-manager/releases). For Debian users, a package is also provided. -Requirements: -- [FEH](https://feh.finalrewind.org/) +### From source If you want to compile the project from source, you will need at least the GO compiler version 1.22. Clone the project and run the make command. The compiled output will be located in the `build` directory. From c479c756d7d56d0023e24bfd82f688badbcb4d01 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 30 Jul 2024 22:33:53 +0200 Subject: [PATCH 04/30] use log instead of fmt --- main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 44161f5..38fbeba 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "os" "os/exec" @@ -30,7 +29,7 @@ func main() { recv := i3.Subscribe(i3.WorkspaceEventType) if len(os.Args) != 2 { - fmt.Errorf("Configuration required!") + log.Printf("[ERROR] Configuration required!") os.Exit(1) } @@ -39,12 +38,14 @@ func main() { 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()) + os.Exit(1) } for recv.Next() { From 8e5d3c79073496fe439b49b822afe734247112dc Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 30 Jul 2024 22:36:47 +0200 Subject: [PATCH 05/30] update readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c47c794..ce80a89 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ $ make To start, you need to create a configuration file in YAML format. This file consists of two keys: `default` to define the path of the default wallpaper, and `workspaces` which allows you to associate each workspace with its corresponding wallpaper. -``` +```yaml default: /home/alice/wallpapers/default.jpg workspaces: "Name of the workspace 1": /home/alice/wallpapers/1.jpg @@ -35,12 +35,12 @@ workspaces: Next, you simply need to run `i3-wallpaper-manager` while specify the path to the configuration file. -``` +```bash DISPLAY=:0 i3-wallpaper-manager /path/to/config.yaml ``` To run it when i3 starts, add this to your i3 configuration: -``` +```bash exec_always DISPLAY=:0 i3-wallpaper-manager /path/to/config.yaml ``` From 098b04f5a599c852913a5a8c68ee1099ed4b944a Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 31 Jul 2024 12:38:34 +0200 Subject: [PATCH 06/30] 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 +} From 88a73855f783f74e71d6e062b4ad180405cb2d57 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 31 Jul 2024 12:43:32 +0200 Subject: [PATCH 07/30] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4cb91a..8ee4019 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ [Unreleased] +## v1.1.0 +### Changed +- Refactoring + ## v1.0.0 ### Added - First release! From c3d8b02d8cd032f8e4e179337886e24962ffec5e Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 31 Jul 2024 12:43:40 +0200 Subject: [PATCH 08/30] apply linter --- event.go | 1 - main.go | 1 - wallpaper.go | 1 - 3 files changed, 3 deletions(-) diff --git a/event.go b/event.go index 1fdd9f3..d59241d 100644 --- a/event.go +++ b/event.go @@ -6,7 +6,6 @@ import ( func HandleFocusEvent(config Config) (*exec.Cmd, error) { files, err := GetOutputsWallpapers(config) - if err != nil { return nil, err } diff --git a/main.go b/main.go index e165275..0c9e58a 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,6 @@ func main() { } config, err := LoadConfiguration(os.Args[1]) - if err != nil { log.Printf("[ERROR] %s", err.Error()) os.Exit(1) diff --git a/wallpaper.go b/wallpaper.go index 1675ee0..3fe854e 100644 --- a/wallpaper.go +++ b/wallpaper.go @@ -18,7 +18,6 @@ func GetWorkspaceWallpaper(workspace string, config Config) string { func GetOutputsWallpapers(config Config) ([]string, error) { files := []string{} outputs, err := i3.GetOutputs() - if err != nil { return files, err } From 530ecf4689f961b535e30ee9e50c97a9c519f211 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 31 Jul 2024 22:55:27 +0200 Subject: [PATCH 09/30] update readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index ce80a89..26d1fd9 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ **i3 wallpaper manager** is a command-line tool designed to change the wallpaper depending of active workspaces on [i3](https://i3wm.org/). +
+
+ ## 📗 How to install the project ### Dependencies From 4c42d27e655eab61e6096339010cce943b6d4d47 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 31 Jul 2024 22:56:33 +0200 Subject: [PATCH 10/30] update readme --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 26d1fd9..eca390e 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,7 @@ **i3 wallpaper manager** is a command-line tool designed to change the wallpaper depending of active workspaces on [i3](https://i3wm.org/). -
-
+