nix/home/program/bash/module/Random.nix

26 lines
501 B
Nix
Raw Normal View History

2024-11-04 04:37:29 +03:00
{ ... }: {
text = ''
2024-11-13 11:06:28 +03:00
# Generate random string.
# Usage: random <LENGTH>
function random() {
local length="''${1}"
if [[ "''${length}" = "" ]]; then
help random
return 2
fi
head /dev/urandom | tr -dc A-Za-z0-9 | head -c''${length}
}
2024-11-04 04:37:29 +03:00
# Picks a random file or directory.
function random_file() {
local IFS=$'\n'
local dirs=($(ls))
local total=''${#dirs[@]}
((total--))
local index=$(shuf -i 0-''${total} -n 1)
2024-04-06 03:03:58 +03:00
2024-11-13 11:06:28 +03:00
printf "%s" ''${dirs[$index]}
2024-11-04 04:37:29 +03:00
}
'';
2024-04-06 03:03:58 +03:00
}