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