dir2org.zsh 1013 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env zsh
  2. # desc:
  3. #
  4. # Output an org compatible structure representing the filesystem from
  5. # the point passed on the command line (or . by default).
  6. #
  7. # options:
  8. # none
  9. #
  10. # usage:
  11. # dir2org.zsh [DIR]...
  12. #
  13. # author:
  14. # Phil Jackson (phil@shellarchive.co.uk)
  15. set -e
  16. function headline {
  17. local depth="${1}"
  18. local text="${2}"
  19. printf "%${depth}s %s" "" | tr ' ' '*'
  20. echo " ${text}"
  21. }
  22. function scan_and_populate {
  23. local depth="${1}"
  24. local dir="${2}"
  25. headline ${depth} "${dir}"
  26. # if there is no files in dir then just move on
  27. [[ $(ls "${dir}" | wc -l) -eq 0 ]] && return
  28. (( depth += 1 ))
  29. for f in $(ls -d "${dir}"/*); do
  30. if [ -d "${f}" ]; then
  31. scan_and_populate ${depth} "${f}"
  32. else
  33. headline ${depth} "[[file://${f}][${${f##*/}%.*}]]"
  34. fi
  35. done
  36. (( depth -= 1 ))
  37. }
  38. function main {
  39. local scan_dir="${1:-$(pwd)}"
  40. local depth=0
  41. scan_and_populate ${depth} "${scan_dir}"
  42. }
  43. main "${@}"