- use dict of lists for dirs
- key = dir, list values = files
- have central file containing cache for all dirs
- history
Last active
April 15, 2019 12:37
-
-
Save Kevin-Mok/da9c08236905fb2c87af6e15c25534ac to your computer and use it in GitHub Desktop.
Create a shuffle "playlist" for the files in a directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Shuffle files for current/given directory. Creates a shuffle cache file | |
# in directory, which rotates through the entire directory contents before | |
# regenerating. | |
# vars {{{ # | |
shuffle_cache="shuffle.txt" | |
regen_flag=0 | |
dir="" | |
old_ifs=$IFS | |
# }}} vars # | |
# usage_msg {{{ # | |
function usage_msg() { | |
# echo "usage: shuffler [-r] [dir_name]" | |
cat <<-EOF | |
Usage: shuffler [-r] [dir_name] | |
-r: regenerate the shuffle cache | |
EOF | |
} | |
# }}} usage_msg # | |
# parse for regen_flag{{{ | |
while getopts ":r" opt; do | |
case $opt in | |
r) | |
echo "-r was triggered!" >&2 | |
regen_flag=1 | |
;; | |
\?) | |
echo "Invalid flag: -$OPTARG" >&2 | |
usage_msg | |
exit 1 | |
;; | |
esac | |
done | |
#}}} | |
# set dir {{{ # | |
shift $((OPTIND-1)) | |
# if no dir, use pwd | |
if [[ "$#" -eq 0 ]]; then | |
dir=$(pwd) | |
elif [[ "$#" -gt 1 ]] || [[ ! -d "$1" ]]; then | |
usage_msg | |
exit 1 | |
else | |
dir="$1" | |
fi | |
# }}} set dir # | |
# rebuild shuffle cache if regen_flag, doesn't exist or file empty | |
if [[ "$regen_flag" -eq 1 ]] || [[ ! -f "$dir"/"$shuffle_cache" ]] || \ | |
[[ "$(head -n 1 "$dir"/"$shuffle_cache")" = "" ]]; then | |
find "$dir"/* ! -name "$shuffle_cache" -type f | shuf > "$dir"/"$shuffle_cache" | |
# create list of all files in dir | |
IFS=$'\n' | |
files=("$(find "$dir"/* ! -name "$shuffle_cache" -type f -printf "%f\n")") | |
echo "${files[@]}" | |
dirs=(["$dir"]=("${files[@]}")) | |
# declare -A dirs | |
# echo "$(find "$dir"/* ! -name "$shuffle_cache" -type f -printf "%f\n")" | |
# dirs | |
fi | |
# head -n 1 "$dir"/"$shuffle_cache" | |
# printf '%s\n\n' "$(sed '1d' "$dir"/"$shuffle_cache")" > "$dir"/"$shuffle_cache" | |
# vim: set tabstop=2 shiftwidth=2 expandtab foldmethod=marker: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment