aboutsummaryrefslogtreecommitdiff
path: root/vis-open
diff options
context:
space:
mode:
authorpmnw <pmnw@mailbox.org>2023-01-26 13:44:47 +0100
committerFelix Van der Jeugt <felix.vanderjeugt@posteo.net>2023-02-13 20:27:23 +0100
commit6a1e93fba75c9759bed0d726798ba54806f7800e (patch)
tree00c3b5c20b44b37d2ef1ad94c0accfb1376141ff /vis-open
parente45f573ddd592f38459591db1924c27272b2b763 (diff)
downloadvis-6a1e93fba75c9759bed0d726798ba54806f7800e.tar.gz
vis-6a1e93fba75c9759bed0d726798ba54806f7800e.tar.xz
vis-open: further improvement & clean-up
Diffstat (limited to 'vis-open')
-rwxr-xr-xvis-open56
1 files changed, 26 insertions, 30 deletions
diff --git a/vis-open b/vis-open
index b8928f9..a79792f 100755
--- a/vis-open
+++ b/vis-open
@@ -5,45 +5,40 @@ set -e
NL='
'
-VIS_MENU_PROMPT=""
-ALLOW_AUTO_SELECT=1
+VIS_MENU_PROMPT=''
+ALLOW_AUTO_SELECT='1'
wrap_dirs() {
- while read -r o
+ while read -r filename
do
- [ -d "$o" ] && printf "%s/\n" "$o" || printf "%s\n" "$o"
+ if [ -d "$filename" ]; then
+ printf '%s/\n' "$filename"
+ else
+ printf '%s\n' "$filename"
+ fi
done
}
-while [ $# -gt 0 ]; do
- case "$1" in
- -h|--help)
- echo "usage: $(basename $0) [-h] [-p prompt] [-f] [--] [file-pattern]"
- exit 0;
- ;;
- -p)
- VIS_MENU_PROMPT=$2
- shift
- shift
- ;;
- -f)
- ALLOW_AUTO_SELECT=""
- shift
+while getopts fhp: opt; do
+ case "$opt" in
+ f)
+ ALLOW_AUTO_SELECT=''
;;
- --)
- shift
- break
+ p)
+ VIS_MENU_PROMPT="$OPTARG"
;;
- *)
- break
+ h|?)
+ printf 'usage: %s [-f] [-h] [-p prompt] [--] [file-pattern]\n' "${0##*/}"
+ exit 0
;;
esac
done
+shift "$((OPTIND - 1))"
# At this point, all the remaining arguments should be the expansion of
# any globs that were passed on the command line.
-if [ $# -eq 1 ] && [ "$ALLOW_AUTO_SELECT" = 1 ]; then
+if [ "$#" -eq 1 ] && [ "$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.
@@ -52,15 +47,16 @@ if [ $# -eq 1 ] && [ "$ALLOW_AUTO_SELECT" = 1 ]; then
# 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)"
+ IFS="$NL" # Don't split ls output on tabs or spaces.
+ exec "$0" -p "$VIS_MENU_PROMPT" -f "$(ls -1)"
else
# We've found a single item, and it's not a directory,
# so it must be a filename (or file-like thing) to open,
# unless the parent directory does not exist.
- if [ -d "$(dirname "$1")" ]; then
- cd "$(dirname "$1")"
- echo "$(pwd -P)"/"$(basename "$1" | sed 's/\*$//')"
+ parentdir="$(dirname -- "$1")"
+ if [ -d "$parentdir" ]; then
+ cd "$parentdir"
+ printf '%s/%s\n' "$(pwd -P)" "$(basename -- "${1%\*}")"
exit 0
else
exit 1
@@ -70,7 +66,7 @@ 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" "$@" | wrap_dirs | vis-menu -b -p "$VIS_MENU_PROMPT")
+CHOICE="$(printf '%s\n' '..' "$@" | wrap_dirs | 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"