Compare commits

...

3 Commits

Author SHA1 Message Date
Nick Chambers 4b2d6fcabb Add a shuffle utility 2021-11-15 15:52:23 -06:00
Nick Chambers a39c3ce06a Make some minor cosmetic improvements 2021-11-15 13:29:04 -06:00
Nick Chambers 474ba22817 Fix the section header 2021-11-15 02:57:08 -06:00
1 changed files with 30 additions and 15 deletions

45
rowbot
View File

@ -80,7 +80,7 @@ is_chan() {
[[ ${msg_args[0]:0:1} = \# ]]
}
# cryptography secure (almost probably) random number generators
# cryptographically secure (almost maybe) pseudo random number utilities
random() {
local min=$1 max=$2
@ -97,6 +97,18 @@ any_file() {
fi
}
shuffle() {
local idx=0 spot tmp
declare -n rowbot_array=$1
for (( ; idx < ${#rowbot_array[@]}; idx += 1 )); do
spot=$(random 0 "${#rowbot_array[@]}")
tmp=${rowbot_array[$idx]}
rowbot_array[$idx]=${rowbot_array[$spot]}
rowbot_array[$spot]=$tmp
done
}
# misc
is_running () {
@ -137,6 +149,17 @@ run_callbacks() {
return "$status"
}
prints() {
# This is a wrapper around printf so the format string isn't known ahead of
# time.
# shellcheck disable=SC2059
printf "$@"
if [[ -t 1 ]]; then
printf \\n
fi
}
# The variables are checked dynamically
# shellcheck disable=SC2034
seconds() {
@ -160,11 +183,7 @@ seconds() {
fi
done
printf -- %s "$time"
if [[ -t 1 ]]; then
printf \\n
fi
prints %s "$time"
}
url() {
@ -174,11 +193,7 @@ url() {
printf ircs://
fi
printf %s:%s "${config[server]}" "${config[port]}"
if [[ -t 1 ]]; then
printf \\n
fi
prints %s:%s "${config[server]}" "${config[port]}"
}
b64_encode() {
@ -194,19 +209,19 @@ b64_encode() {
(( table_idxs[1] = (((numerics[0] & 0x03) << 6) | (numerics[1] & 0xF0) >> 2) >> 2 ))
(( table_idxs[2] = (((numerics[1] & 0x0F) << 4) | (numerics[2] & 0xC0) >> 4) >> 2 ))
(( table_idxs[3] = numerics[2] & 0x3F ))
for table_idx in "${table_idxs[@]}"; do
encoded+=${table[$table_idx]}
done
done
if (( ${#1} % 3 == 1 )); then
encoded=${encoded::-2}==
elif (( ${#1} % 3 == 2 )); then
encoded=${encoded::-1}=
fi
printf %s "$encoded"
if [[ -t 1 ]]; then
printf \\n
fi
prints %s "$encoded"
}
###