dir2org.zsh 1013 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "${@}"