-
Notifications
You must be signed in to change notification settings - Fork 0
/
bspc.go
98 lines (83 loc) · 1.88 KB
/
bspc.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
package bspc
import (
"bytes"
"errors"
"os/exec"
)
type Controller struct {
Monitors []*Monitor
}
func NewController() (*Controller, error) {
c := new(Controller)
monitors, err := Monitors()
if err != nil {
return nil, err
}
c.Monitors = monitors
return c, nil
}
func (c *Controller) MonitorByIndex(index int) (*Monitor, error) {
if index < 0 {
return nil, errors.New("Need to enter a non-negative index")
}
if index >= len(c.Monitors) {
return nil, errors.New("Invalid index")
}
return c.Monitors[index], nil
}
func (c *Controller) MonitorByName(name string) (*Monitor, error) {
for _, monitor := range c.Monitors {
if monitor.Name == name {
return monitor, nil
}
}
return nil, errors.New("Monitor not found")
}
func (c *Controller) DesktopByName(name string) (*Desktop, error) {
for _, monitor := range c.Monitors {
for _, desktop := range monitor.Desktops {
if desktop.Name == name {
return desktop, nil
}
}
}
return nil, errors.New("Desktop not found")
}
func (c *Controller) WindowByID(id string) (*Window, error) {
for _, monitor := range c.Monitors {
for _, desktop := range monitor.Desktops {
for _, window := range desktop.Windows {
if window.ID == id {
return window, nil
}
}
}
}
return nil, errors.New("Window not found")
}
func (c *Controller) FocusedMonitor() (*Monitor, error) {
out, err := exec.Command(CommandName, "query", "--monitor", "focused", "--monitors").Output()
out = bytes.TrimSpace(out)
if err != nil {
return nil, err
}
return c.MonitorByName(string(out))
}
func (c *Controller) RemoveEmptyDesktops() error {
for _, monitor := range c.Monitors {
err := monitor.RemoveEmptyDesktops()
if err != nil {
return err
}
}
return nil
}
func (c *Controller) Defrag() error {
for _, monitor := range c.Monitors {
err := monitor.DefragDesktops()
if err != nil {
return err
}
}
return nil
}