103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Helper functions
|
|
func getWindowList() (map[string]string, error) {
|
|
cmd := exec.Command("wmctrl", "-l")
|
|
var out bytes.Buffer
|
|
cmd.Stdout = &out
|
|
if err := cmd.Run(); err != nil {
|
|
return nil, err
|
|
}
|
|
windows := make(map[string]string)
|
|
scanner := bufio.NewScanner(&out)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
fields := strings.Fields(line)
|
|
if len(fields) > 0 {
|
|
windows[fields[0]] = strings.Join(fields[3:], " ")
|
|
}
|
|
}
|
|
return windows, nil
|
|
}
|
|
|
|
// findNewWindow compares two maps of windows and returns the ID of the new window.
|
|
func findNewWindow(before, after map[string]string) string {
|
|
for id := range after {
|
|
if _, ok := before[id]; !ok {
|
|
return id
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// parseConfig remains the same as before.
|
|
func parseConfig(filePath string) ([]WindowConfig, error) {
|
|
file, err := os.Open(filePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
var configs []WindowConfig
|
|
scanner := bufio.NewScanner(file)
|
|
var currentConfig WindowConfig
|
|
|
|
homeDir, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not get user home directory: %w", err)
|
|
}
|
|
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if strings.HasPrefix(line, " Title: ") {
|
|
title := strings.TrimSpace(strings.TrimPrefix(line, " Title: "))
|
|
currentConfig.Title = title
|
|
parts := strings.SplitN(title, ": ", 2)
|
|
if len(parts) == 2 {
|
|
path := parts[1]
|
|
if strings.HasPrefix(path, "~") {
|
|
path = filepath.Join(homeDir, path[1:])
|
|
}
|
|
currentConfig.Path = path
|
|
}
|
|
} else if strings.HasPrefix(line, " Geometry: ") {
|
|
geomStr := strings.TrimSpace(strings.TrimPrefix(line, " Geometry: "))
|
|
var x, y, w, h string
|
|
_, err := fmt.Sscanf(geomStr, "X=%s Y=%s Width=%s Height=%s", &x, &y, &w, &h)
|
|
if err == nil {
|
|
x = strings.TrimSuffix(x, ",")
|
|
y = strings.TrimSuffix(y, ",")
|
|
w = strings.TrimSuffix(w, ",")
|
|
currentConfig.Geometry = fmt.Sprintf("%sx%s+%s+%s", w, h, x, y)
|
|
}
|
|
} else if strings.HasPrefix(line, " Workspace: ") {
|
|
currentConfig.Workspace = strings.TrimSpace(strings.TrimPrefix(line, " Workspace: "))
|
|
} else if line == "---" {
|
|
if currentConfig.Path != "" {
|
|
configs = append(configs, currentConfig)
|
|
}
|
|
currentConfig = WindowConfig{} // Reset for the next entry
|
|
}
|
|
}
|
|
|
|
if currentConfig.Path != "" {
|
|
configs = append(configs, currentConfig)
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return configs, nil
|
|
}
|