1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00

lib: Improve KDL generator (#7429)

This adds support for generating ordered children and nodes with
attributes and/or properties but no children. These are both needed to
generate zellij keybinding configuration.
This commit is contained in:
Quentin Smith 2025-07-12 15:32:14 -04:00 committed by GitHub
parent ae62fd8ad8
commit ea24675e4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 24 deletions

View file

@ -116,34 +116,34 @@
convertAttrsToKDL =
name: attrs:
let
optArgsString = lib.optionalString (attrs ? "_args") (
lib.pipe attrs._args [
(map literalValueToString)
(lib.concatStringsSep " ")
(s: s + " ")
]
optArgs = map literalValueToString (attrs._args or [ ]);
optProps = lib.mapAttrsToList (name: value: "${name}=${literalValueToString value}") (
attrs._props or { }
);
optPropsString = lib.optionalString (attrs ? "_props") (
lib.pipe attrs._props [
(lib.mapAttrsToList (name: value: "${name}=${literalValueToString value}"))
(lib.concatStringsSep " ")
(s: s + " ")
]
);
orderedChildren = lib.pipe (attrs._children or [ ]) [
(map (child: mapAttrsToList convertAttributeToKDL child))
lib.flatten
];
unorderedChildren = lib.pipe attrs [
(lib.filterAttrs (
name: _:
!(elem name [
"_args"
"_props"
"_children"
])
))
(mapAttrsToList convertAttributeToKDL)
];
children = orderedChildren ++ unorderedChildren;
optChildren = lib.optional (children != [ ]) ''
{
${indentStrings children}
}'';
children = lib.filterAttrs (
name: _:
!(elem name [
"_args"
"_props"
])
) attrs;
in
''
${name} ${optArgsString}${optPropsString}{
${indentStrings (mapAttrsToList convertAttributeToKDL children)}
}'';
lib.concatStringsSep " " ([ name ] ++ optArgs ++ optProps ++ optChildren);
# List Conversion
# String -> ListOf (OneOf [Int Float String Bool Null]) -> String

View file

@ -1,7 +1,12 @@
a 1
argsAndProps 1 2 a=3
b "string"
bigFlatItems 23847590283751 1.239000 "multiline \" \" \"\nstring\n" null
c "multiline string\nwith special characters:\n\t \n \\" \"\n"
duplicateChildren {
child 2
child 1
}
extraAttrs 2 true arg1=1 arg2=false {
nested {
a 1

View file

@ -38,6 +38,10 @@
[ ]
[ null ]
];
duplicateChildren._children = [
{ child = [ 2 ]; }
{ child = [ 1 ]; }
];
extraAttrs = {
_args = [
2
@ -52,6 +56,15 @@
b = null;
};
};
argsAndProps = {
_args = [
1
2
];
_props = {
a = 3;
};
};
listInAttrsInList = {
list1 = [
{ a = 1; }