-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.go
219 lines (191 loc) · 4.35 KB
/
project.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"os/exec"
"sort"
"strings"
"time"
"github.com/shimmerglass/i3-workspace-manager/i3"
)
const winSpawnTimeout = 5 * time.Second
func (m *Manager) OpenProject(project string) error {
log.Printf("opening project %s", project)
err := m.setupProject(project)
if err != nil {
return err
}
for _, cfg := range m.Workspaces {
wk, ok, err := m.ProjectWks(project, cfg.Display)
if err != nil {
return err
}
if ok {
hasWindows := i3.WorkspaceHasWindows(wk.Name)
if !wk.Visible || !hasWindows {
err := i3.SwitchToWorkspace(wk.Name)
if err != nil {
return err
}
}
if !hasWindows {
err = m.openProjectDisplay(project, cfg)
if err != nil {
return err
}
}
} else {
err := m.openProjectCreateWks(project, cfg)
if err != nil {
return err
}
err = m.openProjectDisplay(project, cfg)
if err != nil {
return err
}
}
}
return nil
}
func (m *Manager) openProjectCreateWks(project string, cfg Workspace) error {
wkn, err := m.nextWorkspacesID()
if err != nil {
return err
}
activeWks, err := m.displayActiveWorkspace(cfg.Display)
if err != nil {
return err
}
if activeWks != "" {
err = i3.SwitchToWorkspace(activeWks)
if err != nil {
return err
}
}
userWksName, err := m.workspaceName(project)
if err != nil {
return err
}
wksName := encodeWorkspaceName(userWksName, wksInfo{
Project: project,
Display: cfg.Display,
})
err = i3.SwitchToWorkspace(fmt.Sprintf("%d:%s", wkn, wksName))
if err != nil {
return err
}
if activeWks == "" {
err = i3.MoveCurrentWorkspace(cfg.Display)
if err != nil {
return err
}
}
return nil
}
func (m *Manager) openProjectDisplay(project string, cfg Workspace) error {
winEvts, done := i3.WinEvents()
defer done()
err := m.runOpenCommand(project, cfg.Command)
if err != nil {
return err
}
timeout := time.NewTimer(winSpawnTimeout)
WaitWindow:
for {
select {
case ev := <-winEvts:
log.Printf("i3 win event: %+v", ev)
if ev.Change == "new" {
break WaitWindow
}
case <-timeout.C:
return fmt.Errorf("command %q did not spawn any new window after %s", cfg.Command, winSpawnTimeout)
}
}
return nil
}
func (m *Manager) runOpenCommand(project, command string) error {
cmd := exec.Command("/bin/sh", "-c", command)
env := os.Environ()
out := &bytes.Buffer{}
cmd.Stderr = out
cmd.Stdout = out
env = append(env, fmt.Sprintf("PROJECT_NAME=%s", project))
cmd.Env = env
err := cmd.Start()
if err != nil {
if out.Len() > 0 {
return fmt.Errorf("error opening workspace with command %s: %s", command, out.String())
}
return fmt.Errorf("error setting up workspace: %s", err)
}
return cmd.Process.Release()
}
func (m *Manager) setupProject(name string) error {
if m.SetupCommand == "" {
return nil
}
cmd := exec.Command("/bin/sh", "-c", m.SetupCommand)
env := os.Environ()
stderr := &bytes.Buffer{}
cmd.Stderr = stderr
env = append(env, fmt.Sprintf("PROJECT_NAME=%s", name))
cmd.Env = env
err := cmd.Run()
if err != nil {
if len(stderr.Bytes()) > 0 {
return fmt.Errorf(string(stderr.Bytes()))
}
return fmt.Errorf("error setting up project: %s", err)
}
return nil
}
func (m *Manager) workspaceName(name string) (string, error) {
if m.WksNameCommand == "" {
return name, nil
}
cmd := exec.Command("/bin/sh", "-c", m.WksNameCommand)
env := os.Environ()
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
cmd.Stderr = stderr
cmd.Stdout = stdout
env = append(env, fmt.Sprintf("PROJECT_NAME=%s", name))
cmd.Env = env
err := cmd.Run()
if err != nil {
if len(stderr.Bytes()) > 0 {
return "", fmt.Errorf(string(stderr.Bytes()))
}
return "", fmt.Errorf("error getting workspace name: %s", err)
}
return strings.TrimSpace(stdout.String()), nil
}
func (m *Manager) ProjectList() ([]string, error) {
cmd := exec.Command("/bin/sh", "-c", m.ListCommand)
stderr := &bytes.Buffer{}
stdout := &bytes.Buffer{}
cmd.Stderr = stderr
cmd.Stdout = stdout
err := cmd.Run()
if err != nil {
if len(stderr.Bytes()) > 0 {
return nil, fmt.Errorf(string(stderr.Bytes()))
}
return nil, fmt.Errorf("error listing projects: %s", err)
}
res := []string{}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
p := scanner.Text()
if p == "" {
continue
}
res = append(res, strings.TrimSpace(p))
}
sort.Strings(res)
return res, nil
}