Dotfiles for my tiling window manager + terminal workflow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.2 KiB

  1. #!/bin/bash
  2. # vars {{{ #
  3. shuffle_cache="shuffle.txt"
  4. regen_flag=0
  5. dir=""
  6. # }}} vars #
  7. # usage_msg {{{ #
  8. function usage_msg() {
  9. # echo "usage: shuffler [-r] [dir_name]"
  10. cat <<-EOF
  11. Usage: shuffler [-r] [dir_name]
  12. -r: regenerate the shuffle cache
  13. EOF
  14. }
  15. # }}} usage_msg #
  16. # parse for regen_flag{{{
  17. while getopts ":r" opt; do
  18. case $opt in
  19. r)
  20. echo "-r was triggered!" >&2
  21. regen_flag=1
  22. ;;
  23. \?)
  24. echo "Invalid flag: -$OPTARG" >&2
  25. usage_msg
  26. exit 1
  27. ;;
  28. esac
  29. done
  30. #}}}
  31. # set dir {{{ #
  32. shift $((OPTIND-1))
  33. # if no dir, use pwd
  34. if [[ "$#" -eq 0 ]]; then
  35. dir=$(pwd)
  36. elif [[ "$#" -gt 1 ]] || [[ ! -d "$1" ]]; then
  37. usage_msg
  38. exit 1
  39. else
  40. dir="$1"
  41. fi
  42. # }}} set dir #
  43. # rebuild shuffle cache if regen_flag, doesn't exist or file empty
  44. if [[ "$regen_flag" -eq 1 ]] || [[ ! -f "$dir"/"$shuffle_cache" ]] || \
  45. [[ $(wc -l "$dir"/"$shuffle_cache" | awk '{print $1}') -eq 0 ]]; then
  46. # if [[ "$regen_flag" -eq 1 ]]; then
  47. find "$dir"/* ! -name "$shuffle_cache" -type f | shuf > "$dir"/"$shuffle_cache"
  48. fi
  49. head -n 1 "$dir"/"$shuffle_cache"
  50. printf '%s\n\n' "$(sed '1d' "$dir"/"$shuffle_cache")" > "$dir"/"$shuffle_cache"
  51. # vim: set tabstop=2 shiftwidth=2 expandtab: