logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

vis-open (1896B)


  1. #!/bin/sh
  2. set -e
  3. # Later, we're going to want to set $IFS to a single newline, so let's prepare one.
  4. NL='
  5. '
  6. if [ -z "$VIS_OPEN_LINES" ]; then
  7. VIS_OPEN_LINES='0'
  8. fi
  9. VIS_MENU_PROMPT=''
  10. ALLOW_AUTO_SELECT='1'
  11. wrap_dirs() {
  12. while read -r filename
  13. do
  14. if [ -d "$filename" ]; then
  15. printf '%s/\n' "$filename"
  16. else
  17. printf '%s\n' "$filename"
  18. fi
  19. done
  20. }
  21. while getopts fhp: opt; do
  22. case "$opt" in
  23. f)
  24. ALLOW_AUTO_SELECT=''
  25. ;;
  26. p)
  27. VIS_MENU_PROMPT="$OPTARG"
  28. ;;
  29. h|?)
  30. printf 'usage: %s [-f] [-h] [-p prompt] [--] [file-pattern]\n' "${0##*/}"
  31. exit 0
  32. ;;
  33. esac
  34. done
  35. shift "$((OPTIND - 1))"
  36. # At this point, all the remaining arguments should be the expansion of
  37. # any globs that were passed on the command line.
  38. if [ "$#" -eq 1 ] && [ "$ALLOW_AUTO_SELECT" = '1' ]; then
  39. # If there were globs on the command-line, they've expanded to
  40. # a single item, so we can just process it.
  41. if [ -d "$1" ]; then
  42. # Recurse and show the contents of the named directory,
  43. # We pass -f to force the next iteration to present the
  44. # full list, even if it's just an empty directory.
  45. cd "$1"
  46. IFS="$NL" # Don't split ls output on tabs or spaces.
  47. exec "$0" -p "$VIS_MENU_PROMPT" -f "$(ls -1)"
  48. else
  49. # We've found a single item, and it's not a directory,
  50. # so it must be a filename (or file-like thing) to open,
  51. # unless the parent directory does not exist.
  52. parentdir="$(dirname -- "$1")"
  53. if [ -d "$parentdir" ]; then
  54. cd "$parentdir"
  55. printf '%s/%s\n' "$(pwd -P)" "$(basename -- "${1%\*}")"
  56. exit 0
  57. else
  58. exit 1
  59. fi
  60. fi
  61. fi
  62. # At this point, we have a bunch of options we need to present to the
  63. # user so they can pick one.
  64. CHOICE="$(printf '%s\n' '..' "$@" | wrap_dirs | vis-menu -b -l "$VIS_OPEN_LINES" -p "$VIS_MENU_PROMPT")"
  65. # Did they pick a file or directory? Who knows, let's let the next iteration figure it out.
  66. exec "$0" -p "$VIS_MENU_PROMPT" -- "$CHOICE"