bahaghari/lib/trivial: add toYAML and toBaseDigitsWithGlyphs

This commit is contained in:
Gabriel Arazas 2024-02-24 10:29:59 +08:00
parent 6c5e359fc3
commit 0af7a31d7b
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
2 changed files with 25 additions and 2 deletions

View File

@ -16,6 +16,6 @@ pkgs.lib.makeExtensible
hex = callLibs ./hex.nix; hex = callLibs ./hex.nix;
tinted-theming = callLibs ./tinted-theming.nix; tinted-theming = callLibs ./tinted-theming.nix;
inherit (self.trivial) importYAML; inherit (self.trivial) importYAML toYAML toBaseDigitsWithGlyphs;
inherit (self.hex) toHexString; inherit (self.hex) toHexString;
}) })

View File

@ -1,6 +1,8 @@
{ pkgs, lib }: { pkgs, lib }:
{ {
inherit (pkgs.lib.generators) toYAML;
/* Read YAML files into a Nix expression similar to lib.importJSON and /* Read YAML files into a Nix expression similar to lib.importJSON and
lib.importTOML from nixpkgs standard library. Unlike both of them, this lib.importTOML from nixpkgs standard library. Unlike both of them, this
unfortunately relies on an import-from-derivation (IFD) so it isn't exactly unfortunately relies on an import-from-derivation (IFD) so it isn't exactly
@ -11,7 +13,7 @@
https://pkg.go.dev/gopkg.in/yaml.v3#readme-compatibility https://pkg.go.dev/gopkg.in/yaml.v3#readme-compatibility
Type: importYAML :: path -> any Type: importYAML :: Path -> any
*/ */
importYAML = path: importYAML = path:
let let
@ -20,4 +22,25 @@
''; '';
in in
pkgs.lib.importJSON data; pkgs.lib.importJSON data;
/* Convert a given decimal number to a specified base digit with the set of
glyphs for each digit as returned from lib.toBaseDigits.
Type: toBaseDigitWithGlyphs :: Int -> Int -> Attrs -> String
Example:
toBaseDigitWithGlyphs 24 267 {
"0" = "0";
# ...
"22" = "L";
"23" = "M";
"24" = "N";
}
*/
toBaseDigitsWithGlyphs = base: i: glyphs:
let
baseDigits = pkgs.lib.toBaseDigits i;
toBaseDigits = d: glyphs.${builtins.toString i};
in
pkgs.lib.concatMapStrings toBaseDigits baseDigits;
} }