Skip to content

Commit

Permalink
refactor: (hopefully) make the code more intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
thecaralice committed Jun 19, 2024
1 parent 82dc325 commit 3a27228
Showing 1 changed file with 40 additions and 73 deletions.
113 changes: 40 additions & 73 deletions lib/colors.nix
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ let
g = builtins.substring 2 2 hex;
b = builtins.substring 4 2 hex;
};
inherit (builtins) listToAttrs attrValues;
uncurry = lib.foldl lib.id;
mapAttrValues = compose [
lib.const
lib.mapAttrs
];
_color =
hex:
lib.fix (self: {
Expand All @@ -96,8 +90,8 @@ let
withHashtag = "#${self.hex}";
};
__toString = self: self.hex;
rgb = mapAttrValues toIntBase16 (splitRGB self.hex);
dec = mapAttrValues (x: x / 255.0) self.rgb;
rgb = lib.mapAttrs (_: toIntBase16) (splitRGB self.hex);
dec = lib.mapAttrs (_: x: x / 255.0) self.rgb;
});

ansi-list =
Expand Down Expand Up @@ -146,37 +140,31 @@ let
palette:
let
ansi' = ansi palette;
color-list = [
"red"
"green"
"orange"
"blue"
"magenta"
"cyan"
];
# everything but first (black) and last (white) elements
ansi-list = compose [
lib.tail
lib.init
];
in
{
inherit (ansi'.dark)
red
green
cyan
blue
magenta
;
orange = ansi'.dark.yellow;
builtins.listToAttrs (
lib.zipListsWith lib.nameValuePair (color-list ++ map (x: "bright-${x}") color-list) (
ansi-list ansi'.dark.toList ++ ansi-list ansi'.bright.toList
)
)
// {
yellow = palette.base0A;
brown = palette.base0F;
}
// builtins.listToAttrs (
lib.zipListsWith lib.nameValuePair
(map (x: "bright-${x}") [
"red"
"green"
"orange"
"blue"
"magenta"
"cyan"
])
(
lib.pipe ansi'.bright.toList [
lib.tail
lib.init
]
)
);
};

# paths like `hex`, `rgb.r` etc.
paths =
[
[ ]
Expand Down Expand Up @@ -209,72 +197,51 @@ let
);

base-colors = lib.mapAttrs (lib.const _color) base;
prepend = compose [
lib.singleton
lib.concat
];
# transforms a list of colors to a flat list expected by base16 templates
based = lib.concatMapAttrs (
name: value:
listToAttrs (
map (
p:
lib.pipe
[
(compose [
(prepend name)
(lib.concatStringsSep "-")
])
(lib.flip lib.getAttrFromPath value)
]
[
(map (lib.flip lib.id p))
(uncurry lib.nameValuePair)
]
) paths
builtins.listToAttrs (
lib.forEach paths (path: {
name = lib.concatStringsSep "-" ([ name ] ++ path);
value = lib.getAttrFromPath path value;
})
)
);

inherit (builtins) isList isAttrs;

/*
Maps a value by function `f`, recurring whenever `cond` is `true`
Maps a value by function `f`, recurring whenever `cond` is `true`
and the value under question is either attrs or list.
*/
mapRecursiveCond =
cond: f:
let
inherit (builtins) isAttrs isList;
in
lib.fix (
self: val:
(
if isAttrs val && cond val then
mapAttrValues self
else if isList val && cond val then
map self
else
f
)
val
if isAttrs val && cond val then
lib.mapAttrs (_: self) val
else if isList val && cond val then
map self val
else
f val
);

extra =
palette:
mnemonic palette
// {
ansi = ansi palette;
toList = attrValues palette;
toList = builtins.attrValues palette;
};
total = palette: based palette // extra palette;
total' =
palette:
mapRecursiveCond (x: lib.strings.isConvertibleWithToString x -> builtins.isList x) toString (
total palette
)
mapRecursiveCond (x: lib.strings.isConvertibleWithToString x -> isList x) toString (total palette)
// {
original = palette // extra palette;
};

withHashtag = mapAttrValues (lib.flip lib.mergeAttrs { __toString = self: self.hex.withHashtag; });
withHashtag = lib.mapAttrs (_: x: x // { __toString = self: self.hex.withHashtag; });
in
total' base-colors // { withHashtag = total' (withHashtag base-colors); };
in
Expand Down

0 comments on commit 3a27228

Please sign in to comment.