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 = convertAttrsToKDL =
name: attrs: name: attrs:
let let
optArgsString = lib.optionalString (attrs ? "_args") ( optArgs = map literalValueToString (attrs._args or [ ]);
lib.pipe attrs._args [ optProps = lib.mapAttrsToList (name: value: "${name}=${literalValueToString value}") (
(map literalValueToString) attrs._props or { }
(lib.concatStringsSep " ")
(s: s + " ")
]
); );
optPropsString = lib.optionalString (attrs ? "_props") ( orderedChildren = lib.pipe (attrs._children or [ ]) [
lib.pipe attrs._props [ (map (child: mapAttrsToList convertAttributeToKDL child))
(lib.mapAttrsToList (name: value: "${name}=${literalValueToString value}")) lib.flatten
(lib.concatStringsSep " ") ];
(s: s + " ") 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 in
'' lib.concatStringsSep " " ([ name ] ++ optArgs ++ optProps ++ optChildren);
${name} ${optArgsString}${optPropsString}{
${indentStrings (mapAttrsToList convertAttributeToKDL children)}
}'';
# List Conversion # List Conversion
# String -> ListOf (OneOf [Int Float String Bool Null]) -> String # String -> ListOf (OneOf [Int Float String Bool Null]) -> String

View file

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

View file

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