cosmic: reformat module

This commit is contained in:
atagen 2024-08-01 13:21:30 +10:00
parent e59393c64a
commit c88b8ebf61

View file

@ -1,10 +1,8 @@
{ { pkgs, lib, config, ... }:
pkgs, let
lib, inherit (lib)
config, filterAttrs concatStrings concatStringsSep mapAttrsToList concatLists
... foldlAttrs concatMapAttrs mapAttrs' nameValuePair boolToString;
}: let
inherit (lib) filterAttrs concatStrings concatStringsSep mapAttrsToList concatLists foldlAttrs concatMapAttrs mapAttrs' nameValuePair boolToString;
inherit (builtins) typeOf toString stringLength; inherit (builtins) typeOf toString stringLength;
# build up serialisation machinery from here for various types # build up serialisation machinery from here for various types
@ -13,24 +11,27 @@
array = a: "[${concatStringsSep "," a}]"; array = a: "[${concatStringsSep "," a}]";
# attrset -> hashmap # attrset -> hashmap
_assoc = a: mapAttrsToList (name: val: "${name}: ${val}") a; _assoc = a: mapAttrsToList (name: val: "${name}: ${val}") a;
assoc = a: '' { assoc = a: ''
${concatStringsSep ",\n" (concatLists (map _assoc a))} {
${
concatStringsSep ''
,
'' (concatLists (map _assoc a))
}
}''; }'';
# attrset -> struct # attrset -> struct
_struct_kv = k: v: _struct_kv = k: v:
if v == null if v == null then
then "" ""
else (concatStringsSep ":" [k (serialise.${typeOf v} v)]); else
(concatStringsSep ":" [ k (serialise.${typeOf v} v) ]);
_struct_concat = s: _struct_concat = s:
foldlAttrs ( foldlAttrs (acc: k: v:
acc: k: v: if stringLength acc > 0 then
if stringLength acc > 0 concatStringsSep ", " [ acc (_struct_kv k v) ]
then concatStringsSep ", " [acc (_struct_kv k v)] else
else _struct_kv k v _struct_kv k v) "" s;
) "" _struct_filt = s: _struct_concat (filterAttrs (k: v: v != null) s);
s;
_struct_filt = s:
_struct_concat (filterAttrs (k: v: v != null) s);
struct = s: "(${_struct_filt s})"; struct = s: "(${_struct_filt s})";
toQuotedString = s: ''"${toString s}"''; toQuotedString = s: ''"${toString s}"'';
@ -50,49 +51,42 @@
defineBinding = binding: defineBinding = binding:
struct { struct {
inherit (binding) modifiers; inherit (binding) modifiers;
key = key = if isNull binding.key then null else toQuotedString binding.key;
if isNull binding.key
then null
else toQuotedString binding.key;
}; };
# map keybinding from list of attrset to hashmap of (mod,key): action # map keybinding from list of attrset to hashmap of (mod,key): action
_mapBindings = bindings: _mapBindings = bindings:
map ( map (inner: {
inner: {"${defineBinding inner}" = maybeToString (checkAction inner.action);} "${defineBinding inner}" = maybeToString (checkAction inner.action);
) }) bindings;
bindings; mapBindings = bindings: assoc (_mapBindings bindings);
mapBindings = bindings:
assoc (_mapBindings bindings);
# check a keybinding's action # check a keybinding's action
# escape with quotes if it's a Spawn action # escape with quotes if it's a Spawn action
checkAction = a: checkAction = a:
if typeOf a == "set" && a.type == "Spawn" if typeOf a == "set" && a.type == "Spawn" then {
then {
inherit (a) type; inherit (a) type;
data = toQuotedString a.data; data = toQuotedString a.data;
} } else
else a; a;
maybeToString = s: maybeToString = s:
if typeOf s == "set" if typeOf s == "set" then
then concatStrings [s.type "(" (toString s.data) ")"] concatStrings [ s.type "(" (toString s.data) ")" ]
else s; else
s;
mapCosmicSettings = application: options: mapCosmicSettings = application: options:
mapAttrs' (k: v: mapAttrs' (k: v:
nameValuePair "cosmic/${application}/v${options.version}/${k}" { nameValuePair "cosmic/${application}/v${options.version}/${k}" {
enable = true; enable = true;
text = serialise.${typeOf v} v; text = serialise.${typeOf v} v;
}) }) options.option;
options.option;
cfg = config.programs.cosmic; cfg = config.programs.cosmic;
in { in {
options.programs.cosmic = { options.programs.cosmic = {
enable = with lib; enable = with lib; mkEnableOption "COSMIC DE";
mkEnableOption "COSMIC DE";
defaultKeybindings = with lib; defaultKeybindings = with lib;
mkOption { mkOption {
@ -103,13 +97,13 @@ in {
keybindings = with lib; keybindings = with lib;
mkOption { mkOption {
default = []; default = [ ];
type = with types; type = with types;
listOf (submodule { listOf (submodule {
options = { options = {
modifiers = mkOption { modifiers = mkOption {
type = listOf str; type = listOf str;
default = []; default = [ ];
}; };
key = mkOption { key = mkOption {
type = nullOr str; type = nullOr str;
@ -118,11 +112,9 @@ in {
action = mkOption { action = mkOption {
type = either str (submodule { type = either str (submodule {
options = { options = {
type = mkOption { type = mkOption { type = str; };
type = str;
};
data = mkOption { data = mkOption {
type = oneOf [str int]; type = oneOf [ str int ];
default = ""; default = "";
}; };
}; };
@ -164,7 +156,7 @@ in {
settings = with lib; settings = with lib;
mkOption { mkOption {
default = {}; default = { };
type = with types; type = with types;
attrsOf (submodule { attrsOf (submodule {
options = { options = {
@ -172,9 +164,7 @@ in {
type = str; type = str;
default = "1"; default = "1";
}; };
option = mkOption { option = mkOption { type = attrsOf anything; };
type = attrsOf anything;
};
}; };
}); });
description = '' description = ''
@ -191,20 +181,18 @@ in {
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
assertions = [(hm.assertions.assertPlatform "programs.cosmic" pkgs platforms.linux)]; assertions =
[ (hm.assertions.assertPlatform "programs.cosmic" pkgs platforms.linux) ];
xdg.configFile = xdg.configFile = {
{ "cosmic/com.system76.CosmicSettings.Shortcuts/v1/custom".text =
"cosmic/com.system76.CosmicSettings.Shortcuts/v1/custom".text = mapBindings cfg.keybindings; mapBindings cfg.keybindings;
"cosmic/com.system76.CosmicSettings.Shortcuts/v1/defaults" = { "cosmic/com.system76.CosmicSettings.Shortcuts/v1/defaults" = {
text = "{}"; text = "{}";
enable = !cfg.defaultKeybindings; enable = !cfg.defaultKeybindings;
}; };
} } // concatMapAttrs
// concatMapAttrs ( (application: options: mapCosmicSettings application options)
application: options:
mapCosmicSettings application options
)
config.programs.cosmic.settings; config.programs.cosmic.settings;
}; };
} }