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:
parent
ae62fd8ad8
commit
ea24675e4f
3 changed files with 42 additions and 24 deletions
|
|
@ -116,35 +116,35 @@
|
|||
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 + " ")
|
||||
]
|
||||
);
|
||||
|
||||
children = lib.filterAttrs (
|
||||
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"
|
||||
])
|
||||
) attrs;
|
||||
in
|
||||
''
|
||||
${name} ${optArgsString}${optPropsString}{
|
||||
${indentStrings (mapAttrsToList convertAttributeToKDL children)}
|
||||
))
|
||||
(mapAttrsToList convertAttributeToKDL)
|
||||
];
|
||||
children = orderedChildren ++ unorderedChildren;
|
||||
optChildren = lib.optional (children != [ ]) ''
|
||||
{
|
||||
${indentStrings children}
|
||||
}'';
|
||||
|
||||
in
|
||||
lib.concatStringsSep " " ([ name ] ++ optArgs ++ optProps ++ optChildren);
|
||||
|
||||
# List Conversion
|
||||
# String -> ListOf (OneOf [Int Float String Bool Null]) -> String
|
||||
convertListOfFlatAttrsToKDL =
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue