Initial commit
This commit is contained in:
49
overlays/lib/attrsets.nix
Normal file
49
overlays/lib/attrsets.nix
Normal file
@@ -0,0 +1,49 @@
|
||||
final: prev:
|
||||
let
|
||||
inherit (prev.lib.attrsets) mapAttrs isAttrs filterAttrs listToAttrs nameValuePair attrNames mapAttrsToList;
|
||||
inherit (prev.lib.lists) foldr imap0 imap1;
|
||||
in prev.lib.attrsets // rec {
|
||||
# a -> [String] -> AttrSet{a}
|
||||
mapToAttrsWithConst = constant: items:
|
||||
listToAttrs (map (name: nameValuePair name constant) items);
|
||||
|
||||
# [AttrSet] -> AttrSet
|
||||
concatAttrs = foldr (a: b: a // b) {};
|
||||
|
||||
# (Int -> String -> a -> a) -> AttrSet -> AttrSet
|
||||
imap0Attrs = f: set:
|
||||
listToAttrs (imap0 (i: attr: nameValuePair attr (f i attr set.${attr})) (attrNames set));
|
||||
|
||||
# (Int -> String -> a -> a) -> AttrSet -> AttrSet
|
||||
imap1Attrs = f: set:
|
||||
listToAttrs (imap1 (i: attr: nameValuePair attr (f i attr set.${attr})) (attrNames set));
|
||||
|
||||
# (Int -> String -> a -> nameValuePair) -> AttrSet -> AttrSet
|
||||
imap0Attrs' = f: set:
|
||||
listToAttrs (imap0 (i: attr: f i attr set.${attr}) (attrNames set));
|
||||
|
||||
# (Int -> String -> a -> nameValuePair) -> AttrSet -> AttrSet
|
||||
imap1Attrs' = f: set:
|
||||
listToAttrs (imap1 (i: attr: f i attr set.${attr}) (attrNames set));
|
||||
|
||||
# AttrSet -> AttrSet
|
||||
recursivelyFlatten = set: let
|
||||
shouldRecurse = filterAttrs (n: v: isAttrs v) set;
|
||||
shouldNotRecurse = filterAttrs (n: v: !(isAttrs v)) set;
|
||||
recursedAttrs = mapAttrsToList (n: v: recursivelyFlatten v) shouldRecurse;
|
||||
in
|
||||
concatAttrs ([shouldNotRecurse] ++ recursedAttrs);
|
||||
|
||||
# Takes in a predicate which decides whether or not to recurse further. (true -> recurse)
|
||||
# This will let you recurse until you recurse until you hit attrsets with a special meaning
|
||||
# that you would like to handle after flattening.
|
||||
# It will also stop at everything other than an attribute set.
|
||||
#
|
||||
# (a -> Bool) -> AttrSet -> AttrSet
|
||||
recursivelyFlattenUntil = pred: set: let
|
||||
shouldRecurse = filterAttrs (n: v: isAttrs v && !(pred v)) set;
|
||||
shouldNotRecurse = filterAttrs (n: v: !(isAttrs v) || pred v) set;
|
||||
recursedAttrs = mapAttrsToList (n: v: recursivelyFlattenUntil pred v) shouldRecurse;
|
||||
in
|
||||
concatAttrs ([shouldNotRecurse] ++ recursedAttrs);
|
||||
}
|
10
overlays/lib/default.nix
Normal file
10
overlays/lib/default.nix
Normal file
@@ -0,0 +1,10 @@
|
||||
final: prev:
|
||||
{
|
||||
lib = prev.lib // {
|
||||
attrsets = (import ./attrsets.nix) final prev;
|
||||
lists = (import ./lists.nix) final prev;
|
||||
strings = (import ./strings.nix) final prev;
|
||||
termColors = (import ./termColors.nix) final prev;
|
||||
trivial = (import ./trivial.nix) final prev;
|
||||
};
|
||||
}
|
14
overlays/lib/lists.nix
Normal file
14
overlays/lib/lists.nix
Normal file
@@ -0,0 +1,14 @@
|
||||
final: prev:
|
||||
let
|
||||
inherit (prev.lib.trivial) const;
|
||||
inherit (prev.lib.lists) range any all;
|
||||
in prev.lib.lists // {
|
||||
# a -> Int -> [a]
|
||||
repeat = item: times: map (const item) (range 1 times);
|
||||
|
||||
# [Bool] -> Bool
|
||||
any' = any (boolean: boolean);
|
||||
|
||||
# [Bool] -> Bool
|
||||
all' = all (boolean: boolean);
|
||||
}
|
40
overlays/lib/strings.nix
Normal file
40
overlays/lib/strings.nix
Normal file
@@ -0,0 +1,40 @@
|
||||
final: prev:
|
||||
let
|
||||
inherit (final.lib.lists) repeat length;
|
||||
inherit (prev.lib.strings) concatStringsSep replaceStrings splitString;
|
||||
# inherit (final.lib.strings) wrap;
|
||||
in prev.lib.strings // rec {
|
||||
# String -> [String]
|
||||
lines = splitString "\n";
|
||||
|
||||
# String -> (String -> String) -> String -> String
|
||||
splitMap = splitter: f: string:
|
||||
concatStringsSep splitter (map f (splitString splitter string));
|
||||
|
||||
# (String -> String) -> String -> String
|
||||
mapLines = splitMap "\n";
|
||||
|
||||
# String -> Int -> String
|
||||
repeatString = string: times: concatStringsSep "" (repeat string times);
|
||||
|
||||
# Replaces any occurences in a list of strings with a single replacement.
|
||||
# NOTE: This function does not support regex patterns.
|
||||
#
|
||||
# [String] -> String -> String -> String
|
||||
replaceStrings' = from: to: replaceStrings from (repeat to (length from));
|
||||
|
||||
# [String] -> String
|
||||
unlines = concatStringsSep "\n";
|
||||
|
||||
# [String] -> String
|
||||
unwords = concatStringsSep " ";
|
||||
|
||||
# String -> [String]
|
||||
words = builtins.split "\\s+";
|
||||
|
||||
# String -> String -> String -> String
|
||||
wrap = start: end: string: start + string + end;
|
||||
|
||||
# String -> String -> String
|
||||
wrap' = wrapper: wrap wrapper wrapper;
|
||||
}
|
49
overlays/lib/termColors.nix
Normal file
49
overlays/lib/termColors.nix
Normal file
@@ -0,0 +1,49 @@
|
||||
final: prev:
|
||||
let
|
||||
inherit (final.lib.strings) wrap;
|
||||
inherit (prev.lib.attrsets) mapAttrs' nameValuePair;
|
||||
# inherit (final.lib.myStuff.termColors) escapeCharacter escapeColor resetCharacter wrapWithColor' colorMappings;
|
||||
in rec {
|
||||
# String
|
||||
escapeCharacter = "";
|
||||
|
||||
# String -> String
|
||||
escapeColor = color: "${escapeCharacter}[${color}m";
|
||||
|
||||
# String
|
||||
resetCharacter = escapeColor "0";
|
||||
|
||||
# String -> String -> String
|
||||
wrapWithColor = color: wrap color resetCharacter;
|
||||
|
||||
# String -> String -> String
|
||||
wrapWithColor' = color: wrap (escapeColor color) resetCharacter;
|
||||
|
||||
# AttrSet{String}
|
||||
colorMappings = {
|
||||
"black" = "0";
|
||||
"red" = "1";
|
||||
"green" = "2";
|
||||
"yellow" = "3";
|
||||
"blue" = "4";
|
||||
"magenta" = "5";
|
||||
"cyan" = "6";
|
||||
"white" = "7";
|
||||
};
|
||||
|
||||
# AttrSet{(String -> String)}
|
||||
front = let
|
||||
# AttrSet{(String -> String)}
|
||||
names = mapAttrs' (n: v: nameValuePair n (wrapWithColor' ("3" + v))) colorMappings;
|
||||
|
||||
# AttrSet{(String -> String)}
|
||||
numbers = mapAttrs' (n: v: nameValuePair v (wrapWithColor' ("3" + v))) colorMappings;
|
||||
in names // numbers;
|
||||
back = let
|
||||
# AttrSet{(String -> String)}
|
||||
names = mapAttrs' (n: v: nameValuePair n (wrapWithColor' ("4" + v))) colorMappings;
|
||||
|
||||
# AttrSet{(String -> String)}
|
||||
numbers = mapAttrs' (n: v: nameValuePair v (wrapWithColor' ("4" + v))) colorMappings;
|
||||
in names // numbers;
|
||||
}
|
7
overlays/lib/trivial.nix
Normal file
7
overlays/lib/trivial.nix
Normal file
@@ -0,0 +1,7 @@
|
||||
final: prev:
|
||||
let
|
||||
in prev.lib.trivial // {
|
||||
# a -> b -> Either (a b)
|
||||
withDefault = default: value:
|
||||
if (value == null) then default else value;
|
||||
}
|
Reference in New Issue
Block a user