Skip to content

Commit

Permalink
bash completion: expand both *.gbs and *.gbs.gz
Browse files Browse the repository at this point in the history
Also don't list directories ending with .gbs or .gbs.gz as both
file and directory names.
Once as directory (with a trailing slash) is enough.
  • Loading branch information
mmitch committed Feb 5, 2025
1 parent 808b536 commit 94d7988
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions contrib/gbsplay.bashcompletion
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@ __gbsplay_add_spaces_to_compreply()
}

__gbsplay_expand_filename()
# TODO/CHECK: do all file extensions work (eg .gbs.gz)?
{
COMPREPLY=()
local i
local i ext

# add trailing space to filenames
local -a files
mapfile -t files < <( compgen -f -X '!*.gbs' -- "$cur" ) # does not work: | sed -e 's/\([[:space:]]\)/\\\1/g'
for (( i=0; i < ${#files[@]}; i++ )); do
COMPREPLY[${#COMPREPLY[@]}]="${files[$i]} "
for ext in .gbs .gbs.gz; do

# get all matching files ending with $ext
mapfile -t files < <( compgen -f -X "!*$ext" -- "$cur" )

# add trailing space to filenames
for (( i=0; i < ${#files[@]}; i++ )); do
if ! [ -d "${files[$i]}" ]; then # prevent directories from also showing up as files
COMPREPLY[${#COMPREPLY[@]}]="${files[$i]} "
fi
done

done

# add trailing slash to directories
# get all matching directories
local -a dirs
mapfile -t dirs < <( compgen -d -- "$cur" )

# add trailing slash to directories
for (( i=0; i < ${#dirs[@]}; i++ )); do
COMPREPLY[${#COMPREPLY[@]}]="${dirs[$i]}/"
done
Expand All @@ -44,11 +53,13 @@ __gbsplay_expand_subsongs()
}

__gbsplay_return_empty_completion()
# FIXME: empty COMPREPLY seems to fall back to default filename completion - how can we avoid that?
{
COMPREPLY=()
}

__gbsplay()
# FIXME: gbsinfo don't do tilde expansion, eg. '~/foo.gbs' as a filename won't be found
{
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[$(( COMP_CWORD - 1))]}
Expand Down

0 comments on commit 94d7988

Please sign in to comment.