Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generators: new toKDL parser #4614

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 40 additions & 75 deletions modules/lib/generators.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@
{
toKDL = { }:
let
inherit (lib) concatStringsSep splitString mapAttrsToList any;
inherit (lib) mapAttrsToList;
inherit (builtins) typeOf replaceStrings elem;
inherit (lib.lists) flatten;

# ListOf String -> String
indentStrings = let
# Although the input of this function is a list of strings,
# the strings themselves *will* contain newlines, so you need
# to normalize the list by joining and resplitting them.
unlines = lib.splitString "\n";
lines = lib.concatStringsSep "\n";
indentAll = lines: concatStringsSep "\n" (map (x: " " + x) lines);
in stringsWithNewlines: indentAll (unlines (lines stringsWithNewlines));
indentListOfStrings = list: map (x: " " + x) list;

# String -> String
sanitizeString = replaceStrings [ "\n" ''"'' ] [ "\\n" ''\"'' ];
sanitizeName = replaceStrings [ "'" ] [ ''"'' ];
sanitizeValue = replaceStrings [ "\n" ''"'' ] [ "\\n" ''\"'' ];

isLiteral = element:
(elem (typeOf element) [ "int" "float" "bool" "null" "string" ]);

# OneOf [Int Float String Bool Null] -> String
literalValueToString = element:
lib.throwIfNot
(elem (typeOf element) [ "int" "float" "string" "bool" "null" ])
convertLiteralValueToString = isName: element:
lib.throwIfNot (isLiteral element)
"Cannot convert value of type ${typeOf element} to KDL literal."
(if typeOf element == "null" then
"null"
Expand All @@ -31,72 +27,41 @@
else if element == true then
"true"
else if typeOf element == "string" then
''"${sanitizeString element}"''
if isName then
sanitizeName element
else
''"'' + sanitizeValue element + ''"''
else
toString element);

# Attrset Conversion
# String -> AttrsOf Anything -> String
convertAttrsToKDL = name: attrs:
let
optArgsString = lib.optionalString (attrs ? "_args")
(lib.pipe attrs._args [
(map literalValueToString)
(lib.concatStringsSep " ")
(s: s + " ")
]);

optPropsString = lib.optionalString (attrs ? "_props")
(lib.pipe attrs._props [
(lib.mapAttrsToList
(name: value: "${name}=${literalValueToString value}"))
(lib.concatStringsSep " ")
(s: s + " ")
]);

children =
lib.filterAttrs (name: _: !(elem name [ "_args" "_props" ])) attrs;
in ''
${name} ${optArgsString}${optPropsString}{
${indentStrings (mapAttrsToList convertAttributeToKDL children)}
}'';

# List Conversion
# String -> ListOf (OneOf [Int Float String Bool Null]) -> String
convertListOfFlatAttrsToKDL = name: list:
let flatElements = map literalValueToString list;
in "${name} ${concatStringsSep " " flatElements}";
convertListElement = elem:
if builtins.isAttrs elem then
convertSetToKDL elem
else
builtins.trace elem (convertLiteralValueToString true elem);

# String -> ListOf Anything -> String
convertListOfNonFlatAttrsToKDL = name: list: ''
${name} {
${indentStrings (map (x: convertAttributeToKDL "-" x) list)}
}'';
# flatten list first since hierarchical lists are equivalent to flattened lists
# in KDL
convertListToKDL = list: flatten (map convertListElement (flatten list));

# String -> ListOf Anything -> String
convertListToKDL = name: list:
let elementsAreFlat = !any (el: elem (typeOf el) [ "list" "set" ]) list;
in if elementsAreFlat then
convertListOfFlatAttrsToKDL name list
convertSetElement = name: value:
let
emptyRet = [ (sanitizeName name) ];
prefix = [ (sanitizeName name + " {") ];
suffix = [ "}" ];
in if builtins.isList value then
if value == [ ] then
emptyRet
else
prefix ++ indentListOfStrings (convertListToKDL value) ++ suffix
else if builtins.isAttrs value then
if value == { } then
emptyRet
else
prefix ++ indentListOfStrings (convertSetToKDL value) ++ suffix
else
convertListOfNonFlatAttrsToKDL name list;
sanitizeName name + " " + (convertLiteralValueToString false value);

# Combined Conversion
# String -> Anything -> String
convertAttributeToKDL = name: value:
let vType = typeOf value;
in if elem vType [ "int" "float" "bool" "null" "string" ] then
"${name} ${literalValueToString value}"
else if vType == "set" then
convertAttrsToKDL name value
else if vType == "list" then
convertListToKDL name value
else
throw ''
Cannot convert type `(${typeOf value})` to KDL:
${name} = ${toString value}
'';
in attrs: ''
${concatStringsSep "\n" (mapAttrsToList convertAttributeToKDL attrs)}
'';
convertSetToKDL = set: flatten (mapAttrsToList convertSetElement set);
in attrs: lib.concatStringsSep "\n" (flatten (convertSetToKDL attrs));
}
5 changes: 4 additions & 1 deletion tests/lib/generators/default.nix
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{ generators-tokdl = ./tokdl.nix; }
{
generators-tokdl-types = ./tokdl-types.nix;
generators-tokdl-zellij = ./tokdl-zellij.nix;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ listInAttrsInList {
}
}
nested {
- 1 2
- true false
-
- null
1 2
true false

null
}
unsafeString " \" \n "
46 changes: 46 additions & 0 deletions tests/lib/generators/result-tokdl-zellij.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
default_mode "locked"
keybinds {
locked {
bind "Alt j" {
MoveFocus "Down"
}
bind "Alt k" {
MoveFocus "Up"
}
bind "Alt h" {
MoveFocusOrTab "Left"
}
bind "Alt l" {
MoveFocusOrTab "Right"
}
}
normal {
bind "Alt h" {
MoveFocusOrTab "Left"
}
bind "Alt j" {
MoveFocus "Down"
}
bind "Alt k" {
MoveFocus "Up"
}
bind "Alt l" {
MoveFocusOrTab "Right"
}
}
pane {
bind "t" {
CloseFocus
CloseTab
}
}
}
layouts {
pane size=1 borderless=true {
plugin "location=\"zellij:tab-bar\""
}
pane
pane size=2 borderless=true {
plugin location="zellij:status-bar"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@
{ b = true; }
{
c = null;
d = [{ e = "asdfadfasdfasdf"; }];
d = [{ e = ''"asdfadfasdfasdf"''; }];
}
[ [ [ "test" ] ] ]
];
list2 = [{ a = 8; }];
};
layout = [ ''unbind "a"'' ''unbind "b"'' ];
};

nmt.script = ''
assertFileContent \
home-files/result.txt \
${./tokdl-result.txt}
${./result-tokdl-types.txt}
'';
}
50 changes: 50 additions & 0 deletions tests/lib/generators/tokdl-zellij.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{ config, lib, ... }:

{
home.file."result.txt".text = lib.hm.generators.toKDL { } {
default_mode = "locked";
keybinds = {

# will be sorted
locked = {
"bind 'Alt h'" = { MoveFocusOrTab = "Left"; };
"bind 'Alt l'" = { MoveFocusOrTab = "Right"; };
"bind \"Alt j\"" = { MoveFocus = "Down"; };
"bind \"Alt k\"" = { MoveFocus = "Up"; };
};

# will not be sorted
normal = [
{ "bind 'Alt h'" = { MoveFocusOrTab = "Left"; }; }
{ "bind \"Alt j\"" = { MoveFocus = "Down"; }; }
{ "bind \"Alt k\"" = { MoveFocus = "Up"; }; }
{ "bind 'Alt l'" = { MoveFocusOrTab = "Right"; }; }
];

pane = {
"bind \"t\"" = {
CloseTab = [ ];
CloseFocus = [ ];
};
};
};
layouts = [
{
"pane size=1 borderless=true" = {
plugin = ''location="zellij:tab-bar"'';
};
}
"pane"
{
"pane size=2 borderless=true" =
[ ''plugin location="zellij:status-bar"'' ];
}
];
};

nmt.script = ''
assertFileContent \
home-files/result.txt \
${./result-tokdl-zellij.txt}
'';
}