mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2025-12-06 05:25:41 -05:00
keybinds: fix alt+shift, kdl parsing, allow arguments
This commit is contained in:
@@ -333,35 +333,6 @@ func (n *NiriProvider) isRecentWindowsAction(action string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *NiriProvider) parseSpawnArgs(s string) []string {
|
||||
var args []string
|
||||
var current strings.Builder
|
||||
var inQuote, escaped bool
|
||||
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case escaped:
|
||||
current.WriteRune(r)
|
||||
escaped = false
|
||||
case r == '\\':
|
||||
escaped = true
|
||||
case r == '"':
|
||||
inQuote = !inQuote
|
||||
case r == ' ' && !inQuote:
|
||||
if current.Len() > 0 {
|
||||
args = append(args, current.String())
|
||||
current.Reset()
|
||||
}
|
||||
default:
|
||||
current.WriteRune(r)
|
||||
}
|
||||
}
|
||||
if current.Len() > 0 {
|
||||
args = append(args, current.String())
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
func (n *NiriProvider) buildBindNode(bind *overrideBind) *document.Node {
|
||||
node := document.NewNode()
|
||||
node.SetName(bind.Key)
|
||||
@@ -392,19 +363,48 @@ func (n *NiriProvider) buildActionNode(action string) *document.Node {
|
||||
action = strings.TrimSpace(action)
|
||||
node := document.NewNode()
|
||||
|
||||
if !strings.HasPrefix(action, "spawn ") {
|
||||
parts := n.parseActionParts(action)
|
||||
if len(parts) == 0 {
|
||||
node.SetName(action)
|
||||
return node
|
||||
}
|
||||
|
||||
node.SetName("spawn")
|
||||
args := n.parseSpawnArgs(strings.TrimPrefix(action, "spawn "))
|
||||
for _, arg := range args {
|
||||
node.SetName(parts[0])
|
||||
for _, arg := range parts[1:] {
|
||||
node.AddArgument(arg, "")
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
func (n *NiriProvider) parseActionParts(action string) []string {
|
||||
var parts []string
|
||||
var current strings.Builder
|
||||
var inQuote, escaped bool
|
||||
|
||||
for _, r := range action {
|
||||
switch {
|
||||
case escaped:
|
||||
current.WriteRune(r)
|
||||
escaped = false
|
||||
case r == '\\':
|
||||
escaped = true
|
||||
case r == '"':
|
||||
inQuote = !inQuote
|
||||
case r == ' ' && !inQuote:
|
||||
if current.Len() > 0 {
|
||||
parts = append(parts, current.String())
|
||||
current.Reset()
|
||||
}
|
||||
default:
|
||||
current.WriteRune(r)
|
||||
}
|
||||
}
|
||||
if current.Len() > 0 {
|
||||
parts = append(parts, current.String())
|
||||
}
|
||||
return parts
|
||||
}
|
||||
|
||||
func (n *NiriProvider) writeOverrideBinds(binds map[string]*overrideBind) error {
|
||||
overridePath := n.GetOverridePath()
|
||||
content := n.generateBindsContent(binds)
|
||||
@@ -501,21 +501,46 @@ func (n *NiriProvider) writeBindNode(sb *strings.Builder, bind *overrideBind, in
|
||||
sb.WriteString(" { ")
|
||||
if len(node.Children) > 0 {
|
||||
child := node.Children[0]
|
||||
sb.WriteString(child.Name.String())
|
||||
actionName := child.Name.String()
|
||||
sb.WriteString(actionName)
|
||||
forceQuote := actionName == "spawn"
|
||||
for _, arg := range child.Arguments {
|
||||
sb.WriteString(" ")
|
||||
n.writeQuotedArg(sb, arg.ValueString())
|
||||
n.writeArg(sb, arg.ValueString(), forceQuote)
|
||||
}
|
||||
}
|
||||
sb.WriteString("; }\n")
|
||||
}
|
||||
|
||||
func (n *NiriProvider) writeQuotedArg(sb *strings.Builder, val string) {
|
||||
func (n *NiriProvider) writeArg(sb *strings.Builder, val string, forceQuote bool) {
|
||||
if !forceQuote && n.isNumericArg(val) {
|
||||
sb.WriteString(val)
|
||||
return
|
||||
}
|
||||
sb.WriteString("\"")
|
||||
sb.WriteString(strings.ReplaceAll(val, "\"", "\\\""))
|
||||
sb.WriteString("\"")
|
||||
}
|
||||
|
||||
func (n *NiriProvider) isNumericArg(val string) bool {
|
||||
if val == "" {
|
||||
return false
|
||||
}
|
||||
start := 0
|
||||
if val[0] == '-' || val[0] == '+' {
|
||||
if len(val) == 1 {
|
||||
return false
|
||||
}
|
||||
start = 1
|
||||
}
|
||||
for i := start; i < len(val); i++ {
|
||||
if val[i] < '0' || val[i] > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (n *NiriProvider) validateBindsContent(content string) error {
|
||||
tmpFile, err := os.CreateTemp("", "dms-binds-*.kdl")
|
||||
if err != nil {
|
||||
|
||||
@@ -496,3 +496,119 @@ func TestNiriParseMultipleArgs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNiriParseNumericWorkspaceBinds(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
configFile := filepath.Join(tmpDir, "config.kdl")
|
||||
|
||||
content := `binds {
|
||||
Mod+1 hotkey-overlay-title="Focus Workspace 1" { focus-workspace 1; }
|
||||
Mod+2 hotkey-overlay-title="Focus Workspace 2" { focus-workspace 2; }
|
||||
Mod+0 hotkey-overlay-title="Focus Workspace 10" { focus-workspace 10; }
|
||||
Mod+Shift+1 hotkey-overlay-title="Move to Workspace 1" { move-column-to-workspace 1; }
|
||||
}
|
||||
`
|
||||
if err := os.WriteFile(configFile, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("Failed to write test config: %v", err)
|
||||
}
|
||||
|
||||
result, err := ParseNiriKeys(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseNiriKeys failed: %v", err)
|
||||
}
|
||||
|
||||
if len(result.Section.Keybinds) != 4 {
|
||||
t.Errorf("Expected 4 keybinds, got %d", len(result.Section.Keybinds))
|
||||
}
|
||||
|
||||
for _, kb := range result.Section.Keybinds {
|
||||
switch kb.Key {
|
||||
case "1":
|
||||
if len(kb.Mods) == 1 && kb.Mods[0] == "Mod" {
|
||||
if kb.Action != "focus-workspace" || len(kb.Args) != 1 || kb.Args[0] != "1" {
|
||||
t.Errorf("Mod+1 action/args mismatch: %+v", kb)
|
||||
}
|
||||
if kb.Description != "Focus Workspace 1" {
|
||||
t.Errorf("Mod+1 description = %q, want 'Focus Workspace 1'", kb.Description)
|
||||
}
|
||||
}
|
||||
case "0":
|
||||
if kb.Action != "focus-workspace" || len(kb.Args) != 1 || kb.Args[0] != "10" {
|
||||
t.Errorf("Mod+0 action/args mismatch: %+v", kb)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNiriParseQuotedStringArgs(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
configFile := filepath.Join(tmpDir, "config.kdl")
|
||||
|
||||
content := `binds {
|
||||
Super+Minus hotkey-overlay-title="Adjust Column Width -10%" { set-column-width "-10%"; }
|
||||
Super+Equal hotkey-overlay-title="Adjust Column Width +10%" { set-column-width "+10%"; }
|
||||
Super+Shift+Minus hotkey-overlay-title="Adjust Window Height -10%" { set-window-height "-10%"; }
|
||||
}
|
||||
`
|
||||
if err := os.WriteFile(configFile, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("Failed to write test config: %v", err)
|
||||
}
|
||||
|
||||
result, err := ParseNiriKeys(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseNiriKeys failed: %v", err)
|
||||
}
|
||||
|
||||
if len(result.Section.Keybinds) != 3 {
|
||||
t.Errorf("Expected 3 keybinds, got %d", len(result.Section.Keybinds))
|
||||
}
|
||||
|
||||
for _, kb := range result.Section.Keybinds {
|
||||
if kb.Action == "set-column-width" {
|
||||
if len(kb.Args) != 1 {
|
||||
t.Errorf("set-column-width should have 1 arg, got %d", len(kb.Args))
|
||||
continue
|
||||
}
|
||||
if kb.Args[0] != "-10%" && kb.Args[0] != "+10%" {
|
||||
t.Errorf("set-column-width arg = %q, want -10%% or +10%%", kb.Args[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNiriParseActionWithProperties(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
configFile := filepath.Join(tmpDir, "config.kdl")
|
||||
|
||||
content := `binds {
|
||||
Mod+Shift+1 hotkey-overlay-title="Move to Workspace 1" { move-column-to-workspace 1 focus=false; }
|
||||
Mod+Shift+2 hotkey-overlay-title="Move to Workspace 2" { move-column-to-workspace 2 focus=false; }
|
||||
Alt+Tab { next-window scope="output"; }
|
||||
}
|
||||
`
|
||||
if err := os.WriteFile(configFile, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("Failed to write test config: %v", err)
|
||||
}
|
||||
|
||||
result, err := ParseNiriKeys(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseNiriKeys failed: %v", err)
|
||||
}
|
||||
|
||||
if len(result.Section.Keybinds) != 3 {
|
||||
t.Errorf("Expected 3 keybinds, got %d", len(result.Section.Keybinds))
|
||||
}
|
||||
|
||||
for _, kb := range result.Section.Keybinds {
|
||||
switch kb.Action {
|
||||
case "move-column-to-workspace":
|
||||
if len(kb.Args) != 1 {
|
||||
t.Errorf("move-column-to-workspace should have 1 arg, got %d", len(kb.Args))
|
||||
}
|
||||
case "next-window":
|
||||
if kb.Key != "Tab" {
|
||||
t.Errorf("next-window key = %q, want 'Tab'", kb.Key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -397,3 +397,211 @@ recent-windows {
|
||||
t.Errorf("Expected at least 2 Alt-Tab binds, got %d", len(cheatSheet.Binds["Alt-Tab"]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNiriGenerateBindsContentNumericArgs(t *testing.T) {
|
||||
provider := NewNiriProvider("")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
binds map[string]*overrideBind
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "workspace with numeric arg",
|
||||
binds: map[string]*overrideBind{
|
||||
"Mod+1": {
|
||||
Key: "Mod+1",
|
||||
Action: "focus-workspace 1",
|
||||
Description: "Focus Workspace 1",
|
||||
},
|
||||
},
|
||||
expected: `binds {
|
||||
Mod+1 hotkey-overlay-title="Focus Workspace 1" { focus-workspace 1; }
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "workspace with large numeric arg",
|
||||
binds: map[string]*overrideBind{
|
||||
"Mod+0": {
|
||||
Key: "Mod+0",
|
||||
Action: "focus-workspace 10",
|
||||
Description: "Focus Workspace 10",
|
||||
},
|
||||
},
|
||||
expected: `binds {
|
||||
Mod+0 hotkey-overlay-title="Focus Workspace 10" { focus-workspace 10; }
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "percentage string arg (should be quoted)",
|
||||
binds: map[string]*overrideBind{
|
||||
"Super+Minus": {
|
||||
Key: "Super+Minus",
|
||||
Action: `set-column-width "-10%"`,
|
||||
Description: "Adjust Column Width -10%",
|
||||
},
|
||||
},
|
||||
expected: `binds {
|
||||
Super+Minus hotkey-overlay-title="Adjust Column Width -10%" { set-column-width "-10%"; }
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "positive percentage string arg",
|
||||
binds: map[string]*overrideBind{
|
||||
"Super+Equal": {
|
||||
Key: "Super+Equal",
|
||||
Action: `set-column-width "+10%"`,
|
||||
Description: "Adjust Column Width +10%",
|
||||
},
|
||||
},
|
||||
expected: `binds {
|
||||
Super+Equal hotkey-overlay-title="Adjust Column Width +10%" { set-column-width "+10%"; }
|
||||
}
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := provider.generateBindsContent(tt.binds)
|
||||
if result != tt.expected {
|
||||
t.Errorf("generateBindsContent() =\n%q\nwant:\n%q", result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNiriGenerateActionWithUnquotedPercentArg(t *testing.T) {
|
||||
provider := NewNiriProvider("")
|
||||
|
||||
binds := map[string]*overrideBind{
|
||||
"Super+Equal": {
|
||||
Key: "Super+Equal",
|
||||
Action: "set-window-height +10%",
|
||||
Description: "Adjust Window Height +10%",
|
||||
},
|
||||
}
|
||||
|
||||
content := provider.generateBindsContent(binds)
|
||||
expected := `binds {
|
||||
Super+Equal hotkey-overlay-title="Adjust Window Height +10%" { set-window-height "+10%"; }
|
||||
}
|
||||
`
|
||||
if content != expected {
|
||||
t.Errorf("Content mismatch.\nGot:\n%s\nWant:\n%s", content, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNiriGenerateSpawnWithNumericArgs(t *testing.T) {
|
||||
provider := NewNiriProvider("")
|
||||
|
||||
binds := map[string]*overrideBind{
|
||||
"XF86AudioLowerVolume": {
|
||||
Key: "XF86AudioLowerVolume",
|
||||
Action: `spawn "dms" "ipc" "call" "audio" "decrement" "3"`,
|
||||
Options: map[string]any{"allow-when-locked": true},
|
||||
},
|
||||
}
|
||||
|
||||
content := provider.generateBindsContent(binds)
|
||||
expected := `binds {
|
||||
XF86AudioLowerVolume allow-when-locked=true { spawn "dms" "ipc" "call" "audio" "decrement" "3"; }
|
||||
}
|
||||
`
|
||||
if content != expected {
|
||||
t.Errorf("Content mismatch.\nGot:\n%s\nWant:\n%s", content, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNiriGenerateSpawnNumericArgFromCLI(t *testing.T) {
|
||||
provider := NewNiriProvider("")
|
||||
|
||||
binds := map[string]*overrideBind{
|
||||
"XF86AudioLowerVolume": {
|
||||
Key: "XF86AudioLowerVolume",
|
||||
Action: "spawn dms ipc call audio decrement 3",
|
||||
Options: map[string]any{"allow-when-locked": true},
|
||||
},
|
||||
}
|
||||
|
||||
content := provider.generateBindsContent(binds)
|
||||
expected := `binds {
|
||||
XF86AudioLowerVolume allow-when-locked=true { spawn "dms" "ipc" "call" "audio" "decrement" "3"; }
|
||||
}
|
||||
`
|
||||
if content != expected {
|
||||
t.Errorf("Content mismatch.\nGot:\n%s\nWant:\n%s", content, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNiriGenerateWorkspaceBindsRoundTrip(t *testing.T) {
|
||||
provider := NewNiriProvider("")
|
||||
|
||||
binds := map[string]*overrideBind{
|
||||
"Mod+1": {
|
||||
Key: "Mod+1",
|
||||
Action: "focus-workspace 1",
|
||||
Description: "Focus Workspace 1",
|
||||
},
|
||||
"Mod+2": {
|
||||
Key: "Mod+2",
|
||||
Action: "focus-workspace 2",
|
||||
Description: "Focus Workspace 2",
|
||||
},
|
||||
"Mod+Shift+1": {
|
||||
Key: "Mod+Shift+1",
|
||||
Action: "move-column-to-workspace 1",
|
||||
Description: "Move to Workspace 1",
|
||||
},
|
||||
"Super+Minus": {
|
||||
Key: "Super+Minus",
|
||||
Action: "set-column-width -10%",
|
||||
Description: "Adjust Column Width -10%",
|
||||
},
|
||||
}
|
||||
|
||||
content := provider.generateBindsContent(binds)
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
configFile := filepath.Join(tmpDir, "config.kdl")
|
||||
if err := os.WriteFile(configFile, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("Failed to write temp file: %v", err)
|
||||
}
|
||||
|
||||
result, err := ParseNiriKeys(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse generated content: %v\nContent was:\n%s", err, content)
|
||||
}
|
||||
|
||||
if len(result.Section.Keybinds) != 4 {
|
||||
t.Errorf("Expected 4 keybinds after round-trip, got %d", len(result.Section.Keybinds))
|
||||
}
|
||||
|
||||
foundFocusWS1 := false
|
||||
foundMoveWS1 := false
|
||||
foundSetWidth := false
|
||||
|
||||
for _, kb := range result.Section.Keybinds {
|
||||
switch {
|
||||
case kb.Action == "focus-workspace" && len(kb.Args) > 0 && kb.Args[0] == "1":
|
||||
foundFocusWS1 = true
|
||||
case kb.Action == "move-column-to-workspace" && len(kb.Args) > 0 && kb.Args[0] == "1":
|
||||
foundMoveWS1 = true
|
||||
case kb.Action == "set-column-width" && len(kb.Args) > 0 && kb.Args[0] == "-10%":
|
||||
foundSetWidth = true
|
||||
}
|
||||
}
|
||||
|
||||
if !foundFocusWS1 {
|
||||
t.Error("focus-workspace 1 not found after round-trip")
|
||||
}
|
||||
if !foundMoveWS1 {
|
||||
t.Error("move-column-to-workspace 1 not found after round-trip")
|
||||
}
|
||||
if !foundSetWidth {
|
||||
t.Error("set-column-width -10% not found after round-trip")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user