diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2016-05-29 11:33:28 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2016-05-29 11:33:28 +0200 |
| commit | c22fa0d326369dfa7dd375dc2c399df3d52330c9 (patch) | |
| tree | 85500b5bc52ee316fdf08fcf518ea7cd243cf7a3 | |
| parent | 4ec646e5cfe7af548f26908fe16e19a2d7cc3797 (diff) | |
| parent | c9e0d7735fbf0ca71c003ec6480960a664f1db06 (diff) | |
| download | vis-c22fa0d326369dfa7dd375dc2c399df3d52330c9.tar.gz vis-c22fa0d326369dfa7dd375dc2c399df3d52330c9.tar.xz | |
Merge branch 'vis-open-cleanup' of https://github.com/Screwtapello/vis
| -rwxr-xr-x | vis-open | 58 |
1 files changed, 42 insertions, 16 deletions
@@ -1,38 +1,64 @@ #!/bin/sh +set -e -PATTERN="." -VIS_MENU_ARGS="-b" +# Later, we're going to want to set $IFS to a single newline, so let's prepare one. +NL=' +' + +VIS_MENU_PROMPT="" +ALLOW_AUTO_SELECT=1 while [ $# -gt 0 ]; do case "$1" in -h|--help) - echo "usage: $(basename $0) [-h] [-p prompt] [file-pattern]" + echo "usage: $(basename $0) [-h] [-p prompt] [-f] [--] [file-pattern]" exit 0; ;; -p) - VIS_MENU_ARGS="$VIS_MENU_ARGS -p $2" + VIS_MENU_PROMPT=$2 + shift shift + ;; + -f) + ALLOW_AUTO_SELECT="" + shift + ;; + --) shift + break ;; *) - PATTERN=$* break ;; esac done -DIR="" -[ ! -z "$PATTERN" -a "$PATTERN" != "." -a -d "$PATTERN" ] && DIR="$PATTERN" +# At this point, all the remaining arguments should be the expansion of +# any globs that were passed on the command line. + +if [ $# -eq 1 -a "$ALLOW_AUTO_SELECT" = 1 ]; then + # If there were globs on the command-line, they've expanded to + # a single item, so we can just process it. -while true; do - SEL=$({ echo ..; ls -1 $PATTERN 2>/dev/null || echo $PATTERN; } | vis-menu $VIS_MENU_ARGS) - [ -z "$SEL" ] && exit 1 - [ ! -z "$DIR" ] && SEL="$DIR/$SEL" - if [ -d "$SEL" ]; then - DIR="$SEL" - PATTERN="$DIR" + if [ -d "$1" ]; then + # Recurse and show the contents of the named directory, + # We pass -f to force the next iteration to present the + # full list, even if it's just an empty directory. + cd "$1" + IFS=$NL # Don't split ls output on tabs or spaces. + exec "$0" -p "$VIS_MENU_PROMPT" -f .. $(ls -1) else - echo "$SEL" + # We've found a single item, and it's not a directory, + # so it must be a filename (or file-like thing) to open. + cd "$(dirname "$1")" + echo "$(pwd -P)"/"$(basename "$1")" exit 0 fi -done +fi + +# At this point, we have a bunch of options we need to present to the +# user so they can pick one. +CHOICE=$(printf "%s\n" "$@" | vis-menu -b -p "$VIS_MENU_PROMPT") + +# Did they pick a file or directory? Who knows, let's let the next iteration figure it out. +exec "$0" -p "$VIS_MENU_PROMPT" -- "$CHOICE" |
