-
Notifications
You must be signed in to change notification settings - Fork 4
/
sway.go
86 lines (74 loc) · 1.29 KB
/
sway.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
package main
import (
"context"
"log"
"os"
"github.com/joshuarubin/go-sway"
)
type WSEHandler struct {
sway.EventHandler
s Sway
}
func (h WSEHandler) Workspace(ctx context.Context, evt sway.WorkspaceEvent) {
var active = 0
ws, err := h.s.getWorkspaces()
if err != nil {
log.Fatal(err)
}
for _, w := range ws {
if w.IsActive {
active = w.ID
}
}
w := Workspaces{
Active: active,
Workspaces: ws,
}
w.toJson()
}
type Sway struct {
client sway.Client
}
func (s Sway) detect() bool {
return os.Getenv("SWAYSOCK") != ""
}
func (s Sway) listen() error {
ctx := context.Background()
client, err := sway.New(ctx)
if err != nil {
log.Fatal(err)
}
s.client = client
var active = 0
ws, err := s.getWorkspaces()
if err != nil {
return err
}
for _, w := range ws {
if w.IsActive {
active = w.ID
}
}
w := Workspaces{
Active: active,
Workspaces: ws,
}
w.toJson()
h := WSEHandler{
s: s,
}
return sway.Subscribe(ctx, h, sway.EventTypeWorkspace)
}
func (s Sway) getWorkspaces() ([]Workspace, error) {
ws := []Workspace{}
swayWs, err := s.client.GetWorkspaces(context.Background())
for _, w := range swayWs {
ws = append(ws, Workspace{
IsActive: w.Focused,
ID: int(w.Num),
Name: w.Name,
Monitor: w.Output,
})
}
return ws, err
}