-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (56 loc) · 1.43 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
package main
import (
"fmt"
"log"
"os"
"strconv"
"go.i3wm.org/i3/v4"
)
func ContainerByTabIndex(tc *i3.Node, index uint64) (*i3.Node, error) {
if index < 1 {
return nil, fmt.Errorf("tab index is %d, but must be 1 or greater", index)
}
highestTabIndex := uint64(len(tc.Nodes))
if index > highestTabIndex {
return nil, fmt.Errorf("tab index is %d, but highest index is %d", index, highestTabIndex)
}
return tc.Nodes[index-1], nil
}
func FindFocusedTabbedContainer() (*i3.Node, error) {
tree, err := i3.GetTree()
if err != nil {
return nil, err
}
return tree.Root.FindFocused(func(n *i3.Node) bool {
return n.Layout == i3.Tabbed
}), nil
}
func Focus(n *i3.Node) error {
if _, err := i3.RunCommand(fmt.Sprintf(`[con_id="%d"] focus`, n.ID)); err != nil {
return err
}
return nil
}
func main() {
if len(os.Args) != 2 {
log.Fatalf("usage: %s TABINDEX", os.Args[0])
}
index, err := strconv.ParseUint(os.Args[1], 10, 8)
if err != nil {
log.Fatalf("tab index must be a number: %q: %s", os.Args[1], err)
}
tc, err := FindFocusedTabbedContainer()
if err != nil {
log.Fatalf("failed to find focused tabbed container: %s", err)
}
if tc == nil {
log.Fatalf("focused container is not tabbed")
}
c, err := ContainerByTabIndex(tc, index)
if err != nil {
log.Fatalf("failed to find container by index: %s", err)
}
if err := Focus(c); err != nil {
log.Fatalf("could not focus container at tab index %d", index)
}
}