lib/generators: toHyprconf init

Create shared hyprlang generator for mainting Hyprland ecosystem.
This commit is contained in:
Austin Horstman 2024-04-26 21:31:43 -05:00 committed by Robert Helgesson
parent 1a91cb7cdb
commit 9fdd301a5e
No known key found for this signature in database
GPG key ID: 96E745BD17AA17ED

View file

@ -1,6 +1,49 @@
{ lib }:
{
toHyprconf = { attrs, indentLevel ? 0, importantPrefixes ? [ "$" ], }:
let
inherit (lib)
all concatMapStringsSep concatStrings concatStringsSep filterAttrs foldl
generators hasPrefix isAttrs isList mapAttrsToList replicate;
initialIndent = concatStrings (replicate indentLevel " ");
toHyprconf' = indent: attrs:
let
sections =
filterAttrs (n: v: isAttrs v || (isList v && all isAttrs v)) attrs;
mkSection = n: attrs:
if lib.isList attrs then
(concatMapStringsSep "\n" (a: mkSection n a) attrs)
else ''
${indent}${n} {
${toHyprconf' " ${indent}" attrs}${indent}}
'';
mkFields = generators.toKeyValue {
listsAsDuplicateKeys = true;
inherit indent;
};
allFields =
filterAttrs (n: v: !(isAttrs v || (isList v && all isAttrs v)))
attrs;
isImportantField = n: _:
foldl (acc: prev: if hasPrefix prev n then true else acc) false
importantPrefixes;
importantFields = filterAttrs isImportantField allFields;
fields = builtins.removeAttrs allFields
(mapAttrsToList (n: _: n) importantFields);
in mkFields importantFields
+ concatStringsSep "\n" (mapAttrsToList mkSection sections)
+ mkFields fields;
in toHyprconf' initialIndent attrs;
toKDL = { }:
let
inherit (lib) concatStringsSep splitString mapAttrsToList any;