Skip to content

Commit

Permalink
Make jump posix compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
mal committed Jul 12, 2018
1 parent b5db8e1 commit 7f2652c
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions .common.d/jump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,44 @@ export JMP_LIST=""
export JMP_OPTS=""
export JMP_PATH=$HOME/code

jmp() {
local dir="$(_jmp_list path $1)"
jmp()
{
local dir="$(_jmp_list path $1)"

if [ -n "$dir" ]
then cd "$dir"; return
fi
if [ -n "$dir" ]
then cd "$dir"; return
fi

local dir="$(_jmp_search -name .git -path "*/$1/.git" -type d | head -n1)"
local dir="$(_jmp_search -path "*/$1/.git" | head -n1)"

if [ -n "$dir" ]
then cd "$(dirname "$dir")"
else echo "err: unknown jump" >&2; return 1
fi
if [ -n "$dir" ]
then cd "$(dirname "$dir")"
else echo "err: unknown jump" >&2; return 1
fi
}

_jmp_completion() {
reply=( \
$(_jmp_list names) \
$(_jmp_search -name .git -type d | xargs -n1 dirname | xargs -n1 basename) \
)
_jmp_list()
{
echo "$JMP_LIST" | tr ':' '\n' | case "$1" in
names) cut -d\| -f1 ;;
path) grep "^$2|" | cut -d\| -f2 ;;
esac
}

_jmp_list() {
case "$1" in
names)
echo "$JMP_LIST" | tr ':' '\n' | cut -d\| -f1
;;
path)
echo "$JMP_LIST" | tr ':' '\n' | grep "^$2|" | cut -d\| -f2
;;
esac
}

_jmp_search() {
local IFS="$(echo \'\\n\')"
set -- $(echo $JMP_PATH | tr ':' '\n') $(echo $JMP_OPTS | tr ' ' '\n') "$@"
unset IFS
_jmp_search()
{
local IFS="$(printf '\nn')"; IFS="${IFS%n}"
set -- $(echo $JMP_PATH | tr ':' '\n') $(echo $JMP_OPTS | tr ' ' '\n') \
-name .git -type d "$@"
unset IFS

find "$@"
find "$@"
}

if type compctl &> /dev/null
then compctl -x 'p[1]' -K _jmp_completion -- jmp
if type compctl > /dev/null 2>&1; then
_jmp_comp()
{
reply=($(_jmp_list names) $(_jmp_search | rev | cut -d/ -f2 | rev))
}
compctl -x 'p[1]' -K _jmp_comp -- jmp
fi

0 comments on commit 7f2652c

Please sign in to comment.