refactoring: move logic in different files
This commit is contained in:
parent
8e5d3c7907
commit
098b04f5a5
5 changed files with 108 additions and 49 deletions
57
main.go
57
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue