summaryrefslogtreecommitdiff
path: root/launch_terminal.go
blob: 7ee72fe06537af89b01815ad1594a9752e71efab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
	"time"
)

// WindowConfig holds the configuration for a single terminal window.
type WindowConfig struct {
	Title     string
	Path      string
	Geometry  string // In WIDTHxHEIGHT+X+Y format
	Workspace string
}

func doLaunch() {
	// 1. Get current working directory.
	pwd, err := os.Getwd()
	if err != nil {
		fmt.Println("Failed to get current directory:", err)
		return
	}

	// 2. Read and parse the configuration file.
	configs, err := parseConfig(configFile)
	if err != nil {
		fmt.Printf("Failed to parse config file '%s': %v\n", configFile, err)
		return
	}

	// 3. Find the best matching configuration for the current directory.
	var bestMatch *WindowConfig
	longestPrefix := 0
	for i, config := range configs {
		if strings.HasPrefix(pwd, config.Path) {
			if len(config.Path) > longestPrefix {
				longestPrefix = len(config.Path)
				bestMatch = &configs[i]
			}
		}
	}

	if bestMatch == nil {
		fmt.Printf("No configuration found for directory: %s\n", pwd)
		return
	}
	targetConfig := bestMatch
	fmt.Printf("Found matching configuration for path: %s\n", targetConfig.Path)

	// 4. Get the list of windows before launching the new terminal.
	windowsBefore, err := getWindowList()
	if err != nil {
		fmt.Println("Failed to get initial window list:", err)
		return
	}

	// 5. Launch mate-terminal.
	geomString := targetConfig.Geometry
	cmd := exec.Command("mate-terminal", "--geometry", geomString)
	if err := cmd.Start(); err != nil {
		fmt.Println("Failed to start mate-terminal:", err)
		return
	}
	fmt.Printf("Launched mate-terminal with geometry %s\n", geomString)

	// 6. Find the new window by comparing the window lists.
	var newWindowID string
	for i := 0; i < 10; i++ {
		time.Sleep(500 * time.Millisecond)
		windowsAfter, err := getWindowList()
		if err != nil {
			fmt.Println("Failed to get updated window list:", err)
			continue
		}
		newWindowID = findNewWindow(windowsBefore, windowsAfter)
		if newWindowID != "" {
			break
		}
	}

	if newWindowID == "" {
		fmt.Println("Could not find the new terminal window.")
		return
	}
	fmt.Printf("Found new window with ID: %s\n", newWindowID)

	// 7. Move the window to the correct workspace.
	cmd = exec.Command("wmctrl", "-i", "-r", newWindowID, "-t", targetConfig.Workspace)
	if err := cmd.Run(); err != nil {
		fmt.Println("Failed to move window to workspace:", err)
	} else {
		fmt.Printf("Moved window to workspace %s\n", targetConfig.Workspace)
	}

	// 8. Set the final window title.
	finalTitle := fmt.Sprintf("jcarr@framebook: %s", pwd)
	cmd = exec.Command("wmctrl", "-i", "-r", newWindowID, "-T", finalTitle)
	if err := cmd.Run(); err != nil {
		fmt.Println("Failed to set final window title:", err)
	} else {
		fmt.Println("Window setup complete.")
	}
}