Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trailing slash when looking for directories #24

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ _bcpp_compgen() {
# argument to specify desired completion behavior
#
_bcpp_complete() {
local WILDCARDS ACTION LINE OPTION INPUT QUOTE
local WILDCARDS ACTION LINE OPTION INPUT QUOTE SUBDIRS

ACTION="$1"
if [[ "_$1" == "_-d" ]]
Expand Down Expand Up @@ -136,17 +136,16 @@ _bcpp_complete() {
# If input is already a valid path, do not try to be clever
if [[ -e "$INPUT" ]]
then
if [[ "_$ACTION" == "_directory" ]]
COMPREPLY=($(compgen -S / -d "$INPUT"))
if [[ "_$ACTION" != "_directory" ]]
then
OPTION="dirnames"
else
OPTION="filenames"
fi
if [[ -d "$INPUT" && "${INPUT: -1}" != '/' ]]
then
COMPREPLY=("$INPUT/")
else
COMPREPLY=($(compgen -o "$OPTION" "$INPUT"))
SUBDIRS=($(compgen -d "$INPUT"))
while read -r file
do
# FIXME: https://stackoverflow.com/a/14367368/11248508
[[ " $SUBDIRS " == *" $file "* ]] && continue
COMPREPLY+=("$file")
done < <(compgen -f "$INPUT")
fi
return
fi
Expand All @@ -167,14 +166,20 @@ _bcpp_complete() {
_bcpp_silent_bg _bcpp_compgen "$WILDCARDS" "$pipe"
while read -r -d $'\n' LINE
do
if [[ "_$ACTION" == "_directory" && ! -d "$LINE" ]]
then # skip non-directory paths when looking for directory
continue
fi
if [[ -z "$LINE" ]]
then # skip empty suggestions
continue
fi
if [[ ! -d "$LINE" ]]
then
if [[ "_$ACTION" == "_directory" ]]
then # skip non-directory paths when looking for directory
continue
fi
elif [[ "${LINE: -1}" != "/" ]]
then # always add trailing slash to directories
LINE="$LINE/"
fi
if [[ -z "$QUOTE" ]]
then # escape special characters unless user has opened a quote
LINE=$(printf "%q" "$LINE")
Expand Down