-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
450 lines (380 loc) · 9.19 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package main
import (
"bufio"
"errors"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/jawher/mow.cli"
"github.com/op/go-logging"
"github.com/proxypoke/i3ipc"
)
var log = logging.MustGetLogger("x3")
var ErrWorkspaceNotFound = errors.New("No workspace found")
var ErrArgParseFailed = errors.New("Failed to parse args")
type Direction string
type Orientation string
type Layout string
const (
Left = Direction("left")
Right = Direction("right")
Up = Direction("up")
Down = Direction("down")
Horizontal = Orientation("horizontal")
Vertical = Orientation("vertical")
Default = Layout("default")
Tabbed = Layout("tabbed")
Stacking = Layout("stacking")
)
func inverse(d Direction) Direction {
switch d {
case Left:
return Right
case Right:
return Left
case Up:
return Down
case Down:
return Up
}
return Direction("")
}
func getStdin(cliArgs []string) []string {
fi, _ := os.Stdin.Stat()
if fi.Mode()&os.ModeNamedPipe != 0 {
reader := bufio.NewReader(os.Stdin)
res, err := reader.ReadString('\n')
if err != nil {
return cliArgs
}
res = strings.TrimSpace(res)
for _, arg := range strings.Split(res, " ") {
cliArgs = append(cliArgs, arg)
}
}
return cliArgs
}
func main() {
x3 := cli.App("x3", "XMonad workspace handling and more for i3-wm")
x3.Version("v version", "0.1")
debug := x3.Bool(cli.BoolOpt{
Name: "debug",
Value: false,
Desc: "Enable debug logs",
})
x3.Before = func() {
if *debug {
backend := logging.NewLogBackend(os.Stderr, "", 0)
backendLevel := logging.AddModuleLevel(backend)
backendLevel.SetLevel(logging.DEBUG, "")
logging.SetBackend(backend)
}
}
x3.Command("show", "Show or create workspace on focused screen", func(cmd *cli.Cmd) {
wsName := cmd.StringArg("WSNAME", "", "Workspace name")
cmd.Action = func() {
Show(*wsName)
}
})
x3.Command("rename", "Rename current workspace", func(cmd *cli.Cmd) {
wsName := cmd.StringArg("WSNAME", "", "New workspace name")
cmd.Action = func() {
Rename(*wsName)
}
})
x3.Command("bind", "Bind current workspace to num", func(cmd *cli.Cmd) {
num := cmd.StringArg("NUM", "", "")
cmd.Action = func() {
Bind(*num)
}
})
x3.Command("swap", "Swap visible workspaces when there is 2 screens", func(cmd *cli.Cmd) {
cmd.Action = func() {
Swap()
}
})
x3.Command("list", "List all workspace names", func(cmd *cli.Cmd) {
cmd.Action = func() {
List()
}
})
x3.Command("current", "Current workspace name", func(cmd *cli.Cmd) {
cmd.Action = func() {
Current()
}
})
x3.Command("move", "Move current container to workspace", func(cmd *cli.Cmd) {
ws := cmd.StringArg("NUM_OR_NAME", "", "")
cmd.Action = func() {
Move(*ws)
}
})
x3.Command("merge", "Merge current container into other container", func(cmd *cli.Cmd) {
d := cmd.StringArg("DIRECTION", "", "The direction where to merge (left/right/up/down)")
o := cmd.StringArg("ORIENTATION", "", "Split mode (horizontal/vertical)")
l := cmd.StringArg("LAYOUT", "", "Layout type to use (default/tabbed/stacking)")
cmd.Action = func() {
Merge(Direction(*d), Orientation(*o), Layout(*l))
}
})
args := getStdin(os.Args)
x3.Run(args)
}
type I3WS []i3ipc.Workspace
func (ws I3WS) Len() int {
return len(ws)
}
func (ws I3WS) Swap(i, j int) {
ws[i], ws[j] = ws[j], ws[i]
}
func (ws I3WS) Less(i, j int) bool {
if ws[i].Num != -1 {
return ws[i].Num < ws[j].Num
} else {
return ws[i].Name < ws[j].Name
}
}
type I3 struct {
s *i3ipc.IPCSocket
workspaces []i3ipc.Workspace
chain *I3CmdChain
}
func (i3 *I3) RunChain() {
cmd := strings.Join(*i3.chain, ";")
log.Debugf("Run: %s", cmd)
i3.s.Command(cmd)
}
func (i3 *I3) GetWSNum(num int32) (i3ipc.Workspace, error) {
for _, ws := range i3.workspaces {
if ws.Num == num {
return ws, nil
}
}
return i3ipc.Workspace{}, ErrWorkspaceNotFound
}
func (i3 *I3) GetWSName(name string) (i3ipc.Workspace, error) {
for _, ws := range i3.workspaces {
if strings.Contains(ws.Name, name) {
return ws, nil
}
}
return i3ipc.Workspace{}, ErrWorkspaceNotFound
}
func (i3 *I3) GetWS(nameOrNum string) (i3ipc.Workspace, error) {
var ws i3ipc.Workspace
num, err := strconv.ParseInt(nameOrNum, 10, 32)
if err != nil {
ws, err = i3.GetWSName(nameOrNum)
} else {
ws, err = i3.GetWSNum(int32(num))
}
if err != nil {
return i3ipc.Workspace{}, err
}
return ws, nil
}
func (i3 *I3) CurrentWS() (i3ipc.Workspace, error) {
for _, ws := range i3.workspaces {
if ws.Focused == true {
return ws, nil
}
}
return i3ipc.Workspace{}, ErrWorkspaceNotFound
}
func (i3 *I3) OutputWS(output string) (i3ipc.Workspace, error) {
for _, ws := range i3.workspaces {
if ws.Visible && ws.Output == output {
return ws, nil
}
}
return i3ipc.Workspace{}, ErrWorkspaceNotFound
}
func (i3 *I3) ActiveOutputs() ([]i3ipc.Output, error) {
var active []i3ipc.Output
outputs, err := i3.s.GetOutputs()
if err == nil {
for _, o := range outputs {
if o.Active {
active = append(active, o)
}
}
}
return active, err
}
type I3CmdChain []string
func (c *I3CmdChain) Add(cmd string) {
log.Debugf("Add cmd: %s", cmd)
*c = append(*c, cmd)
}
func (c *I3CmdChain) ShowWS(ws i3ipc.Workspace) {
c.Add("workspace " + string(ws.Name))
}
func (c *I3CmdChain) RenameWS(wsName string) {
c.Add("rename workspace to " + wsName)
}
func (c *I3CmdChain) MoveWSToOuput(output string) {
c.Add("move workspace to output " + output)
}
func (c *I3CmdChain) FocusOutput(output string) {
c.Add("focus output " + output)
}
func (c *I3CmdChain) SwapWS(ws1 i3ipc.Workspace, ws2 i3ipc.Workspace) {
c.MoveWSToOuput(ws2.Output)
c.ShowWS(ws2)
c.MoveWSToOuput(ws1.Output)
c.FocusOutput(ws1.Output)
}
func (c *I3CmdChain) ShowWSOnOutput(ws i3ipc.Workspace, output string) {
c.ShowWS(ws)
if ws.Output != output {
c.MoveWSToOuput(output)
}
}
func (c *I3CmdChain) MoveContainerToWS(wsName string) {
c.Add("move container to workspace " + wsName)
}
func (c *I3CmdChain) FocusContainer(d Direction) {
c.Add(fmt.Sprintf("focus %s", d))
}
func (c *I3CmdChain) SplitContainer(o Orientation) {
c.Add(fmt.Sprintf("split %s", o))
}
func (c *I3CmdChain) MoveContainer(d Direction) {
c.Add(fmt.Sprintf("move %s", d))
}
func (c *I3CmdChain) ChangeLayout(l Layout) {
c.Add(fmt.Sprintf("layout %s", l))
}
func WSName(ws i3ipc.Workspace) string {
var name string
splitName := strings.Split(ws.Name, ":")
if len(splitName) > 1 {
name = splitName[1]
} else {
name = ws.Name
}
return name
}
func Init() I3 {
socket, _ := i3ipc.GetIPCSocket()
workspaces, _ := socket.GetWorkspaces()
chain := I3CmdChain{}
return I3{s: socket, workspaces: workspaces, chain: &chain}
}
func Show(wsName string) {
i3 := Init()
targetWS, err := i3.GetWS(wsName)
if err != nil {
i3.chain.ShowWS(i3ipc.Workspace{Name: wsName})
i3.RunChain()
return
}
currentWS, _ := i3.CurrentWS()
if currentWS == targetWS {
return
}
if currentWS.Visible && targetWS.Visible {
i3.chain.SwapWS(currentWS, targetWS)
} else {
// bring workspace to output
i3.chain.ShowWSOnOutput(targetWS, currentWS.Output)
// make WS history correct
i3.chain.ShowWS(currentWS)
i3.chain.ShowWS(targetWS)
}
i3.chain.FocusOutput(currentWS.Output)
i3.RunChain()
}
func Swap() {
i3 := Init()
outputs, _ := i3.ActiveOutputs()
if len(outputs) != 2 {
return
}
ws1, _ := i3.GetWS(outputs[0].Current_Workspace)
ws2, _ := i3.GetWS(outputs[1].Current_Workspace)
if ws1.Focused {
i3.chain.SwapWS(ws1, ws2)
} else {
i3.chain.SwapWS(ws2, ws1)
}
i3.RunChain()
}
func List() {
i3 := Init()
names := make([]string, len(i3.workspaces))
sort.Sort(I3WS(i3.workspaces))
for i, ws := range i3.workspaces {
names[i] = ws.Name
}
fmt.Printf(strings.Join(names, "\n"))
}
func Current() {
i3 := Init()
ws, _ := i3.CurrentWS()
fmt.Printf(WSName(ws))
}
func Rename(wsName string) {
i3 := Init()
ws, _ := i3.CurrentWS()
if ws.Num != -1 {
wsName = strconv.Itoa(int(ws.Num)) + ":" + wsName
}
i3.chain.RenameWS(wsName)
i3.RunChain()
}
func Bind(wsNum string) {
i3 := Init()
currentWS, _ := i3.CurrentWS()
currentName := WSName(currentWS)
currentNum := strconv.Itoa(int(currentWS.Num))
if currentNum == wsNum {
return
}
otherWS, err := i3.GetWS(wsNum)
if err == nil {
otherName := WSName(otherWS)
i3.chain.ShowWS(otherWS)
// num to bind for the other WS
if currentNum != "-1" {
i3.chain.RenameWS(currentNum + ":" + otherName)
} else {
i3.chain.RenameWS(otherName)
}
// restore other output WS if needed
if otherWS.Output != currentWS.Output {
otherOutputWS, err := i3.OutputWS(otherWS.Output)
if err == nil && otherOutputWS != otherWS {
i3.chain.ShowWS(otherOutputWS)
}
}
i3.chain.ShowWS(currentWS)
} else {
fmt.Printf("%s\n", err)
}
i3.chain.RenameWS(wsNum + ":" + currentName)
i3.chain.FocusOutput(currentWS.Output)
i3.RunChain()
}
func Move(wsName string) {
i3 := Init()
ws, err := i3.GetWS(wsName)
// new workspace
if err != nil {
i3.chain.MoveContainerToWS(wsName)
} else {
i3.chain.MoveContainerToWS(ws.Name)
}
i3.RunChain()
}
func Merge(d Direction, o Orientation, l Layout) {
i3 := Init()
i3.chain.FocusContainer(d)
i3.chain.SplitContainer(o)
i3.chain.FocusContainer(inverse(d))
i3.chain.MoveContainer(d)
i3.chain.ChangeLayout(l)
i3.RunChain()
}