bahaghari: update library arguments

It makes it confusing sometimes. Not worth keeping the design.
This commit is contained in:
Gabriel Arazas 2024-03-02 18:10:21 +08:00
parent b25ed0989d
commit 66b35ea2c6
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
10 changed files with 120 additions and 113 deletions

View File

@ -10,7 +10,7 @@
pkgs.lib.makeExtensible
(self:
let
callLibs = file: import file { lib = self; inherit pkgs; };
callLibs = file: import file { inherit (pkgs) lib; inherit pkgs self; };
in
{
trivial = callLibs ./trivial.nix;

View File

@ -1,7 +1,7 @@
# A small utility library for manipulating hexadecimal numbers. It's made in 15
# minutes with a bunch of duct tape on it but it's working for its intended
# purpose.
{ pkgs, lib }:
{ pkgs, lib, self }:
let
glyphList =
@ -24,7 +24,7 @@ let
"F"
];
baseSet = lib.generateBaseDigitType glyphList;
baseSet = self.generateBaseDigitType glyphList;
in
rec {
/* Returns a convenient glyph set for creating your own conversion or
@ -41,7 +41,7 @@ rec {
=> [ "F" "10" "11" ]
*/
range = first: last:
builtins.map (n: baseSet.fromDec n) (pkgs.lib.lists.range first last);
builtins.map (n: baseSet.fromDec n) (lib.lists.range first last);
/* Checks if the given hex string is valid or not.
@ -77,13 +77,13 @@ rec {
*/
pad = n: hex:
let
strLength = pkgs.lib.stringLength hex;
strLength = lib.stringLength hex;
reqWidth = n - strLength;
components = pkgs.lib.genList (_: "0") reqWidth ++ [ hex ];
components = lib.genList (_: "0") reqWidth ++ [ hex ];
in
assert pkgs.lib.assertMsg (isHexString hex)
assert lib.assertMsg (isHexString hex)
"bahaghariLib.hex.pad: given hex number (${hex}) is not valid";
if (reqWidth <= 0)
then hex
else pkgs.lib.concatStringsSep "" components;
else lib.concatStringsSep "" components;
}

View File

@ -1,7 +1,7 @@
# A little math utility for common operations. Don't expect any high-level
# mathematical operations nor godly optimizations expected from a typical math
# library, it's just basic high school type of shit in all aspects.
{ pkgs, lib }:
{ pkgs, lib, self }:
rec {
/* Returns the absolute value of the given number.
@ -53,7 +53,7 @@ rec {
=> true
*/
isWithinRange = min: max: number:
(pkgs.lib.max number min) <= (pkgs.lib.min number max);
(lib.max number min) <= (lib.min number max);
/* Given a number, make it grow by given amount of percentage.
A value of 100 should make the number doubled.
@ -86,7 +86,7 @@ rec {
let
res = grow number value;
in
pkgs.lib.min max (pkgs.lib.max res min);
lib.min max (lib.max res min);
/* Given a number, return its value by the given percentage.

View File

@ -1,16 +1,16 @@
{ pkgs, lib }:
{ pkgs, lib, self }:
# TODO: Remove the legacy scheme support once it is entirely removed in Tinted
# Theming standard.
let
isBaseX = i: palette:
let
paletteNames = pkgs.lib.attrNames palette;
maxDigitLength = pkgs.lib.lists.length (pkgs.lib.toBaseDigits 10 i);
mkBaseAttr = hex: "base${lib.hex.pad maxDigitLength hex}";
schemeNames = builtins.map mkBaseAttr (lib.hex.range 0 (i - 1));
paletteNames = lib.attrNames palette;
maxDigitLength = lib.lists.length (lib.toBaseDigits 10 i);
mkBaseAttr = hex: "base${self.hex.pad maxDigitLength hex}";
schemeNames = builtins.map mkBaseAttr (self.hex.range 0 (i - 1));
in
(pkgs.lib.count (name: pkgs.lib.elem name schemeNames) paletteNames) == i;
(lib.count (name: lib.elem name schemeNames) paletteNames) == i;
in
rec {
# TODO: Return a Nix object to generate a Tinted Theming color scheme from an
@ -105,7 +105,7 @@ rec {
else null;
palette =
pkgs.lib.attrsets.removeAttrs scheme [ "author" "description" "scheme" ];
lib.attrsets.removeAttrs scheme [ "author" "description" "scheme" ];
in
{
inherit (scheme) author;
@ -113,8 +113,8 @@ rec {
name = scheme.scheme;
}
// pkgs.lib.optionalAttrs (scheme?description) { inherit (scheme) description; }
// pkgs.lib.optionalAttrs (system != null) { inherit system; };
// lib.optionalAttrs (scheme?description) { inherit (scheme) description; }
// lib.optionalAttrs (system != null) { inherit system; };
/* Imports a Base16 scheme. This also handles converting the legacy Base16
schema into the new one if it's detected. Take note, every single token
@ -139,9 +139,9 @@ rec {
*/
importScheme = yamlpath:
let
scheme = lib.importYAML yamlpath;
scheme = self.importYAML yamlpath;
in
assert pkgs.lib.assertMsg (isValidScheme scheme || isLegacyScheme scheme)
assert lib.assertMsg (isValidScheme scheme || isLegacyScheme scheme)
"bahaghariLib.tinted-theming.importScheme: given data is not a valid Tinted Theming scheme";
if isLegacyScheme scheme
then modernizeLegacyScheme scheme

View File

@ -1,4 +1,4 @@
{ pkgs, lib }:
{ pkgs, lib, self }:
rec {
inherit (pkgs.lib.generators) toYAML;
@ -21,10 +21,10 @@ rec {
importYAML = path:
let
dataDrv = pkgs.runCommand "convert-yaml-to-json" { } ''
${pkgs.lib.getExe' pkgs.yaml2json "yaml2json"} < "${path}" > "$out"
${lib.getExe' pkgs.yaml2json "yaml2json"} < "${path}" > "$out"
'';
in
pkgs.lib.importJSON dataDrv;
lib.importJSON dataDrv;
/* Convert a given decimal number to a specified base digit with the set of
glyphs for each digit as returned from lib.toBaseDigits.
@ -44,10 +44,10 @@ rec {
*/
toBaseDigitsWithGlyphs = base: i: glyphs:
let
baseDigits = pkgs.lib.toBaseDigits base i;
baseDigits = lib.toBaseDigits base i;
toBaseDigits = d: glyphs.${builtins.toString d};
in
pkgs.lib.concatMapStrings toBaseDigits baseDigits;
lib.concatMapStrings toBaseDigits baseDigits;
/* Generates a glyph set usable for `toBaseDigitsWithGlyphs`. Take note the
given list is assumed to be sorted and the generated glyph set starts at
@ -68,11 +68,11 @@ rec {
generateGlyphSet = glyphsList:
let
glyphsList' =
pkgs.lib.lists.imap0
(i: glyph: pkgs.lib.nameValuePair (builtins.toString i) glyph)
lib.lists.imap0
(i: glyph: lib.nameValuePair (builtins.toString i) glyph)
glyphsList;
in
pkgs.lib.listToAttrs glyphsList';
lib.listToAttrs glyphsList';
/* Generates a conversion table for a sorted list of glyphs to its decimal
number. Suitable for creating your own conversion function. Accepts the
@ -93,11 +93,11 @@ rec {
generateConversionTable = glyphsList:
let
glyphsList' =
pkgs.lib.lists.imap0
(i: glyph: pkgs.lib.nameValuePair glyph i)
lib.lists.imap0
(i: glyph: lib.nameValuePair glyph i)
glyphsList;
in
pkgs.lib.listToAttrs glyphsList';
lib.listToAttrs glyphsList';
/* A factory function for generating an attribute set containing a glyph
set, a conversion table, and a conversion function to and from decimal.
@ -118,7 +118,7 @@ rec {
}
*/
generateBaseDigitType = glyphsList: rec {
base = pkgs.lib.length glyphsList;
base = lib.length glyphsList;
glyphSet = generateGlyphSet glyphsList;
conversionTable = generateConversionTable glyphsList;
@ -129,18 +129,18 @@ rec {
# open a can of worms about implementing this with stringy types.
fromDec = decimal:
let
digits = pkgs.lib.toBaseDigits base decimal;
digits = lib.toBaseDigits base decimal;
in
pkgs.lib.concatMapStrings (d: glyphSet.${builtins.toString d}) digits;
lib.concatMapStrings (d: glyphSet.${builtins.toString d}) digits;
toDec = digit:
let
chars = pkgs.lib.stringToCharacters digit;
maxDigits = (pkgs.lib.length chars) - 1;
chars = lib.stringToCharacters digit;
maxDigits = (lib.length chars) - 1;
convertDigitToDec =
pkgs.lib.lists.imap0 (i: v: conversionTable.${v} * (lib.math.pow base (maxDigits - i))) chars;
lib.lists.imap0 (i: v: conversionTable.${v} * (self.math.pow base (maxDigits - i))) chars;
in
pkgs.lib.foldl (sum: v: sum + v) 0 convertDigitToDec;
lib.foldl (sum: v: sum + v) 0 convertDigitToDec;
};
/* Given a range of two numbers, ensure the value is only returned within the
@ -154,7 +154,10 @@ rec {
clamp (-100) 100 (-234)
=> -100
clamp (-100) 100 54
=> 54
*/
clamp = min: max: value:
pkgs.lib.min max (pkgs.lib.max min value);
lib.min max (lib.max min value);
}

View File

@ -2,10 +2,14 @@
let
lib = import ../../lib { inherit pkgs; };
callLib = file: import file {
inherit (pkgs) lib; inherit pkgs;
self = lib;
};
in
{
hex = import ./hex.nix { inherit pkgs lib; };
math = import ./math.nix { inherit pkgs lib; };
trivial = import ./trivial { inherit pkgs lib; };
tinted-theming = import ./tinted-theming { inherit pkgs lib; };
hex = callLib ./hex.nix;
math = callLib ./math.nix;
trivial = callLib ./trivial;
tinted-theming = callLib ./tinted-theming;
}

View File

@ -1,63 +1,63 @@
{ pkgs, lib }:
{ pkgs, lib, self }:
pkgs.lib.runTests {
lib.runTests {
testToHexString = {
expr = lib.hex.fromDec 293454837;
expr = self.hex.fromDec 293454837;
expected = "117DC3F5";
};
testToHexString2 = {
expr = lib.hex.fromDec 4500;
expr = self.hex.fromDec 4500;
expected = "1194";
};
testToHexString3 = {
expr = lib.hex.fromDec 5942819;
expr = self.hex.fromDec 5942819;
expected = "5AAE23";
};
testHexToDec = {
expr = lib.hex.toDec "FF";
expr = self.hex.toDec "FF";
expected = 255;
};
testHexToDec2 = {
expr = lib.hex.toDec "333FAB333";
expr = self.hex.toDec "333FAB333";
expected = 13756969779;
};
testCreateHexRange = {
expr = lib.hex.range 10 17;
expr = self.hex.range 10 17;
expected = [ "A" "B" "C" "D" "E" "F" "10" "11" ];
};
testCreateHexWithHigherStart = {
expr = lib.hex.range 49 17;
expr = self.hex.range 49 17;
expected = [ ];
};
testIsHexString = {
expr = lib.hex.isHexString "ABC";
expr = self.hex.isHexString "ABC";
expected = true;
};
testIsHexStringWithInvalidHex = {
expr = lib.hex.isHexString "WHAT IS THIS";
expr = self.hex.isHexString "WHAT IS THIS";
expected = false;
};
testHexPad = {
expr = lib.hex.pad 5 "A";
expr = self.hex.pad 5 "A";
expected = "0000A";
};
testHexPadWithLowerMaxDigits = {
expr = lib.hex.pad 1 "9AC";
expr = self.hex.pad 1 "9AC";
expected = "9AC";
};
testHexPadWithNegativeDigits = {
expr = lib.hex.pad (-5) "A42C";
expr = self.hex.pad (-5) "A42C";
expected = "A42C";
};
}

View File

@ -1,98 +1,98 @@
{ pkgs, lib }:
{ pkgs, lib, self }:
pkgs.lib.runTests {
lib.runTests {
testMathPowPositive = {
expr = lib.math.pow 2 8;
expr = self.math.pow 2 8;
expected = 256;
};
testMathPowNegative = {
expr = lib.math.pow 2.0 (-1);
expr = self.math.pow 2.0 (-1);
expected = 0.5;
};
testMathPowZero = {
expr = lib.math.pow 31 0;
expr = self.math.pow 31 0;
expected = 1;
};
testMathAbsoluteValue = {
expr = lib.math.abs 5493;
expr = self.math.abs 5493;
expected = 5493;
};
testMathAbsoluteValue2 = {
expr = lib.math.abs (-435354);
expr = self.math.abs (-435354);
expected = 435354;
};
testMathPercentage = {
expr = lib.math.percentage 50 100;
expr = self.math.percentage 50 100;
expected = 50;
};
testMathPercentage2 = {
expr = lib.math.percentage 13 453;
expr = self.math.percentage 13 453;
expected = 58.89;
};
testMathPercentageNegative = {
expr = lib.math.percentage (-20) 500;
expr = self.math.percentage (-20) 500;
expected = -100;
};
testMathPercentageNegative2 = {
expr = lib.math.percentage (-64) 843;
expr = self.math.percentage (-64) 843;
expected = -539.52;
};
testMathPercentageZero = {
expr = lib.math.percentage 0 45723;
expr = self.math.percentage 0 45723;
expected = 0;
};
testMathPercentageZero2 = {
expr = lib.math.percentage 0 (-3423);
expr = self.math.percentage 0 (-3423);
expected = 0;
};
testMathGrow = {
expr = lib.math.grow 500 12;
expr = self.math.grow 500 12;
expected = 72;
};
testMathGrow2 = {
expr = lib.math.grow 55.5 5.5;
expr = self.math.grow 55.5 5.5;
expected = 8.5525;
};
testMathGrowVariantMax = {
expr = lib.math.grow' 0 255 130 100;
expr = self.math.grow' 0 255 130 100;
expected = 255;
};
testMathGrowVariantMin = {
expr = lib.math.grow' 0 255 130 (-500);
expr = self.math.grow' 0 255 130 (-500);
expected = 0;
};
testMathRoundDown = {
expr = lib.math.round 2.3;
expr = self.math.round 2.3;
expected = 2;
};
testMathRoundUp = {
expr = lib.math.round 2.8;
expr = self.math.round 2.8;
expected = 3;
};
testMathWithinRange = {
expr = lib.math.isWithinRange (-100) 100 50;
expr = self.math.isWithinRange (-100) 100 50;
expected = true;
};
testMathWithinRange2 = {
expr = lib.math.isWithinRange 5 10 (-5);
expr = self.math.isWithinRange 5 10 (-5);
expected = false;
};
}

View File

@ -1,14 +1,14 @@
{ pkgs, lib }:
{ pkgs, lib, self }:
let
sampleBase16Scheme = lib.tinted-theming.importScheme ./sample-base16-scheme.yml;
sampleBase16Scheme' = lib.tinted-theming.importScheme ./sample-base16-scheme-with-missing-colors.yml;
sampleBase24Scheme = lib.tinted-theming.importScheme ./sample-base24-scheme.yml;
sampleBase24Scheme' = lib.tinted-theming.importScheme ./sample-base24-scheme-with-missing-colors.yml;
sampleBase16Scheme = self.tinted-theming.importScheme ./sample-base16-scheme.yml;
sampleBase16Scheme' = self.tinted-theming.importScheme ./sample-base16-scheme-with-missing-colors.yml;
sampleBase24Scheme = self.tinted-theming.importScheme ./sample-base24-scheme.yml;
sampleBase24Scheme' = self.tinted-theming.importScheme ./sample-base24-scheme-with-missing-colors.yml;
in
pkgs.lib.runTests {
lib.runTests {
testTintedThemingSchemeImport = {
expr = lib.tinted-theming.importScheme ./sample-base16-scheme.yml;
expr = self.tinted-theming.importScheme ./sample-base16-scheme.yml;
expected = {
system = "base16";
name = "Bark on a tree";
@ -37,7 +37,7 @@ pkgs.lib.runTests {
};
testTintedThemingLegacyBase24SchemeImport = {
expr = lib.tinted-theming.importScheme ./sample-legacy-base24.yml;
expr = self.tinted-theming.importScheme ./sample-legacy-base24.yml;
expected = {
system = "base24";
name = "Scheme Name";
@ -73,7 +73,7 @@ pkgs.lib.runTests {
};
testTintedThemingLegacyBase16SchemeImport = {
expr = lib.tinted-theming.importScheme ./sample-legacy-base16.yml;
expr = self.tinted-theming.importScheme ./sample-legacy-base16.yml;
expected = {
system = "base16";
name = "Scheme Name";
@ -101,32 +101,32 @@ pkgs.lib.runTests {
};
testIsBase16 = {
expr = lib.tinted-theming.isBase16 sampleBase16Scheme.palette;
expr = self.tinted-theming.isBase16 sampleBase16Scheme.palette;
expected = true;
};
testIsNotBase16 = {
expr = lib.tinted-theming.isBase16 sampleBase16Scheme'.palette;
expr = self.tinted-theming.isBase16 sampleBase16Scheme'.palette;
expected = false;
};
testIsBase24 = {
expr = lib.tinted-theming.isBase24 sampleBase24Scheme.palette;
expr = self.tinted-theming.isBase24 sampleBase24Scheme.palette;
expected = true;
};
testIsNotBase24 = {
expr = lib.tinted-theming.isBase24 sampleBase24Scheme'.palette;
expr = self.tinted-theming.isBase24 sampleBase24Scheme'.palette;
expected = false;
};
testIsALegacyBase16Scheme = {
expr = lib.tinted-theming.isLegacyScheme (lib.importYAML ./sample-legacy-base16.yml);
expr = self.tinted-theming.isLegacyScheme (self.importYAML ./sample-legacy-base16.yml);
expected = true;
};
testIsALegacyBase24Scheme = {
expr = lib.tinted-theming.isLegacyScheme (lib.importYAML ./sample-legacy-base24.yml);
expr = self.tinted-theming.isLegacyScheme (self.importYAML ./sample-legacy-base24.yml);
expected = true;
};
}

View File

@ -1,4 +1,4 @@
{ pkgs, lib }:
{ pkgs, lib, self }:
let
customOctalGlyphs = {
@ -66,21 +66,21 @@ let
"23" = "N";
};
base24Set = lib.trivial.generateBaseDigitType base24GlyphsList;
base24Set = self.trivial.generateBaseDigitType base24GlyphsList;
in
pkgs.lib.runTests {
lib.runTests {
testGenerateCustomGlyphSet = {
expr = lib.trivial.generateGlyphSet [ "A" "B" "C" "D" "E" "F" "G" "H" ];
expr = self.trivial.generateGlyphSet [ "A" "B" "C" "D" "E" "F" "G" "H" ];
expected = customOctalGlyphs;
};
testGenerateBase24GlyphSet = {
expr = lib.trivial.generateGlyphSet base24GlyphsList;
expr = self.trivial.generateGlyphSet base24GlyphsList;
expected = customBase24Glyphs;
};
testGenerateConversionTable = {
expr = lib.trivial.generateConversionTable [ "A" "B" "C" "D" "E" "F" "G" "H" ];
expr = self.trivial.generateConversionTable [ "A" "B" "C" "D" "E" "F" "G" "H" ];
expected = {
"A" = 0;
"B" = 1;
@ -94,7 +94,7 @@ pkgs.lib.runTests {
};
testGenerateConversionTable2 = {
expr = lib.trivial.generateConversionTable
expr = self.trivial.generateConversionTable
[
"0"
"1"
@ -145,29 +145,29 @@ pkgs.lib.runTests {
};
testBaseDigitWithCustomOctalGlyph = {
expr = lib.trivial.toBaseDigitsWithGlyphs 8 9 customOctalGlyphs;
expr = self.trivial.toBaseDigitsWithGlyphs 8 9 customOctalGlyphs;
expected = "BB";
};
testBaseDigitWithCustomOctalGlyph2 = {
expr = lib.trivial.toBaseDigitsWithGlyphs 8 641 customOctalGlyphs;
expr = self.trivial.toBaseDigitsWithGlyphs 8 641 customOctalGlyphs;
expected = "BCAB";
};
testBaseDigitWithProperBase24Glyph = {
expr = lib.trivial.toBaseDigitsWithGlyphs 24 641 customBase24Glyphs;
expr = self.trivial.toBaseDigitsWithGlyphs 24 641 customBase24Glyphs;
expected = "12H";
};
testBaseDigitWithProperBase24Glyph2 = {
expr = lib.trivial.toBaseDigitsWithGlyphs 24 2583 customBase24Glyphs;
expr = self.trivial.toBaseDigitsWithGlyphs 24 2583 customBase24Glyphs;
expected = "4BF";
};
# We're mainly testing if the underlying YAML library is mostly compliant
# We're mainly testing if the underlying YAML selfrary is mostly compliant
# with whatever it claims.
testImportBasicYAML = {
expr = lib.trivial.importYAML ./simple.yml;
expr = self.trivial.importYAML ./simple.yml;
expected = {
hello = "there";
how-are-you-doing = "I'm fine. Thank you for asking.\n";
@ -177,7 +177,7 @@ pkgs.lib.runTests {
};
testImportTintedThemingBase16YAML = {
expr = lib.trivial.importYAML ../tinted-theming/sample-base16-scheme.yml;
expr = self.trivial.importYAML ../tinted-theming/sample-base16-scheme.yml;
expected = {
system = "base16";
name = "Bark on a tree";
@ -207,22 +207,22 @@ pkgs.lib.runTests {
# YAML is a superset of JSON (or was it the other way around?) after v1.2.
testToYAML = {
expr = lib.trivial.toYAML { } { hello = "there"; };
expr = self.trivial.toYAML { } { hello = "there"; };
expected = "{\"hello\":\"there\"}";
};
testNumberClamp = {
expr = lib.trivial.clamp 1 10 4;
expr = self.trivial.clamp 1 10 4;
expected = 4;
};
testNumberClampMin = {
expr = lib.trivial.clamp 1 10 (-5);
expr = self.trivial.clamp 1 10 (-5);
expected = 1;
};
testNumberClampMax = {
expr = lib.trivial.clamp 1 10 453;
expr = self.trivial.clamp 1 10 453;
expected = 10;
};
}