forked from andreas-hartmann/kaomoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
67 lines (60 loc) · 2.27 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{
description = "Generate a random kaomoji";
inputs = {
nixpkgs.url = "nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
# Create system-specific outputs for the standard Nix systems
# https://github.com/numtide/flake-utils/blob/main/default.nix#L3-L9
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
kaomojiJson = pkgs.copyPathToStore (self + "/kaomoji.json");
myprogram = pkgs.writeShellApplication {
name = "generate-kaomoji";
runtimeInputs = [ pkgs.jq ];
text = ''
if [[ " $* " == *" --help "* ]]; then
echo "Usage: $(basename "$0") [options] [JQ filter]"
echo ""
echo "Options:"
echo " --help Show this help message and exit"
echo " -r, --raw-output Output raw strings, not JSON texts"
echo " [JQ filter] Apply a custom jq filter to the selected kaomoji"
echo ""
echo "Examples:"
echo ""
echo "# generate a kaomoji object"
example1=$("$0")
echo "> $(basename "$0")"
echo "$example1"
echo ""
echo "# select the raw kaomoji string"
example2=$(echo "$example1" | jq -r ".value")
echo "> $(basename "$0") -r .value"
echo "$example2"
exit 0
fi
ARRAY=$(cat ${kaomojiJson})
LENGTH=$(echo "$ARRAY" | jq '.kaomoji | length')
RAND_INDEX=$((RANDOM % LENGTH))
RAW_OUTPUT=""
if [[ " $* " == *" -r "* ]] || [[ " $* " == *" --raw-output "* ]]; then
RAW_OUTPUT="-r"
fi
EXTRA_ARGS=$(echo "$*" | sed -e 's/--raw-output//g' -e 's/-r//g')
if [[ -z "$EXTRA_ARGS" ]]; then
EXTRA_ARGS="."
fi
echo "$ARRAY" | jq --argjson index $RAND_INDEX '.kaomoji[$index]' | jq $RAW_OUTPUT "$EXTRA_ARGS"
'';
};
in {
packages.default = myprogram;
apps.default = {
type = "app";
program = "${myprogram}/bin/generate-kaomoji";
};
});
}