-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
178 lines (159 loc) · 5.46 KB
/
Copy pathmain.go
File metadata and controls
178 lines (159 loc) · 5.46 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// hyprspacefix: bulk-assign Hyprland workspaces to a monitor.
//
// Emits either modern Lua-config dispatchers (Hyprland 0.55+) or the legacy
// hyprlang syntax, auto-selected from `hyprctl version` (override with --syntax).
//
// Usage: hyprspacefix --name=DP-1 --range=1-5 [--syntax=auto|lua|hyprlang]
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
)
// luaConfigMinMinor is the first 0.x minor release where Hyprland's Lua config
// (and the new dispatcher syntax) became the default.
const luaConfigMinMinor = 55
// checkHyprctl verifies that hyprctl is available.
func checkHyprctl() error {
if _, err := exec.LookPath("hyprctl"); err != nil {
return fmt.Errorf("hyprctl not found in PATH: %v", err)
}
return nil
}
var versionRe = regexp.MustCompile(`(\d+)\.(\d+)\.(\d+)`)
// detectLuaSyntax reports whether the running Hyprland expects the Lua-config
// dispatcher syntax, by parsing `hyprctl version`. Also returns the parsed
// version string for logging.
func detectLuaSyntax() (useLua bool, version string, err error) {
out, err := exec.Command("hyprctl", "version").CombinedOutput()
if err != nil {
return false, "", fmt.Errorf("running `hyprctl version`: %v", err)
}
m := versionRe.FindStringSubmatch(string(out))
if m == nil {
return false, "", fmt.Errorf("no version number found in `hyprctl version` output")
}
major, _ := strconv.Atoi(m[1])
minor, _ := strconv.Atoi(m[2])
version = m[1] + "." + m[2] + "." + m[3]
useLua = major > 0 || (major == 0 && minor >= luaConfigMinMinor)
return useLua, version, nil
}
// workspaceCommands returns the batch sub-commands that bind workspace ws to
// monitor name, in either Lua (0.55+) or legacy hyprlang syntax.
func workspaceCommands(ws int, name string, useLua, persistent bool) []string {
if useLua {
// A persistent rule both binds the workspace to the monitor and
// creates/places it there (the move dispatchers are gone/no-op on
// 0.55). Without persistence the rule only affects future creation;
// with it, existing workspaces get relocated too — at the cost of the
// workspace always existing, so some bars show it even when empty.
rule := fmt.Sprintf("eval hl.workspace_rule({ workspace = '%d', monitor = '%s'", ws, name)
if persistent {
rule += ", persistent = true"
}
rule += " })"
return []string{rule}
}
kw := fmt.Sprintf("keyword workspace %d,monitor:%s", ws, name)
if persistent {
kw += ",persistent:true"
}
return []string{
kw,
fmt.Sprintf("dispatch moveworkspacetomonitor %d %s", ws, name),
}
}
func main() {
monitorName := flag.String("name", "", "Monitor name")
workspaceRange := flag.String("range", "", "Workspace range (e.g., '1-5')")
syntax := flag.String("syntax", "auto", "Dispatcher syntax: auto|lua|hyprlang")
persistent := flag.Bool("persistent", true, "Make workspaces persistent (always exist). Required on 0.55+ to relocate already-existing workspaces; set =false for a cleaner bar if your real layout switches re-home without it.")
dryRun := flag.Bool("dry-run", false, "Print commands without executing them")
verbose := flag.Bool("verbose", false, "Enable verbose output")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s --name=monitor-name --range=start-end [options]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Example:\n")
fmt.Fprintf(os.Stderr, " %s --name=DP-1 --range=1-5\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
}
flag.Parse()
log.SetFlags(0)
if *verbose {
log.SetFlags(log.Ltime | log.Lmicroseconds)
}
if *monitorName == "" || *workspaceRange == "" {
flag.Usage()
os.Exit(1)
}
if err := checkHyprctl(); err != nil {
log.Fatal(err)
}
// Parse range.
rangeParts := strings.Split(*workspaceRange, "-")
if len(rangeParts) != 2 {
log.Fatal("Range must be in format 'n-m'")
}
start, err := strconv.Atoi(rangeParts[0])
if err != nil {
log.Fatal("Invalid start range:", err)
}
end, err := strconv.Atoi(rangeParts[1])
if err != nil {
log.Fatal("Invalid end range:", err)
}
// Select dispatcher syntax.
var useLua bool
switch strings.ToLower(*syntax) {
case "lua":
useLua = true
case "hyprlang", "legacy":
useLua = false
case "auto", "":
l, ver, derr := detectLuaSyntax()
if derr != nil {
log.Fatalf("could not auto-detect Hyprland version (use --syntax=lua|hyprlang to override): %v", derr)
}
useLua = l
if *verbose {
mode := "hyprlang"
if useLua {
mode = "lua"
}
log.Printf("detected Hyprland %s -> %s syntax", ver, mode)
}
default:
log.Fatalf("invalid --syntax %q (want auto|lua|hyprlang)", *syntax)
}
// Build batch commands.
var batchCmds []string
for ws := start; ws <= end; ws++ {
batchCmds = append(batchCmds, workspaceCommands(ws, *monitorName, useLua, *persistent)...)
}
batchCmd := strings.Join(batchCmds, " ; ")
if *dryRun {
fmt.Println("Would execute:")
// Double-quote the shell wrapper so the single-quoted Lua strings
// inside survive a copy-paste into the shell.
fmt.Printf("hyprctl --batch \"%s\"\n", batchCmd)
return
}
if *verbose {
log.Printf("Configuring workspaces %d-%d on monitor %s", start, end, *monitorName)
}
// No shell is involved here, so quotes in batchCmd are passed literally.
out, err := exec.Command("hyprctl", "--batch", batchCmd).CombinedOutput()
if err != nil {
log.Fatalf("Error executing batch commands: %v\nOutput: %s", err, out)
}
if *verbose {
log.Printf("Successfully configured %d workspaces", end-start+1)
}
}