-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspaces.go
202 lines (163 loc) · 3.23 KB
/
workspaces.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
package main
import (
"bytes"
"encoding/gob"
"sort"
"strings"
"github.com/eknkc/basex"
"github.com/shimmerglass/i3-workspace-manager/i3"
)
const (
workspaceStart = 20
)
var encoding *basex.Encoding
func init() {
e, err := basex.NewEncoding("\uFEFF\u200D")
if err != nil {
panic(err)
}
encoding = e
}
type wksInfo struct {
Project string
Display string
}
func encodeWorkspaceName(name string, info wksInfo) string {
buf := &bytes.Buffer{}
gob.NewEncoder(buf).Encode(info)
return name + "\u200B" + encoding.Encode(buf.Bytes())
}
func decodeWorkspaceName(wk i3.Workspace) (wksInfo, bool, error) {
res := wksInfo{}
parts := strings.Split(wk.Name, "\u200B")
if len(parts) != 2 {
return res, false, nil
}
bin, err := encoding.Decode(parts[1])
if err != nil {
return res, false, err
}
buf := bytes.NewBuffer(bin)
err = gob.NewDecoder(buf).Decode(&res)
if err != nil {
return res, false, err
}
return res, true, nil
}
func (m *Manager) ProjectWks(project, display string) (i3.Workspace, bool, error) {
wks, err := i3.Workspaces()
if err != nil {
return i3.Workspace{}, false, err
}
for _, w := range wks {
wkInfo, ok, err := decodeWorkspaceName(w)
if err != nil {
return i3.Workspace{}, false, err
}
if !ok {
continue
}
if wkInfo.Project != project {
continue
}
if w.Output != display {
continue
}
return w, true, nil
}
return i3.Workspace{}, false, nil
}
func (m *Manager) CurrentProject() (string, bool, error) {
wks, err := i3.Workspaces()
if err != nil {
return "", false, err
}
for _, w := range wks {
if !w.Visible {
continue
}
wkInfo, ok, err := decodeWorkspaceName(w)
if err != nil {
return "", false, err
}
if ok {
return wkInfo.Project, true, nil
}
}
return "", false, nil
}
func (m *Manager) IsProjectVisble(project string) (bool, error) {
i := 0
wks, err := i3.Workspaces()
if err != nil {
return false, err
}
for _, w := range wks {
if !w.Visible {
continue
}
wkInfo, ok, err := decodeWorkspaceName(w)
if err != nil {
return false, err
}
if ok && wkInfo.Project == project {
i++
}
}
return i == len(m.Workspaces), nil
}
func (m *Manager) OpenProjects() ([]string, error) {
pmap := map[string]bool{}
wks, err := i3.Workspaces()
if err != nil {
return nil, err
}
for _, w := range wks {
if !i3.WorkspaceHasWindows(w.Name) {
continue
}
wkInfo, ok, err := decodeWorkspaceName(w)
if err != nil {
return nil, err
}
if ok {
pmap[wkInfo.Project] = true
}
}
projects := []string{}
for k := range pmap {
projects = append(projects, k)
}
sort.Strings(projects)
return projects, nil
}
var minWorkspace = int64(workspaceStart)
func (m *Manager) nextWorkspacesID() (int64, error) {
wks, err := i3.Workspaces()
if err != nil {
return 0, err
}
usedNums := map[int64]bool{}
for _, w := range wks {
usedNums[w.Num] = true
}
for n := minWorkspace; ; n++ {
if usedNums[n] {
continue
}
minWorkspace = n + 1
return n, nil
}
}
func (m *Manager) displayActiveWorkspace(display string) (string, error) {
allWks, err := i3.Workspaces()
if err != nil {
return "", err
}
for _, w := range allWks {
if w.Visible && w.Output == display {
return w.Name, nil
}
}
return "", nil
}