1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2025-01-30 21:05:02 +01:00

feat(yazi): Add nice messages for conflicting keybinds

This commit is contained in:
lordkekz 2025-01-24 13:55:18 +01:00
parent 806bec4b8e
commit f786cb3474
No known key found for this signature in database
GPG key ID: 340729DA7E86CF0F

View file

@ -275,13 +275,13 @@ in {
''; '';
}) cfg.${opt}; }) cfg.${opt};
mkAssertNoConflictedKeybinds = keymapSection: let mkAssertNoConflictedKeybinds = keymapSectionName: keymapSection: let
allCustomKeybinds = pipe keymapSection [ allCustomKeybinds = pipe keymapSection [
(collect isList) (collect isList)
(concatLists) (concatLists)
(map (x: setAttr x "remainingSequence" x.on)) (map (x: setAttr x "remainingSequence" x.on))
]; ];
seqKeysStructured = remainingKeybinds: with lib; let seqKeysStructured = remainingKeybinds: let
currentKeys = pipe remainingKeybinds [ currentKeys = pipe remainingKeybinds [
(catAttrs "remainingSequence") (catAttrs "remainingSequence")
(filter (x: x != [])) (filter (x: x != []))
@ -300,18 +300,42 @@ in {
(map dropCurrentKey) (map dropCurrentKey)
seqKeysStructured seqKeysStructured
])) // { ])) // {
# If any keybinds end here, put all remaining keybinds as conflicting # If any keybinds end here, put all keybinds which have this one as a prefix as conflicting
"_" = optionals (any hasNoRemainingKeys remainingKeybinds) remainingKeybinds; "_" = optionals (any hasNoRemainingKeys remainingKeybinds) remainingKeybinds;
}; };
# list of (list of keybinds with identical sequence) # potentialConflicts :: list of (list of keybinds with identical sequence)
# e.g. [ [["a" "b"] ["a" "b"]] ["c"] ] has one conflict for "a" then "b" but no conflict for "c" # The following example has one conflict for keys "a" followed by "b"
# and a conflict for keys "c" only and keys "c" followed by "d"
# but no conflict for key "e"
# [
# # First attrset of seqKeysStructured
# [
# { remainingSequence = []; on = ["a" "b"]; run = "escape"; ... }
# { remainingSequence = []; on = ["a" "b"]; run = "quit"; ... }
# ]
# [
# { remainingSequence = []; on = ["c"]; run = "open"; ...}
# ]
# # Generated in second attrset of seqKeysStructured, see "_" = ...
# [
# { remainingSequence = []; on = ["c" "d"]; run = "escape"; ... }
# { remainingSequence = []; on = ["c"]; run = "quit"; ... }
# ]
# ]
potentialConflicts = collect isList (seqKeysStructured allCustomKeybinds); potentialConflicts = collect isList (seqKeysStructured allCustomKeybinds);
conflicts = filter (x: length x != 1) potentialConflicts; conflicts = filter (x: length x > 1) potentialConflicts;
listConflictingKeybinds = conflict: concatMapStrings (keybind: ''
${builtins.toJSON (removeAttrs keybind ["remainingSequence"])}
'') conflict;
conflictsMessages = concatMapStringsSep "\n" (c: ''
Conflict between:
${listConflictingKeybinds c}'') conflicts; # Lack of newline intended
in { in {
assertion = conflicts == []; assertion = conflicts == [];
message = '' message = lib.trace conflicts ''
There are conflicting keybinds! There are conflicting keybinds in: `programs.yazi.keymap.${keymapSectionName}`
${lib.trace conflicts ""} ${conflictsMessages}
''; '';
}; };
in concatLists [ in concatLists [
@ -325,7 +349,7 @@ in {
]) ])
(mkAsserts "plugins" [ "init.lua" ]) (mkAsserts "plugins" [ "init.lua" ])
# Assert uniqueness for each layer independently # Assert uniqueness for each layer independently
(mapAttrsToList (_: mkAssertNoConflictedKeybinds) cfg.keymap) (mapAttrsToList (mkAssertNoConflictedKeybinds) cfg.keymap)
]; ];
}; };
} }