-
Notifications
You must be signed in to change notification settings - Fork 8
/
i3icons.go
146 lines (134 loc) · 3.72 KB
/
i3icons.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
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"strings"
"github.com/nwhirschfeld/i3ipc"
)
func main() {
usr, err := user.Current()
if err != nil {
log.Fatal( err )
}
// handle command line arguments
var configFileName = flag.String("c", usr.HomeDir + "/.config/i3icons/i3icons2.config", "config file")
var verbose = flag.Bool("v", false, "verbose")
flag.Parse()
// Open our configFile
configFile, err := os.Open(*configFileName)
if err != nil {
fmt.Println(err)
flag.Usage()
os.Exit(1)
}
defer configFile.Close()
// read our config File and write to hash map
byteValue, _ := ioutil.ReadAll(configFile)
configLines := strings.Split(string(byteValue), "\n")
config := make(map[string]string)
for _, ci := range configLines {
p := strings.Split(string(ci), "=")
if len(p) == 2 {
config[p[0]] = p[1]
}
}
// open I3IPC socket and subscribe to window events
ipcsocket, _ := i3ipc.GetIPCSocket()
channel, err := i3ipc.Subscribe(i3ipc.I3WindowEvent)
EventLoop(channel, ipcsocket, config, *verbose)
}
// SubNodeByName gets the subnode of an I3Node by name
func SubNodeByName(node *i3ipc.I3Node, name string) (root i3ipc.I3Node, err error) {
if strings.Compare(node.Name, name) == 0 {
return *node, nil
}
for _, arm := range node.Nodes {
res, err := SubNodeByName(&arm, name)
if err == nil {
return res, err
}
}
return i3ipc.I3Node{}, errors.New("no such Node")
}
// SubNodesWithoutName gets the subnodes of an I3Node which doesn't match to a specific name
func SubNodesWithoutName(node *i3ipc.I3Node, name string) (nodes []i3ipc.I3Node, err error) {
result := make([]i3ipc.I3Node, len(node.Nodes))
for i, item := range node.Nodes {
if strings.Compare(item.Name, name) != 0 {
result[i] = item
}
}
return result, nil
}
// FlattenNode gets the ends of the trees
func FlattenNode(node *i3ipc.I3Node) (nodes []i3ipc.I3Node, err error) {
if len(node.Nodes) == 0 {
result := make([]i3ipc.I3Node, 1)
result[0] = *node
return result, nil
}
result := make([]i3ipc.I3Node, 0)
for _, item := range node.Nodes {
oldnodes := result
newnodes, _ := FlattenNode(&item)
result = make([]i3ipc.I3Node, len(oldnodes)+len(newnodes))
for i, item := range oldnodes {
result[i] = item
}
for i, item := range newnodes {
result[i+len(oldnodes)] = item
}
}
return result, nil
}
// EventLoop - main event loop
func EventLoop(events chan i3ipc.Event, ipcsocket *i3ipc.IPCSocket, config map[string]string, verbose bool) {
for range events {
tree, _ := ipcsocket.GetTree()
screens, _ := SubNodesWithoutName(&tree, "__i3")
for _, screen := range screens {
wss, _ := SubNodeByName(&screen, "content")
for _, ws := range wss.Nodes {
name := ws.Name
number := strings.Split(name, " ")[0]
windows, _ := FlattenNode(&ws)
newname := number
windownames := make([]string, len(windows))
for i, win := range windows {
winname:=strings.ToLower(win.WindowProperties.Class)
if(verbose){
fmt.Println(winname)
}
// rename window to config item, if present
if val, ok := config[winname]; ok {
winname = val
} else if len(winname) > 7 {
winname = winname[:4] + ".." + winname[len(winname)-3:]
}
// check if workspace name already contains window title
choose := true
for _, n := range windownames {
if strings.Compare(n, winname) == 0 {
choose = false
}
}
if choose {
windownames[i] = winname
}
}
// rename workspace
for _, windowname := range windownames {
if(len(windowname) > 0) {
newname = fmt.Sprintf("%s %s", newname, windowname)
}
}
ipcsocket.Command("rename workspace \"" + name + "\" to " + newname)
}
}
}
}