-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (115 loc) · 2.66 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
package main
import (
"fmt"
"os"
"time"
i3 "go.i3wm.org/i3/v4"
)
//sizes of container for snap
var sizes = map[string]string{
"left": "50ppt 100ppt",
"right": "50ppt 100ppt",
"top": "100ppt 50ppt",
"bottom": "100ppt 50ppt",
}
//start positions of container for snap direction
var positions = map[string]string{
"left": "0ppt 0ppt",
"right": "50ppt 0ppt",
"top": "0ppt 0ppt",
"bottom": "0ppt 50ppt",
}
func last(index int, len int) int {
if index == 0 {
return len - 1
}
return index - 1
}
func next(index int, len int) int {
if index == len-1 {
return 0
}
return index + 1
}
func traverseNodes(parent *i3.Node) (nodes []*i3.Node) {
if parent.Window != 0 {
nodes = append(nodes, parent)
}
for _, node := range parent.Nodes {
nodes = append(nodes, traverseNodes(node)...)
}
for _, node := range parent.FloatingNodes {
nodes = append(nodes, traverseNodes(node)...)
}
return
}
func getWindowNodes(tree i3.Tree) []*i3.Node {
ws := tree.Root.FindFocused(func(n *i3.Node) bool {
return n.Type == i3.WorkspaceNode
})
return traverseNodes(ws)
}
func snap(tree i3.Tree, dir string) {
focused := tree.Root.FindChild(func(m *i3.Node) bool {
return m.Focused == true
})
if !focused.IsFloating() {
i3.RunCommand("floating toggle")
}
i3.RunCommand("resize set " + sizes[dir])
i3.RunCommand("move position " + positions[dir])
}
func focus(tree i3.Tree, direction string) {
focusedIndex, fullScreen := 0, 0
windowNodes := getWindowNodes(tree)
windowCount := len(windowNodes)
for i, node := range windowNodes {
if node.Focused {
focusedIndex = i
fullScreen = int(node.FullscreenMode)
}
}
if fullScreen >= 1 {
i3.RunCommand("fullscreen toggle")
}
if direction == "prev" {
i3.RunCommand(fmt.Sprintf("[id=%d] focus", windowNodes[last(focusedIndex, windowCount)].Window))
return
}
i3.RunCommand(fmt.Sprintf("[id=%d] focus", windowNodes[next(focusedIndex, windowCount)].Window))
}
func peek(tree i3.Tree, timeout string) {
var focusedId int64
focusedIndex := 0
windowNodes := getWindowNodes(tree)
windowCount := len(windowNodes)
for i, node := range windowNodes {
if node.Focused {
focusedIndex = i
focusedId = node.Window
}
}
t, err := time.ParseDuration(timeout)
if err != nil {
fmt.Println(err)
return
}
for nextId := 0; int64(nextId) != focusedId; time.Sleep(t) {
focusedIndex = next(focusedIndex, windowCount)
nextId = int(windowNodes[focusedIndex].Window)
i3.RunCommand(fmt.Sprintf("[id=%d] focus", nextId))
}
}
func main() {
command := os.Args[1]
arg := os.Args[2]
tree, _ := i3.GetTree()
switch command {
case "snap":
snap(tree, arg)
case "focus":
focus(tree, arg)
case "peek":
peek(tree, arg)
}
}