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

57
main.go
View file

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