-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
120 lines (104 loc) · 2.62 KB
/
monitor.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
package bspc
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strconv"
)
type Monitor struct {
Name string
Desktops []*Desktop
}
func Monitors() ([]*Monitor, error) {
out, err := exec.Command(CommandName, "query", "--monitors").Output()
if err != nil {
return nil, err
}
names := bytes.Split(out, []byte("\n"))
// Last entry is blank
monitors := make([]*Monitor, len(names)-1)
for i, name := range names[:len(names)-1] {
monitors[i] = &Monitor{Name: string(name)}
err = monitors[i].loadDesktops()
if err != nil {
return nil, err
}
}
return monitors, nil
}
func (m *Monitor) loadDesktops() error {
out, err := exec.Command(CommandName, "query", "--monitor", m.Name, "--desktops").Output()
if err != nil {
return err
}
names := bytes.Split(out, []byte("\n"))
// Last entry is blank
desktops := make([]*Desktop, len(names)-1)
for i, name := range names[:len(names)-1] {
desktops[i] = &Desktop{Name: string(name)}
err = desktops[i].loadWindows()
if err != nil {
return err
}
}
m.Desktops = desktops
return nil
}
func (m *Monitor) Rename(newName string) error {
err := exec.Command(CommandName, "monitor", m.Name, "--rename", newName).Run()
if err != nil {
return err
}
m.Name = newName
return nil
}
func (m *Monitor) AddDesktops(desktops ...string) error {
// TODO: Refactor into one shell call
for _, desktop := range desktops {
err := exec.Command(CommandName, "monitor", m.Name, "--add-desktops", desktop).Run()
if err != nil {
return err
}
m.Desktops = append(m.Desktops, &Desktop{Name: desktop})
}
return nil
}
func (m *Monitor) DesktopByIndex(index int) (*Desktop, error) {
if index < 0 {
return nil, errors.New("Need to enter a non-negative index")
}
if index >= len(m.Desktops) {
return nil, errors.New("Invalid index")
}
return m.Desktops[index], nil
}
func (m *Monitor) RemoveEmptyDesktops() error {
for i, desktop := range m.Desktops {
if len(desktop.Windows) == 0 {
err := m.RemoveDesktopByIndex(i + 1)
if err != nil {
return err
}
}
}
return nil
}
func (m *Monitor) RemoveDesktopByIndex(index int) error {
fmt.Println("Removing desktop", index)
err := exec.Command(CommandName, "desktop", m.Name+":^"+strconv.Itoa(index), "--remove").Run()
return err
}
func (m *Monitor) DefragDesktops() error {
for i := range m.Desktops {
err := exec.Command(CommandName, "desktop", m.Name+":^"+strconv.Itoa(i), "--rename", string(i+1)).Run()
if err != nil {
return err
}
}
return nil
}
func (m *Monitor) FocusDesktopByIndex(index int) error {
err := exec.Command(CommandName, "desktop", m.Name+":^"+strconv.Itoa(index), "--focus").Run()
return err
}