~yerinalexey/dotfiles

e7534a85dee054a65cce91878da45da9f3df624e — Alexey Yerin 4 months ago 92bd5eb
Improve menu setup for vis-complte and vis-open
2 files changed, 143 insertions(+), 0 deletions(-)

A scripts/vis-complete
A scripts/vis-open
A scripts/vis-complete => scripts/vis-complete +65 -0
@@ 0,0 1,65 @@
#!/bin/sh
set -e

basic_regex_quote() { printf "%s" "$1" | sed 's|[\\.*^$[]|\\&|g'; }
glob_quote () { printf "%s" "$1" | sed 's|[\\?*[]]|\\&|g'; }

PATTERN=""
COMPLETE_WORD=0
FIND_FILE_LIMIT=1000

while [ $# -gt 0 ]; do
	case "$1" in
	-h|--help)
		echo "usage: $(basename "$0") [-h] [--file|--word] [pattern]"
		exit 0;
		;;
	--file)
		shift
		;;
	--word)
		COMPLETE_WORD=1
		shift
		;;
	*)
		PATTERN="$1"
		break
		;;
	esac
done

if [ $COMPLETE_WORD = 1 ]; then
	tr -cs '[:alnum:]_' '\n' |
		grep "^$(basic_regex_quote "$PATTERN")." |
		sort -u
else
	# Expand to absolute path because of the -path option below.
	case $PATTERN in
		/*)
			XPATTERN=$PATTERN
			;;
		'~'|'~/'*)
			XPATTERN=$HOME$(echo $PATTERN | tail -c +2)
			;;
		*)
			XPATTERN=$PWD/$PATTERN
			;;
	esac

	# The first path condition rules out paths that start with "." unless
	# they start with "..". That way, hidden paths should stay hidden, but
	# non-normalised paths should still show up.
	find $(dirname "$XPATTERN") \
		-name '.*' -prune \
		-o \( \
			! -name '.*' \
			-a -path "$(glob_quote "$XPATTERN")*" \
			-print \
		\) 2>/dev/null |
		head -n $FIND_FILE_LIMIT |
		sort |
		sed "s|^$(dirname $XPATTERN)/||"
fi |
	vis-menu -b -l 5 | # PATCH: -b -l 5
	sed "s|^$(basename $PATTERN)$(echo $PATTERN | tail -c 2 | fgrep /)||" |
	tr -d '\n'

A scripts/vis-open => scripts/vis-open +78 -0
@@ 0,0 1,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 -l 20 -p "$VIS_MENU_PROMPT") # PATCH: -b -l 20

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