-
Notifications
You must be signed in to change notification settings - Fork 0
/
seek.go
72 lines (62 loc) · 1.45 KB
/
seek.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
package desktops
import "github.com/thrisp/scpwm/euclid/branch"
type MatchDesktop func(Desktop) bool
func seek(desktops *branch.Branch, fn MatchDesktop) Desktop {
curr := desktops.Front()
for curr != nil {
desktop := curr.Value.(Desktop)
if match := fn(desktop); match {
return desktop
}
curr = curr.Next()
}
return nil
}
func isFocused(d Desktop) bool {
if d.Focused() {
return true
}
return false
}
func Focused(desktops *branch.Branch) Desktop {
return seek(desktops, isFocused)
}
func seekOffset(desktops *branch.Branch, fn MatchDesktop, offset int) Desktop {
curr := desktops.Front()
for curr != nil {
desktop := curr.Value.(Desktop)
if match := fn(desktop); match {
switch offset {
case -1:
desktop = curr.PrevContinuous().Value.(Desktop)
case 1:
desktop = curr.NextContinuous().Value.(Desktop)
}
return desktop
}
curr = curr.Next()
}
return nil
}
func Prev(desktops *branch.Branch) Desktop {
return seekOffset(desktops, isFocused, -1)
}
func Next(desktops *branch.Branch) Desktop {
return seekOffset(desktops, isFocused, 1)
}
func seekAny(desktops *branch.Branch, fn MatchDesktop) []Desktop {
var ret []Desktop
curr := desktops.Front()
for curr != nil {
dsk := curr.Value.(Desktop)
if match := fn(dsk); match {
ret = append(ret, dsk)
}
curr = curr.Next()
}
return ret
}
func All(desktops *branch.Branch) []Desktop {
fn := func(d Desktop) bool { return true }
return seekAny(desktops, fn)
}