mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-06-08 12:13:31 -04:00
refactor(Hyprland): Update Lua migration and keybind writes
- emit native hl.dsp.* dispatchers for generated Lua keybinds - keep legacy hyprland.conf installs read-only but preserved until dms setup migration
This commit is contained in:
@@ -68,6 +68,8 @@ func (h *HyprlandProvider) GetCheatSheet() (*keybinds.CheatSheet, error) {
|
||||
Effective: result.DMSStatus.Effective,
|
||||
OverriddenBy: result.DMSStatus.OverriddenBy,
|
||||
StatusMessage: result.DMSStatus.StatusMessage,
|
||||
ConfigFormat: result.DMSStatus.ConfigFormat,
|
||||
ReadOnly: result.DMSStatus.ReadOnly,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,6 +221,9 @@ func (h *HyprlandProvider) validateAction(action string) error {
|
||||
}
|
||||
|
||||
func (h *HyprlandProvider) SetBind(key, action, description string, options map[string]any) error {
|
||||
if err := h.ensureWritableConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := h.validateAction(action); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -242,9 +247,10 @@ func (h *HyprlandProvider) SetBind(key, action, description string, options map[
|
||||
}
|
||||
}
|
||||
|
||||
normalizedKey := strings.ToLower(key)
|
||||
canonicalKey := canonicalHyprlandOverrideKey(key)
|
||||
normalizedKey := hyprlandOverrideMapKey(canonicalKey)
|
||||
existingBinds[normalizedKey] = &hyprlandOverrideBind{
|
||||
Key: key,
|
||||
Key: canonicalKey,
|
||||
Action: action,
|
||||
Description: description,
|
||||
Flags: flags,
|
||||
@@ -255,21 +261,28 @@ func (h *HyprlandProvider) SetBind(key, action, description string, options map[
|
||||
}
|
||||
|
||||
func (h *HyprlandProvider) RemoveBind(key string) error {
|
||||
if err := h.ensureWritableConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
existingBinds, err := h.loadOverrideBinds()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
normalizedKey := strings.ToLower(key)
|
||||
existingBinds[normalizedKey] = &hyprlandOverrideBind{Key: key, Unbind: true}
|
||||
canonicalKey := canonicalHyprlandOverrideKey(key)
|
||||
normalizedKey := hyprlandOverrideMapKey(canonicalKey)
|
||||
existingBinds[normalizedKey] = &hyprlandOverrideBind{Key: canonicalKey, Unbind: true}
|
||||
return h.writeOverrideBinds(existingBinds)
|
||||
}
|
||||
|
||||
func (h *HyprlandProvider) ResetBind(key string) error {
|
||||
if err := h.ensureWritableConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
existingBinds, err := h.loadOverrideBinds()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
normalizedKey := strings.ToLower(key)
|
||||
normalizedKey := hyprlandOverrideMapKey(key)
|
||||
delete(existingBinds, normalizedKey)
|
||||
return h.writeOverrideBinds(existingBinds)
|
||||
}
|
||||
@@ -284,10 +297,46 @@ type hyprlandOverrideBind struct {
|
||||
Unbind bool
|
||||
}
|
||||
|
||||
func (h *HyprlandProvider) ensureWritableConfig() error {
|
||||
if h.isLegacyConfigReadOnly() {
|
||||
return fmt.Errorf("hyprland legacy conf configs are read-only; run dms setup to migrate to Lua before editing keybinds")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *HyprlandProvider) isLegacyConfigReadOnly() bool {
|
||||
expanded, err := utils.ExpandPath(h.configPath)
|
||||
if err != nil {
|
||||
expanded = h.configPath
|
||||
}
|
||||
luaPath := filepath.Join(expanded, "hyprland.lua")
|
||||
if st, err := os.Stat(luaPath); err == nil && st.Mode().IsRegular() {
|
||||
return false
|
||||
}
|
||||
confPath := filepath.Join(expanded, "hyprland.conf")
|
||||
if st, err := os.Stat(confPath); err == nil && st.Mode().IsRegular() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *HyprlandProvider) loadOverrideBinds() (map[string]*hyprlandOverrideBind, error) {
|
||||
return readLuaOrHyprlangOverride(h.GetOverridePath())
|
||||
}
|
||||
|
||||
func canonicalHyprlandOverrideKey(key string) string {
|
||||
trimmed := strings.TrimSpace(key)
|
||||
normalized := luaKeyComboToInternalKey(trimmed)
|
||||
if normalized == "" {
|
||||
return trimmed
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
func hyprlandOverrideMapKey(key string) string {
|
||||
return strings.ToLower(canonicalHyprlandOverrideKey(key))
|
||||
}
|
||||
|
||||
func (h *HyprlandProvider) getBindSortPriority(action string) int {
|
||||
switch {
|
||||
case strings.HasPrefix(action, "exec") && strings.Contains(action, "dms"):
|
||||
@@ -368,24 +417,354 @@ func normalizeLuaBindKeyPart(part string) string {
|
||||
return part
|
||||
}
|
||||
|
||||
type luaField struct {
|
||||
name string
|
||||
value string
|
||||
}
|
||||
|
||||
func luaDispatcherTableCall(funcName string, fields ...luaField) string {
|
||||
parts := make([]string, 0, len(fields))
|
||||
for _, field := range fields {
|
||||
if field.name == "" || field.value == "" {
|
||||
continue
|
||||
}
|
||||
parts = append(parts, field.name+" = "+field.value)
|
||||
}
|
||||
return fmt.Sprintf(`%s({ %s })`, funcName, strings.Join(parts, ", "))
|
||||
}
|
||||
|
||||
func luaStringField(name, value string) luaField {
|
||||
return luaField{name: name, value: strconv.Quote(strings.TrimSpace(value))}
|
||||
}
|
||||
|
||||
func luaBoolField(name string, value bool) luaField {
|
||||
if value {
|
||||
return luaField{name: name, value: "true"}
|
||||
}
|
||||
return luaField{name: name, value: "false"}
|
||||
}
|
||||
|
||||
func luaNumberOrStringField(name, value string) luaField {
|
||||
value = strings.TrimSpace(value)
|
||||
if isBareLuaNumber(value) {
|
||||
return luaField{name: name, value: value}
|
||||
}
|
||||
return luaStringField(name, value)
|
||||
}
|
||||
|
||||
func isBareLuaNumber(value string) bool {
|
||||
if value == "" || strings.HasPrefix(value, "+") {
|
||||
return false
|
||||
}
|
||||
if value[0] == '-' {
|
||||
value = value[1:]
|
||||
}
|
||||
if value == "" {
|
||||
return false
|
||||
}
|
||||
digitsBeforeDot := 0
|
||||
i := 0
|
||||
for i < len(value) && value[i] >= '0' && value[i] <= '9' {
|
||||
digitsBeforeDot++
|
||||
i++
|
||||
}
|
||||
digitsAfterDot := 0
|
||||
if i < len(value) && value[i] == '.' {
|
||||
i++
|
||||
for i < len(value) && value[i] >= '0' && value[i] <= '9' {
|
||||
digitsAfterDot++
|
||||
i++
|
||||
}
|
||||
}
|
||||
return i == len(value) && (digitsBeforeDot > 0 || digitsAfterDot > 0)
|
||||
}
|
||||
|
||||
func splitHyprlandAction(action string) (dispatcher, params string) {
|
||||
action = strings.TrimSpace(action)
|
||||
if action == "" {
|
||||
return "", ""
|
||||
}
|
||||
idx := strings.IndexFunc(action, func(r rune) bool {
|
||||
return r == ' ' || r == '\t' || r == '\r' || r == '\n'
|
||||
})
|
||||
if idx < 0 {
|
||||
return strings.ToLower(action), ""
|
||||
}
|
||||
return strings.ToLower(strings.TrimSpace(action[:idx])), strings.TrimSpace(action[idx+1:])
|
||||
}
|
||||
|
||||
func firstParam(params string) (head, rest string) {
|
||||
params = strings.TrimSpace(params)
|
||||
if params == "" {
|
||||
return "", ""
|
||||
}
|
||||
fields := strings.Fields(params)
|
||||
if len(fields) == 0 {
|
||||
return "", ""
|
||||
}
|
||||
head = fields[0]
|
||||
rest = strings.TrimSpace(strings.TrimPrefix(params, head))
|
||||
return head, rest
|
||||
}
|
||||
|
||||
func xyParams(params string) (x, y string, relative bool, ok bool) {
|
||||
fields := strings.Fields(params)
|
||||
if len(fields) > 0 && strings.EqualFold(fields[0], "exact") {
|
||||
relative = false
|
||||
fields = fields[1:]
|
||||
} else {
|
||||
relative = true
|
||||
}
|
||||
if len(fields) < 2 {
|
||||
return "", "", relative, false
|
||||
}
|
||||
return fields[0], fields[1], relative, true
|
||||
}
|
||||
|
||||
func dispatcherWorkspaceMove(params string, follow *bool) string {
|
||||
workspace, window := firstParam(params)
|
||||
if workspace == "" {
|
||||
return ""
|
||||
}
|
||||
fields := []luaField{luaStringField("workspace", workspace)}
|
||||
if follow != nil {
|
||||
fields = append(fields, luaBoolField("follow", *follow))
|
||||
}
|
||||
if window != "" {
|
||||
fields = append(fields, luaStringField("window", window))
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.window.move", fields...)
|
||||
}
|
||||
|
||||
func dispatcherActiveMoveResize(funcName, params string) string {
|
||||
x, y, relative, ok := xyParams(params)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if !isBareLuaNumber(x) || !isBareLuaNumber(y) {
|
||||
return ""
|
||||
}
|
||||
return luaDispatcherTableCall(funcName,
|
||||
luaNumberOrStringField("x", x),
|
||||
luaNumberOrStringField("y", y),
|
||||
luaBoolField("relative", relative),
|
||||
)
|
||||
}
|
||||
|
||||
func luaActionStringFromKnownHyprlandAction(action string) (string, bool) {
|
||||
dispatcher, params := splitHyprlandAction(action)
|
||||
switch dispatcher {
|
||||
case "spawn", "exec":
|
||||
return fmt.Sprintf(`hl.dsp.exec_cmd(%s)`, strconv.Quote(params)), true
|
||||
case "killactive":
|
||||
return `hl.dsp.window.kill()`, true
|
||||
case "closewindow":
|
||||
if params == "" {
|
||||
return `hl.dsp.window.close()`, true
|
||||
}
|
||||
return fmt.Sprintf(`hl.dsp.window.close(%s)`, strconv.Quote(params)), true
|
||||
case "killwindow":
|
||||
if params == "" {
|
||||
return `hl.dsp.window.kill()`, true
|
||||
}
|
||||
return fmt.Sprintf(`hl.dsp.window.kill(%s)`, strconv.Quote(params)), true
|
||||
case "togglefloating":
|
||||
return `hl.dsp.window.float({ action = "toggle" })`, true
|
||||
case "setfloating":
|
||||
return `hl.dsp.window.float({ action = "set" })`, true
|
||||
case "settiled":
|
||||
return `hl.dsp.window.float({ action = "unset" })`, true
|
||||
case "fullscreen":
|
||||
mode := strings.TrimSpace(params)
|
||||
switch mode {
|
||||
case "", "0":
|
||||
return `hl.dsp.window.fullscreen({ mode = "fullscreen", action = "toggle" })`, true
|
||||
case "1":
|
||||
return `hl.dsp.window.fullscreen({ mode = "maximized", action = "toggle" })`, true
|
||||
}
|
||||
case "fullscreenstate":
|
||||
internal, rest := firstParam(params)
|
||||
client, _ := firstParam(rest)
|
||||
if internal != "" && client != "" {
|
||||
return luaDispatcherTableCall("hl.dsp.window.fullscreen_state",
|
||||
luaNumberOrStringField("internal", internal),
|
||||
luaNumberOrStringField("client", client),
|
||||
), true
|
||||
}
|
||||
case "pin":
|
||||
if params == "" {
|
||||
return `hl.dsp.window.pin()`, true
|
||||
}
|
||||
case "centerwindow":
|
||||
return `hl.dsp.window.center()`, true
|
||||
case "resizewindow":
|
||||
return `hl.dsp.window.resize()`, true
|
||||
case "movewindow":
|
||||
if params == "" {
|
||||
return `hl.dsp.window.drag()`, true
|
||||
}
|
||||
if monitor, ok := strings.CutPrefix(params, "mon:"); ok {
|
||||
return luaDispatcherTableCall("hl.dsp.window.move", luaStringField("monitor", monitor)), true
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.window.move", luaStringField("direction", params)), true
|
||||
case "swapwindow":
|
||||
if params == "" {
|
||||
return "", false
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.window.swap", luaStringField("direction", params)), true
|
||||
case "resizeactive":
|
||||
if expr := dispatcherActiveMoveResize("hl.dsp.window.resize", params); expr != "" {
|
||||
return expr, true
|
||||
}
|
||||
case "moveactive":
|
||||
if expr := dispatcherActiveMoveResize("hl.dsp.window.move", params); expr != "" {
|
||||
return expr, true
|
||||
}
|
||||
case "workspace":
|
||||
if params == "" {
|
||||
return "", false
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.focus", luaStringField("workspace", params)), true
|
||||
case "focusworkspaceoncurrentmonitor":
|
||||
if params == "" {
|
||||
return "", false
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.focus", luaStringField("workspace", params), luaBoolField("on_current_monitor", true)), true
|
||||
case "movetoworkspace":
|
||||
if expr := dispatcherWorkspaceMove(params, nil); expr != "" {
|
||||
return expr, true
|
||||
}
|
||||
case "movetoworkspacesilent":
|
||||
follow := false
|
||||
if expr := dispatcherWorkspaceMove(params, &follow); expr != "" {
|
||||
return expr, true
|
||||
}
|
||||
case "togglespecialworkspace":
|
||||
if params == "" {
|
||||
return `hl.dsp.workspace.toggle_special()`, true
|
||||
}
|
||||
return fmt.Sprintf(`hl.dsp.workspace.toggle_special(%s)`, strconv.Quote(params)), true
|
||||
case "renameworkspace":
|
||||
workspace, name := firstParam(params)
|
||||
if workspace != "" {
|
||||
fields := []luaField{luaStringField("workspace", workspace)}
|
||||
if name != "" {
|
||||
fields = append(fields, luaStringField("name", name))
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.workspace.rename", fields...), true
|
||||
}
|
||||
case "movecurrentworkspacetomonitor":
|
||||
if params != "" {
|
||||
return luaDispatcherTableCall("hl.dsp.workspace.move", luaStringField("monitor", params)), true
|
||||
}
|
||||
case "moveworkspacetomonitor":
|
||||
workspace, monitor := firstParam(params)
|
||||
if workspace != "" && monitor != "" {
|
||||
return luaDispatcherTableCall("hl.dsp.workspace.move", luaStringField("workspace", workspace), luaStringField("monitor", monitor)), true
|
||||
}
|
||||
case "swapactiveworkspaces":
|
||||
monitor1, rest := firstParam(params)
|
||||
monitor2, _ := firstParam(rest)
|
||||
if monitor1 != "" && monitor2 != "" {
|
||||
return luaDispatcherTableCall("hl.dsp.workspace.swap_monitors", luaStringField("monitor1", monitor1), luaStringField("monitor2", monitor2)), true
|
||||
}
|
||||
case "movefocus":
|
||||
if params != "" {
|
||||
return luaDispatcherTableCall("hl.dsp.focus", luaStringField("direction", params)), true
|
||||
}
|
||||
case "focusmonitor":
|
||||
if params != "" {
|
||||
return luaDispatcherTableCall("hl.dsp.focus", luaStringField("monitor", params)), true
|
||||
}
|
||||
case "focuswindow":
|
||||
if params != "" {
|
||||
return luaDispatcherTableCall("hl.dsp.focus", luaStringField("window", params)), true
|
||||
}
|
||||
case "focuscurrentorlast":
|
||||
return `hl.dsp.focus({ last = true })`, true
|
||||
case "focusurgentorlast":
|
||||
return `hl.dsp.focus({ urgent_or_last = true })`, true
|
||||
case "layoutmsg":
|
||||
if params != "" {
|
||||
return fmt.Sprintf(`hl.dsp.layout(%s)`, strconv.Quote(params)), true
|
||||
}
|
||||
case "alterzorder":
|
||||
mode, window := firstParam(params)
|
||||
if mode != "" {
|
||||
fields := []luaField{luaStringField("mode", mode)}
|
||||
if window != "" {
|
||||
fields = append(fields, luaStringField("window", window))
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.window.alter_zorder", fields...), true
|
||||
}
|
||||
case "setprop":
|
||||
window, rest := firstParam(params)
|
||||
prop, value := firstParam(rest)
|
||||
if window != "" && prop != "" && value != "" {
|
||||
return luaDispatcherTableCall("hl.dsp.window.set_prop",
|
||||
luaStringField("window", window),
|
||||
luaStringField("prop", prop),
|
||||
luaStringField("value", value),
|
||||
), true
|
||||
}
|
||||
case "dpms":
|
||||
dpmsAction := strings.TrimSpace(params)
|
||||
switch dpmsAction {
|
||||
case "on":
|
||||
dpmsAction = "enable"
|
||||
case "off":
|
||||
dpmsAction = "disable"
|
||||
}
|
||||
if dpmsAction == "" {
|
||||
return `hl.dsp.dpms({})`, true
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.dpms", luaStringField("action", dpmsAction)), true
|
||||
case "exit":
|
||||
return `hl.dsp.exit()`, true
|
||||
case "submap":
|
||||
return fmt.Sprintf(`hl.dsp.submap(%s)`, strconv.Quote(params)), true
|
||||
case "global":
|
||||
return fmt.Sprintf(`hl.dsp.global(%s)`, strconv.Quote(params)), true
|
||||
case "event":
|
||||
return fmt.Sprintf(`hl.dsp.event(%s)`, strconv.Quote(params)), true
|
||||
case "pass":
|
||||
if params == "" {
|
||||
return `hl.dsp.pass({})`, true
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.pass", luaStringField("window", params)), true
|
||||
case "sendshortcut":
|
||||
mod, rest := firstParam(params)
|
||||
key, window := firstParam(rest)
|
||||
if mod != "" && key != "" {
|
||||
fields := []luaField{luaStringField("mods", mod), luaStringField("key", key)}
|
||||
if window != "" {
|
||||
fields = append(fields, luaStringField("window", window))
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.send_shortcut", fields...), true
|
||||
}
|
||||
case "sendkeystate":
|
||||
mod, rest := firstParam(params)
|
||||
key, rest := firstParam(rest)
|
||||
state, window := firstParam(rest)
|
||||
if mod != "" && key != "" && state != "" {
|
||||
fields := []luaField{luaStringField("mods", mod), luaStringField("key", key), luaStringField("state", state)}
|
||||
if window != "" {
|
||||
fields = append(fields, luaStringField("window", window))
|
||||
}
|
||||
return luaDispatcherTableCall("hl.dsp.send_key_state", fields...), true
|
||||
}
|
||||
case "togglegroup":
|
||||
return `hl.dsp.group.toggle()`, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func luaActionStringFromHyprlangAction(action string) string {
|
||||
action = strings.TrimSpace(action)
|
||||
if strings.HasPrefix(action, "spawn ") {
|
||||
return fmt.Sprintf(`hl.dsp.exec_cmd(%s)`, strconv.Quote(strings.TrimSpace(strings.TrimPrefix(action, "spawn "))))
|
||||
}
|
||||
if strings.HasPrefix(action, "exec ") {
|
||||
return fmt.Sprintf(`hl.dsp.exec_cmd(%s)`, strconv.Quote(strings.TrimPrefix(action, "exec ")))
|
||||
}
|
||||
switch action {
|
||||
case "killactive":
|
||||
return `hl.dsp.window.kill()`
|
||||
case "togglefloating":
|
||||
return `hl.dsp.window.float({ action = "toggle" })`
|
||||
case "exit":
|
||||
return `hl.dsp.exit()`
|
||||
default:
|
||||
return fmt.Sprintf(`hl.dsp.exec_cmd(%s)`, strconv.Quote("hyprctl dispatch "+action))
|
||||
if expr, ok := luaActionStringFromKnownHyprlandAction(action); ok {
|
||||
return expr
|
||||
}
|
||||
return fmt.Sprintf(`hl.dsp.exec_cmd(%s)`, strconv.Quote("hyprctl dispatch "+action))
|
||||
}
|
||||
|
||||
func luaExprToInternalAction(expr string) string {
|
||||
@@ -498,11 +877,12 @@ func readLuaOrHyprlangOverride(path string) (map[string]*hyprlandOverrideBind, e
|
||||
continue
|
||||
}
|
||||
if key, ok := parseLuaUnbindLine(line); ok {
|
||||
pendingUnbinds[strings.ToLower(key)] = key
|
||||
pendingUnbinds[hyprlandOverrideMapKey(key)] = canonicalHyprlandOverrideKey(key)
|
||||
continue
|
||||
}
|
||||
if kb, ok := parseLuaBindOverrideLine(line); ok {
|
||||
normalizedKey := strings.ToLower(kb.Key)
|
||||
kb.Key = canonicalHyprlandOverrideKey(kb.Key)
|
||||
normalizedKey := hyprlandOverrideMapKey(kb.Key)
|
||||
binds[normalizedKey] = kb
|
||||
delete(pendingUnbinds, normalizedKey)
|
||||
continue
|
||||
@@ -520,7 +900,8 @@ func readLuaOrHyprlangOverride(path string) (map[string]*hyprlandOverrideBind, e
|
||||
action = kb.Dispatcher + " " + kb.Params
|
||||
}
|
||||
flags := kb.Flags
|
||||
normalizedKey := strings.ToLower(keyStr)
|
||||
keyStr = canonicalHyprlandOverrideKey(keyStr)
|
||||
normalizedKey := hyprlandOverrideMapKey(keyStr)
|
||||
binds[normalizedKey] = &hyprlandOverrideBind{
|
||||
Key: keyStr,
|
||||
Action: action,
|
||||
|
||||
@@ -54,6 +54,8 @@ type HyprlandParser struct {
|
||||
dmsProcessed bool
|
||||
removedKeys map[string]bool // bare hl.unbind targets (negative overrides)
|
||||
defaultDMSKeys map[string]bool // keys present in dms/binds.{lua,conf}
|
||||
configFormat string
|
||||
readOnly bool
|
||||
}
|
||||
|
||||
func NewHyprlandParser(configDir string) *HyprlandParser {
|
||||
@@ -310,6 +312,8 @@ type HyprlandDMSStatus struct {
|
||||
Effective bool
|
||||
OverriddenBy int
|
||||
StatusMessage string
|
||||
ConfigFormat string
|
||||
ReadOnly bool
|
||||
}
|
||||
|
||||
func (p *HyprlandParser) buildDMSStatus() *HyprlandDMSStatus {
|
||||
@@ -319,6 +323,8 @@ func (p *HyprlandParser) buildDMSStatus() *HyprlandDMSStatus {
|
||||
IncludePosition: p.dmsIncludePos,
|
||||
TotalIncludes: p.includeCount,
|
||||
BindsAfterDMS: p.bindsAfterDMS,
|
||||
ConfigFormat: p.configFormat,
|
||||
ReadOnly: p.readOnly,
|
||||
}
|
||||
|
||||
switch {
|
||||
@@ -398,6 +404,13 @@ func (p *HyprlandParser) ParseWithDMS() (*HyprlandSection, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.EqualFold(filepath.Ext(mainConfig), ".lua") {
|
||||
p.configFormat = "lua"
|
||||
p.readOnly = false
|
||||
} else {
|
||||
p.configFormat = "hyprlang"
|
||||
p.readOnly = true
|
||||
}
|
||||
section, err := p.parseFileWithSource(mainConfig, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -1004,7 +1017,18 @@ func luaExprToDispatcherParams(expr string) (dispatcher, params string) {
|
||||
}
|
||||
}
|
||||
return "exec", strings.TrimSpace(strings.TrimPrefix(expr, "hl.dsp.exec_cmd"))
|
||||
case strings.Contains(expr, "hl.dsp.window.kill()"):
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.close("):
|
||||
if arg := luaCallStringArgValue(expr, "hl.dsp.window.close"); arg != "" {
|
||||
return "closewindow", arg
|
||||
}
|
||||
return "closewindow", ""
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.kill("):
|
||||
if luaTableBoolFieldValue(expr, "force") {
|
||||
return "forcekillactive", ""
|
||||
}
|
||||
if arg := luaCallStringArgValue(expr, "hl.dsp.window.kill"); arg != "" {
|
||||
return "killwindow", arg
|
||||
}
|
||||
return "killactive", ""
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.fullscreen("):
|
||||
switch luaTableStringField(expr, "mode") {
|
||||
@@ -1014,8 +1038,26 @@ func luaExprToDispatcherParams(expr string) (dispatcher, params string) {
|
||||
return "fullscreen", "0"
|
||||
}
|
||||
return "fullscreen", luaTableStringField(expr, "mode")
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.fullscreen_state("):
|
||||
internal := luaStringValue(luaTableScalarField(expr, "internal"))
|
||||
client := luaStringValue(luaTableScalarField(expr, "client"))
|
||||
return joinDispatcherParams("fullscreenstate", internal, client)
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.float("):
|
||||
return "togglefloating", ""
|
||||
switch luaTableStringField(expr, "action") {
|
||||
case "set":
|
||||
return "setfloating", ""
|
||||
case "unset":
|
||||
return "settiled", ""
|
||||
default:
|
||||
return "togglefloating", ""
|
||||
}
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.pin("):
|
||||
if action := luaTableStringField(expr, "action"); action != "" && action != "toggle" {
|
||||
return "pin", action
|
||||
}
|
||||
return "pin", ""
|
||||
case strings.Contains(expr, "hl.dsp.window.center()"):
|
||||
return "centerwindow", ""
|
||||
case strings.Contains(expr, "hl.dsp.group.toggle()"):
|
||||
return "togglegroup", ""
|
||||
case strings.HasPrefix(expr, "hl.dsp.focus("):
|
||||
@@ -1025,18 +1067,43 @@ func luaExprToDispatcherParams(expr string) (dispatcher, params string) {
|
||||
case luaTableStringField(expr, "monitor") != "":
|
||||
return "focusmonitor", luaTableStringField(expr, "monitor")
|
||||
case luaTableStringField(expr, "workspace") != "":
|
||||
if luaTableBoolFieldValue(expr, "on_current_monitor") {
|
||||
return "focusworkspaceoncurrentmonitor", luaTableStringField(expr, "workspace")
|
||||
}
|
||||
return "workspace", luaTableStringField(expr, "workspace")
|
||||
case luaTableStringField(expr, "window") != "":
|
||||
return "focuswindow", luaTableStringField(expr, "window")
|
||||
case luaTableBoolFieldValue(expr, "urgent_or_last"):
|
||||
return "focusurgentorlast", ""
|
||||
case luaTableBoolFieldValue(expr, "last"):
|
||||
return "focuscurrentorlast", ""
|
||||
}
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.move("):
|
||||
switch {
|
||||
case luaTableScalarField(expr, "x") != "" || luaTableScalarField(expr, "y") != "":
|
||||
x := luaStringValue(luaTableScalarField(expr, "x"))
|
||||
y := luaStringValue(luaTableScalarField(expr, "y"))
|
||||
if x == "" {
|
||||
x = "0"
|
||||
}
|
||||
if y == "" {
|
||||
y = "0"
|
||||
}
|
||||
prefix := ""
|
||||
if raw, ok := luaTableBoolField(expr, "relative"); ok && !raw {
|
||||
prefix = "exact "
|
||||
}
|
||||
return "moveactive", prefix + x + " " + y
|
||||
case luaTableStringField(expr, "direction") != "":
|
||||
return "movewindow", luaTableStringField(expr, "direction")
|
||||
case luaTableStringField(expr, "monitor") != "":
|
||||
return "movewindow", "mon:" + luaTableStringField(expr, "monitor")
|
||||
case luaTableStringField(expr, "workspace") != "":
|
||||
return "movetoworkspace", luaTableStringField(expr, "workspace")
|
||||
action := "movetoworkspace"
|
||||
if follow, ok := luaTableBoolField(expr, "follow"); ok && !follow {
|
||||
action = "movetoworkspacesilent"
|
||||
}
|
||||
return joinDispatcherParams(action, luaTableStringField(expr, "workspace"), luaTableStringField(expr, "window"))
|
||||
}
|
||||
case expr == "hl.dsp.window.drag()":
|
||||
return "movewindow", ""
|
||||
@@ -1052,19 +1119,69 @@ func luaExprToDispatcherParams(expr string) (dispatcher, params string) {
|
||||
if y == "" {
|
||||
y = "0"
|
||||
}
|
||||
return "resizeactive", x + " " + y
|
||||
}
|
||||
case strings.HasPrefix(expr, "hl.dsp.layout("):
|
||||
arg := extractLuaCallStringArg(expr, "hl.dsp.layout")
|
||||
if arg != "" {
|
||||
if u, err := strconv.Unquote(arg); err == nil {
|
||||
return "layoutmsg", u
|
||||
prefix := ""
|
||||
if relative, ok := luaTableBoolField(expr, "relative"); ok && !relative {
|
||||
prefix = "exact "
|
||||
}
|
||||
return "resizeactive", prefix + x + " " + y
|
||||
}
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.swap("):
|
||||
return "swapwindow", luaTableStringField(expr, "direction")
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.alter_zorder("):
|
||||
mode := luaTableStringField(expr, "mode")
|
||||
if mode == "" {
|
||||
mode = luaTableStringField(expr, "zheight")
|
||||
}
|
||||
return joinDispatcherParams("alterzorder", mode, luaTableStringField(expr, "window"))
|
||||
case strings.HasPrefix(expr, "hl.dsp.window.set_prop("):
|
||||
prop := luaTableStringField(expr, "prop")
|
||||
if prop == "" {
|
||||
prop = luaTableStringField(expr, "property")
|
||||
}
|
||||
return joinDispatcherParams("setprop", luaTableStringField(expr, "window"), prop, luaTableStringField(expr, "value"))
|
||||
case strings.HasPrefix(expr, "hl.dsp.workspace.rename("):
|
||||
return joinDispatcherParams("renameworkspace", luaTableStringField(expr, "workspace"), luaTableStringField(expr, "name"))
|
||||
case strings.HasPrefix(expr, "hl.dsp.workspace.move("):
|
||||
workspace := luaTableStringField(expr, "workspace")
|
||||
monitor := luaTableStringField(expr, "monitor")
|
||||
if workspace != "" {
|
||||
return joinDispatcherParams("moveworkspacetomonitor", workspace, monitor)
|
||||
}
|
||||
return "movecurrentworkspacetomonitor", monitor
|
||||
case strings.HasPrefix(expr, "hl.dsp.workspace.swap_monitors("):
|
||||
return joinDispatcherParams("swapactiveworkspaces", luaTableStringField(expr, "monitor1"), luaTableStringField(expr, "monitor2"))
|
||||
case strings.HasPrefix(expr, "hl.dsp.workspace.toggle_special("):
|
||||
return "togglespecialworkspace", luaCallStringArgValue(expr, "hl.dsp.workspace.toggle_special")
|
||||
case strings.HasPrefix(expr, "hl.dsp.layout("):
|
||||
if arg := luaCallStringArgValue(expr, "hl.dsp.layout"); arg != "" {
|
||||
return "layoutmsg", arg
|
||||
}
|
||||
case strings.HasPrefix(expr, "hl.dsp.dpms("):
|
||||
if action := luaTableStringField(expr, "action"); action != "" {
|
||||
switch action {
|
||||
case "enable":
|
||||
return "dpms", "on"
|
||||
case "disable":
|
||||
return "dpms", "off"
|
||||
}
|
||||
return "dpms", action
|
||||
}
|
||||
return "dpms", ""
|
||||
case strings.HasPrefix(expr, "hl.dsp.submap("):
|
||||
return "submap", luaCallStringArgValue(expr, "hl.dsp.submap")
|
||||
case strings.HasPrefix(expr, "hl.dsp.global("):
|
||||
return "global", luaCallStringArgValue(expr, "hl.dsp.global")
|
||||
case strings.HasPrefix(expr, "hl.dsp.event("):
|
||||
return "event", luaCallStringArgValue(expr, "hl.dsp.event")
|
||||
case strings.HasPrefix(expr, "hl.dsp.pass("):
|
||||
if window := luaTableStringField(expr, "window"); window != "" {
|
||||
return "pass", window
|
||||
}
|
||||
return "pass", luaCallStringArgValue(expr, "hl.dsp.pass")
|
||||
case strings.HasPrefix(expr, "hl.dsp.send_shortcut("):
|
||||
return joinDispatcherParams("sendshortcut", luaTableModsField(expr), luaTableStringField(expr, "key"), luaTableStringField(expr, "window"))
|
||||
case strings.HasPrefix(expr, "hl.dsp.send_key_state("):
|
||||
return joinDispatcherParams("sendkeystate", luaTableModsField(expr), luaTableStringField(expr, "key"), luaTableStringField(expr, "state"), luaTableStringField(expr, "window"))
|
||||
case strings.Contains(expr, "hl.dsp.exit()"):
|
||||
return "exit", ""
|
||||
default:
|
||||
@@ -1073,6 +1190,17 @@ func luaExprToDispatcherParams(expr string) (dispatcher, params string) {
|
||||
return "exec", "hyprctl dispatch lua:" + expr
|
||||
}
|
||||
|
||||
func joinDispatcherParams(dispatcher string, values ...string) (string, string) {
|
||||
parts := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
value = strings.TrimSpace(value)
|
||||
if value != "" {
|
||||
parts = append(parts, value)
|
||||
}
|
||||
}
|
||||
return dispatcher, strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
func extractLuaCallStringArg(callExpr, funcName string) string {
|
||||
callExpr = strings.TrimSpace(callExpr)
|
||||
prefix := funcName + "("
|
||||
@@ -1100,10 +1228,46 @@ func extractLuaCallStringArg(callExpr, funcName string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func luaCallStringArgValue(callExpr, funcName string) string {
|
||||
arg := extractLuaCallStringArg(callExpr, funcName)
|
||||
if arg == "" {
|
||||
return ""
|
||||
}
|
||||
u, err := strconv.Unquote(arg)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func luaTableStringField(expr, field string) string {
|
||||
return luaStringValue(luaTableScalarField(expr, field))
|
||||
}
|
||||
|
||||
func luaTableModsField(expr string) string {
|
||||
if mods := luaTableStringField(expr, "mods"); mods != "" {
|
||||
return mods
|
||||
}
|
||||
return luaTableStringField(expr, "mod")
|
||||
}
|
||||
|
||||
func luaTableBoolFieldValue(expr, field string) bool {
|
||||
value, ok := luaTableBoolField(expr, field)
|
||||
return ok && value
|
||||
}
|
||||
|
||||
func luaTableBoolField(expr, field string) (bool, bool) {
|
||||
raw := strings.ToLower(luaTableScalarField(expr, field))
|
||||
switch raw {
|
||||
case "true":
|
||||
return true, true
|
||||
case "false":
|
||||
return false, true
|
||||
default:
|
||||
return false, false
|
||||
}
|
||||
}
|
||||
|
||||
func luaTableScalarField(expr, field string) string {
|
||||
re := regexp.MustCompile(`(?s)\b` + regexp.QuoteMeta(field) + `\s*=\s*("(?:\\.|[^"])*"|'(?:\\.|[^'])*'|\[\[.*?\]\]|-?\d+(?:\.\d+)?|true|false)`)
|
||||
m := re.FindStringSubmatch(expr)
|
||||
|
||||
@@ -70,12 +70,17 @@ func TestHyprlandLuaBindRoundTripHelpers(t *testing.T) {
|
||||
wantParams string
|
||||
}{
|
||||
{`hl.dsp.exec_cmd([[dms ipc call brightness increment 5 ""]])`, "exec", `dms ipc call brightness increment 5 ""`},
|
||||
{`hl.dsp.exec_cmd([[hyprctl dispatch workspace 1]])`, "workspace", "1"},
|
||||
{`hl.dsp.window.fullscreen({ mode = "maximized", action = "toggle" })`, "fullscreen", "1"},
|
||||
{`hl.dsp.focus({ workspace = "e+1" })`, "workspace", "e+1"},
|
||||
{`hl.dsp.focus({ workspace = "2", on_current_monitor = true })`, "focusworkspaceoncurrentmonitor", "2"},
|
||||
{`hl.dsp.window.move({ monitor = "l" })`, "movewindow", "mon:l"},
|
||||
{`hl.dsp.window.resize({ x = "-10%", y = 0, relative = true })`, "resizeactive", "-10% 0"},
|
||||
{`hl.dsp.window.move({ workspace = "special:magic", follow = false })`, "movetoworkspacesilent", "special:magic"},
|
||||
{`hl.dsp.window.resize({ x = -100, y = 0, relative = true })`, "resizeactive", "-100 0"},
|
||||
{`hl.dsp.window.resize({ x = 1280, y = 720, relative = false })`, "resizeactive", "exact 1280 720"},
|
||||
{`hl.dsp.layout("togglesplit")`, "layoutmsg", "togglesplit"},
|
||||
{`hl.dsp.dpms({ action = "toggle" })`, "dpms", "toggle"},
|
||||
{`hl.dsp.workspace.rename({ workspace = "1", name = "work" })`, "renameworkspace", "1 work"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -119,6 +124,41 @@ hl.bind("SUPER + N", hl.dsp.exec_cmd("dms ipc call notepad toggle")) -- Notepad:
|
||||
}
|
||||
}
|
||||
|
||||
func TestLuaActionStringFromHyprlangActionUsesNativeDispatchers(t *testing.T) {
|
||||
tests := []struct {
|
||||
action string
|
||||
want string
|
||||
}{
|
||||
{"workspace 1", `hl.dsp.focus({ workspace = "1" })`},
|
||||
{"movetoworkspace 2", `hl.dsp.window.move({ workspace = "2" })`},
|
||||
{"movetoworkspacesilent special:magic", `hl.dsp.window.move({ workspace = "special:magic", follow = false })`},
|
||||
{"focusmonitor DP-1", `hl.dsp.focus({ monitor = "DP-1" })`},
|
||||
{"resizeactive exact 1280 720", `hl.dsp.window.resize({ x = 1280, y = 720, relative = false })`},
|
||||
{"dpms toggle", `hl.dsp.dpms({ action = "toggle" })`},
|
||||
{"renameworkspace 1 work", `hl.dsp.workspace.rename({ workspace = "1", name = "work" })`},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.action, func(t *testing.T) {
|
||||
got := luaActionStringFromHyprlangAction(tt.action)
|
||||
if got != tt.want {
|
||||
t.Fatalf("luaActionStringFromHyprlangAction(%q) = %q, want %q", tt.action, got, tt.want)
|
||||
}
|
||||
if strings.Contains(got, "hyprctl dispatch") {
|
||||
t.Fatalf("expected native Lua dispatcher, got legacy dispatch wrapper: %q", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLuaActionStringFallsBackForUnsupportedResizePercentages(t *testing.T) {
|
||||
got := luaActionStringFromHyprlangAction("resizeactive exact 100% 100%")
|
||||
want := `hl.dsp.exec_cmd("hyprctl dispatch resizeactive exact 100% 100%")`
|
||||
if got != want {
|
||||
t.Fatalf("luaActionStringFromHyprlangAction() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHyprlandLuaBindsUserOverridesDefaults(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
dmsDir := filepath.Join(tmpDir, "dms")
|
||||
@@ -283,6 +323,64 @@ func TestHyprlandRemoveBindWritesNegativeOverrideForDefault(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHyprlandSetBindLeavesConfOnlyInstallReadOnly(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(tmpDir, "hyprland.conf"), []byte("bind = SUPER, T, exec, kitty\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
provider := NewHyprlandProvider(tmpDir)
|
||||
err := provider.SetBind("SUPER+N", "workspace 1", "Workspace 1", nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected SetBind to reject conf-only Hyprland config")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "read-only") {
|
||||
t.Fatalf("expected read-only error, got %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmpDir, "dms", "binds-user.lua")); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected no Lua override to be created for conf-only config, stat err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHyprlandSetBindUpdatesSpacedLuaOverrideWithoutDuplicates(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
dmsDir := filepath.Join(tmpDir, "dms")
|
||||
if err := os.MkdirAll(dmsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
override := `-- DMS user keybind overrides
|
||||
|
||||
hl.unbind("SUPER + SHIFT + S")
|
||||
hl.bind("SUPER + 1", hl.dsp.exec_cmd("hyprctl dispatch workspace 1"))
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(dmsDir, "binds-user.lua"), []byte(override), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
provider := NewHyprlandProvider(tmpDir)
|
||||
if err := provider.SetBind("SUPER + 1", "workspace 1", "", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(filepath.Join(dmsDir, "binds-user.lua"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := string(data)
|
||||
if strings.Count(got, `hl.unbind("SUPER + 1")`) != 1 {
|
||||
t.Fatalf("expected one SUPER+1 unbind, got:\n%s", got)
|
||||
}
|
||||
if strings.Count(got, `hl.bind("SUPER + 1", hl.dsp.focus({ workspace = "1" }))`) != 1 {
|
||||
t.Fatalf("expected one native SUPER+1 bind, got:\n%s", got)
|
||||
}
|
||||
if strings.Contains(got, "hyprctl dispatch workspace 1") {
|
||||
t.Fatalf("expected old hyprctl workspace dispatcher to be replaced, got:\n%s", got)
|
||||
}
|
||||
if !strings.Contains(got, `hl.unbind("SUPER + SHIFT + S")`) {
|
||||
t.Fatalf("expected unrelated override to be preserved, got:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHyprlandRemoveBindReplacesExistingOverrideWithNegativeOverride(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
dmsDir := filepath.Join(tmpDir, "dms")
|
||||
|
||||
Reference in New Issue
Block a user