-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (90 loc) · 2.31 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
package main
import (
"encoding/json"
"math"
"net"
"os"
"os/signal"
"strings"
"swaystatus/swayipc"
"syscall"
"time"
)
func get_focused_name(root map[string]interface{}) string {
root_focus, _ := root["focus"].([]interface{})
root_nodes, _ := root["nodes"].([]interface{})
if len(root_focus) == 0 {
return root["name"].(string)
}
for _, node := range root_nodes {
if node.(map[string]interface{})["id"].(float64) == root_focus[0] {
return get_focused_name(node.(map[string]interface{}))
}
}
return ""
}
func get_time() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func get_title() string {
addr := swayipc.Getaddr()
conn := swayipc.Getsock(addr)
msg := swayipc.Pack(4, []byte(""))
conn.Write(msg)
var result map[string]interface{}
resp := swayipc.Unpack(conn)
json.Unmarshal(resp, &result)
conn.Close()
return get_focused_name(result)
}
func update_status_bar(title string, time string) {
var OFFSET int = 100
middle := int(math.RoundToEven(float64(len(title)) / 2))
space := strings.Repeat(" ", OFFSET-middle)
status := title + space + time + "\n"
os.Stdout.Write([]byte(status))
}
func poll_changes(title chan string) {
var addr string = swayipc.Getaddr()
var conn net.Conn = swayipc.Getsock(addr)
var events []string = []string{"window"}
swayipc.Subscribe(conn, events) // subscribe to window change events
defer conn.Close()
var result map[string]interface{}
for {
response := swayipc.Unpack(conn)
json.Unmarshal(response, &result)
if result["change"] == "focus" || result["change"] == "title" {
window, _ := result["container"].(map[string]interface{})
if window["name"] != nil{
title <- window["name"].(string)
}else{
title <- ""
}
}
}
}
func main() {
title_queue := make(chan string)
interrupt := make(chan os.Signal)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
tick := time.NewTicker(time.Second)
defer tick.Stop()
go poll_changes(title_queue)
var title string = get_title()
mainloop:
for {
select {
case <-tick.C:
current_time := get_time()
update_status_bar(title, current_time)
case title = <-title_queue:
current_time := get_time()
update_status_bar(title, current_time)
tick.Reset(time.Second)
case <-interrupt:
os.Stdout.Write([]byte("Closing..."))
break mainloop
}
}
}