diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-13 10:15:20 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-13 10:15:20 +0300 |
| commit | d4971998edae804c39797486be7a342d43e61e5c (patch) | |
| tree | 830c008847704b40b7f45d4c7c620beed03958d6 /src/lib/random.source.sh | |
| parent | e9437367b8fc7a4cb0ffed6c0a8ba89356a3b9ad (diff) | |
Refactor helper modules for im0
Diffstat (limited to 'src/lib/random.source.sh')
| -rw-r--r-- | src/lib/random.source.sh | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/lib/random.source.sh b/src/lib/random.source.sh new file mode 100644 index 0000000..5a1ef59 --- /dev/null +++ b/src/lib/random.source.sh @@ -0,0 +1,74 @@ +random_seed_is_set() { + [ -n "$RANDOM_SEED" ] +} + +deterministic_index() { + local -r namespace="$1"; shift + local -r count="$1"; shift + local checksum + + checksum=$(printf '%s' "${RANDOM_SEED}:$namespace" | cksum) + checksum=${checksum%% *} + + printf '%s\n' $(( checksum % count )) +} + +random_index() { + local -r namespace="$1"; shift + local -r count="$1"; shift + + if random_seed_is_set; then + deterministic_index "$namespace" "$count" + else + printf '%s\n' $(( RANDOM % count )) + fi +} + +random_animation_css_class() { + local -r speed="$1"; shift + local -r context="${1:-$speed}" + local -i index + local -a classes=( + "animate-opacity-$speed" + "animate-top-$speed" + "animate-left-$speed" + "animate-right-$speed" + "animate-bottom-$speed" + "animate-zoom-$speed" + "animate-snap-rotate-$speed" + "animate-hard-zoom-$speed" + "animate-slam-left-$speed" + "animate-slam-right-$speed" + "animate-flash-in-$speed" + "animate-invert-pop-$speed" + "animate-posterize-pop-$speed" + "animate-skew-snap-$speed" + "animate-glitch-step-$speed" + ) + + index=$(random_index "animation:$speed:$context" "${#classes[@]}") + printf '%s\n' "${classes[index]}" +} + +deterministic_shuffle() { + local checksum + local line + + while IFS= read -r line; do + checksum=$(printf '%s' "${RANDOM_SEED}:shuffle:$line" | cksum) + checksum=${checksum%% *} + printf '%010u\t%s\n' "$checksum" "$line" + done | sort -n -k1,1 -k2,2 | cut -f2- +} + +maybe_shuffle() { + if [ "$SHUFFLE" = yes ]; then + if random_seed_is_set; then + deterministic_shuffle + else + sort -R + fi + else + sort + fi +} |
