refactoring: move logic in different files
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-07-31 12:38:34 +02:00
commit 098b04f5a5
Signed by: deblan
GPG key ID: 579388D585F70417
5 changed files with 108 additions and 49 deletions

29
config.go Normal file
View file

@ -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
}

15
event.go Normal file
View file

@ -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)
}

19
feh.go Normal file
View file

@ -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()
}

57
main.go
View file

@ -3,28 +3,10 @@ package main
import ( import (
"log" "log"
"os" "os"
"os/exec"
"go.i3wm.org/i3" "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() { func main() {
recv := i3.Subscribe(i3.WorkspaceEventType) recv := i3.Subscribe(i3.WorkspaceEventType)
@ -33,47 +15,24 @@ func main() {
os.Exit(1) os.Exit(1)
} }
data, err := os.ReadFile(os.Args[1]) config, err := LoadConfiguration(os.Args[1])
config := Config{}
if err != nil { if err != nil {
log.Printf("[error] %s", err.Error()) 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) os.Exit(1)
} }
for recv.Next() { for recv.Next() {
event := recv.Event().(*i3.WorkspaceEvent) event := recv.Event().(*i3.WorkspaceEvent)
if event.Change != "focus" { if event.Change == "focus" {
continue cmd, err := HandleFocusEvent(config)
}
outputs, err := i3.GetOutputs() if err != nil {
args := []string{} log.Printf("[ERROR] cmd=%s error=%s", cmd.String(), err.Error())
if err == nil {
for _, output := range outputs {
if output.CurrentWorkspace != "" {
args = append(args, "--bg-fill", getWallpaper(output.CurrentWorkspace, config))
}
}
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 { } else {
log.Printf("[ERROR] %s", err.Error()) log.Printf("[SUCCESS] cmd=%s", cmd.String())
}
} }
} }
} }

37
wallpaper.go Normal file
View file

@ -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
}