Skip to content

Instantly share code, notes, and snippets.

@mattn
Created May 29, 2009 16:36
Show Gist options
  • Save mattn/120051 to your computer and use it in GitHub Desktop.
Save mattn/120051 to your computer and use it in GitHub Desktop.
mpc complete function for bash
function _mpc() {
local cur prev temp word cword
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $(mpc --help 2>&1 | sed -n "s/^mpc \\($cur[a-z]\\+\\) .*$/\\1/p") )
return
elif [ $COMP_CWORD -eq 2 ]; then
case "$prev" in
ls|add)
word=`echo $cur | sed 's/\\\\//g'`
temp=`mktemp $$.XXXXXX`
if [ "`echo "$word" | grep /`" != "" ]; then
word=`echo "$word" | sed 's#\(.*\)/[^\/]*$#\1#'`
else
word=''
fi
mpc ls "$word" 2> /dev/null | grep "$cur" > $temp
i=0
while read line; do
line=`echo $line | sed 's/ /\\\\ /g'`
is_mp3=`echo $line | grep .mp3$`
if [ $prev = ls ]; then
if [ ! "$is_mp3" ]; then
COMPREPLY[$i]=$line
i=`expr $i + 1`
COMPREPLY[$i]=$line/
i=`expr $i + 1`
fi
else
COMPREPLY[$i]=$line
i=`expr $i + 1`
if [ ! "$is_mp3" ]; then
COMPREPLY[$i]=$line/
i=`expr $i + 1`
fi
fi
done < $temp
rm -f $temp
return 0
;;
esac
fi
COMPREPLY=( $( compgen -f -- $cur ) )
return 0
}
complete -F _mpc mpc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment