dir2org.zsh 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.bash [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. (( depth += 1 ))
  27. for f in $(ls -d "${dir}"/* 2>/dev/null); do
  28. if [ -d "${f}" ]; then
  29. scan_and_populate ${depth} "${f}"
  30. else
  31. headline ${depth} "[[file://${f}][${${f##*/}%.*}]]"
  32. fi
  33. done
  34. let "depth -= 1"
  35. }
  36. function main {
  37. local scan_dir="${1:-$(pwd)}"
  38. local depth=0
  39. scan_and_populate ${depth} "${scan_dir}"
  40. }
  41. main "${@}"