-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
152 lines (138 loc) · 3.67 KB
/
commands.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"log"
"net"
)
type StateManager interface {
SetActive(args ...string)
ActiveWindow() *Window
Filter(field string, value any) map[string]*Window
Client() Executor
IsPinned(addr string) bool
SavePinned(addr string)
RemovePinned(addr string)
}
type Command func(StateManager, ...string)
var EnabledCmmands map[string]Command = map[string]Command{
"minimize": Minimize,
"restore": Restore,
"fullscreen": ForceFullScreen,
"float": ToggleFloating,
"pin": Pin,
}
var hyprctlCmds map[string]string = map[string]string{
"active": "hyprctl dispatch activewindow",
"minimize": "hyprctl dispatch movetoworkspacesilent special hyprman,address:%v",
"restore": "hyprctl dispatch movetoworkspacesilent %v,address:%v",
"fullscreen": "hyprctl dispatch fullscreen %v address:%v",
"pin": "hyprctl dispatch pin address:%v",
"float": "hyprctl dispatch togglefloating",
}
func availableCmds() string {
s := ""
for cmd := range EnabledCmmands {
s = s + cmd + "\n"
}
return s
}
// toggles fullscreen of type t for window at address addr.
//
// t is one of "0" (real fullscreen) or "1" (monocle)
func toggleFull(e Executor, addr string, t string) error {
cmd := fmt.Sprintf(hyprctlCmds["fullscreen"], t, addr)
out, err := e.Exec(cmd)
if err != nil {
log.Fatal("Unable to toggle fullscreen", err)
}
fmt.Println("fullscreen output:", out)
return nil
}
// Provides a way for command functions to access state
// Sends active window to 'hyprman' workspace and remvoes pin attribte
func Minimize(s StateManager, _ ...string) {
w := s.ActiveWindow()
cmd := fmt.Sprintf(hyprctlCmds["minimize"], w.Address)
fmt.Println(cmd)
resp, err := s.Client().Exec(cmd)
if err != nil {
log.Println("Unable to minimize", err)
return
}
log.Println("Miminize command:", resp)
}
// Returns a minimized window to the workspace it was on and reattaches the pin attribute
func Restore(s StateManager, _ ...string) {
w := s.ActiveWindow()
cmd := fmt.Sprintf(hyprctlCmds["restore"], fmt.Sprint(w.Workspace), w.Address)
_, err := s.Client().Exec(cmd)
if err != nil {
log.Println("Unable to restore", err)
return
}
}
// Forces a window to become fullscreen even if it is pinned
func ForceFullScreen(s StateManager, args ...string) {
fscreenType := ""
if len(args) > 0 {
fscreenType = args[0]
}
c := s.Client()
w := s.ActiveWindow()
if !w.Fullscreen {
if w.Pinned {
fmt.Println("Unpinning")
s.SavePinned(w.Address)
Pin(s, w.Address)
}
toggleFull(c, w.Address, fscreenType)
w.Fullscreen = true
return
}
toggleFull(c, w.Address, fscreenType)
w.Fullscreen = false
if s.IsPinned(w.Address) {
fmt.Println("Re-pinning")
Pin(s, w.Address)
s.RemovePinned(w.Address)
return
}
}
// Toggles pinned status of the active window
func ToggleFloating(s StateManager, _ ...string) {
c := s.Client()
cmd := hyprctlCmds["float"]
resp, err := c.Exec(cmd)
if err != nil {
log.Println("unable to toggle floating:", err)
}
fmt.Println("Float response:", resp)
}
// Toggles pinned status of the active window or window with given `address`
func Pin(s StateManager, address ...string) {
var w *Window
addr := ""
if len(address) > 0 {
addr = address[0]
}
if addr == "" {
w = s.ActiveWindow()
} else {
w = s.Filter("address", addr)[addr]
}
c := s.Client()
cmd := fmt.Sprintf(hyprctlCmds["pin"], w.Address)
out, err := c.Exec(cmd)
if err != nil {
log.Println("Error pinning window:", out, err)
return
}
fmt.Println(addr, "is pinned?", w.Pinned)
}
func daemonConnect() net.Conn {
conn, err := net.Dial("unix", socFile)
if err != nil {
log.Fatal("Unable to connect to daemon. Is hyprmand running?")
}
return conn
}