File: vis-open

package info (click to toggle)
vis 0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,360 kB
  • sloc: ansic: 22,281; sh: 950; makefile: 356; python: 47
file content (78 lines) | stat: -rwxr-xr-x 1,766 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh
set -e

# 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

wrap_dirs() {
	local o

	while read o
	do
		[ -d "$o" ] && printf "%s/\n" "$o" || printf "%s\n" "$o"
	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
		;;
	--)
		shift
		break
		;;
	*)
		break
		;;
	esac
done

# 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.

	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
		# 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/\*$//')"
			exit 0
		else
			exit 1
		fi
	fi
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")

# 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"