1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00
wting_autojump/bin/autojump.bash

141 lines
3.1 KiB
Bash
Raw Normal View History

if [[ -z $AUTOJUMP_SOURCED ]]; then
export AUTOJUMP_SOURCED=1
# Script include
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# set user installation paths
export PATH="${SCRIPT_DIR}:${PATH}"
# set error file location
if [[ "$(uname)" == "Darwin" ]]; then
2014-01-08 04:13:01 +00:00
export AUTOJUMP_ERROR_PATH=~/Library/autojump/errors.log
elif [[ -n "${XDG_DATA_HOME}" ]]; then
2014-01-08 04:13:01 +00:00
export AUTOJUMP_ERROR_PATH="${XDG_DATA_HOME}/autojump/errors.log"
else
2014-01-08 04:13:01 +00:00
export AUTOJUMP_ERROR_PATH=~/.local/share/autojump/errors.log
fi
2014-01-08 04:13:01 +00:00
if [[ ! -d "$(dirname ${AUTOJUMP_ERROR_PATH})" ]]; then
mkdir -p "$(dirname ${AUTOJUMP_ERROR_PATH})"
fi
2014-01-08 04:13:01 +00:00
# enable tab completion
_autojump() {
local cur
cur=${COMP_WORDS[*]:1}
comps=$(autojump --complete $cur)
echo "$comps" | while read i; do
COMPREPLY=("${COMPREPLY[@]}" "${i}")
done
}
complete -F _autojump j
2012-02-08 21:27:43 +00:00
# change pwd hook
autojump_add_to_database() {
if [[ -f "${AUTOJUMP_ERROR_PATH}" ]]; then
(autojump --add "$(pwd)" >/dev/null 2>>${AUTOJUMP_ERROR_PATH} &) &>/dev/null
else
(autojump --add "$(pwd)" >/dev/null &) &>/dev/null
fi
}
case $PROMPT_COMMAND in
2013-02-01 20:59:34 +00:00
*autojump*)
;;
2013-02-01 20:59:34 +00:00
*)
PROMPT_COMMAND="${PROMPT_COMMAND:+$(echo "${PROMPT_COMMAND}" | awk '{gsub(/; *$/,"")}1') ; }autojump_add_to_database"
;;
esac
# default autojump command
j() {
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
autojump ${@}
return
fi
output="$(autojump ${@})"
if [[ -d "${output}" ]]; then
if [ -t 1 ]; then # if stdout is a terminal, use colors
echo -e "\\033[31m${output}\\033[0m"
else
echo -e "${output}"
fi
cd "${output}"
else
echo "autojump: directory '${@}' not found"
echo "\n${output}\n"
echo "Try \`autojump --help\` for more information."
false
fi
}
# jump to child directory (subdirectory of current path)
jc() {
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
autojump ${@}
return
else
j $(pwd) ${@}
fi
}
# open autojump results in file browser
jo() {
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
autojump ${@}
return
2013-12-31 14:39:14 +00:00
fi
output="$(autojump ${@})"
if [[ -d "${output}" ]]; then
case ${OSTYPE} in
linux*)
xdg-open "${output}"
;;
darwin*)
open "${output}"
;;
cygwin)
cygstart "" $(cygpath -w -a ${output})
;;
*)
echo "Unknown operating system: ${OSTYPE}." 1>&2
;;
esac
2013-12-31 14:39:14 +00:00
else
echo "autojump: directory '${@}' not found"
echo "\n${output}\n"
echo "Try \`autojump --help\` for more information."
false
2013-02-14 18:59:01 +00:00
fi
}
# open autojump results (child directory) in file browser
jco() {
if [[ ${1} == -* ]] && [[ ${1} != "--" ]]; then
autojump ${@}
return
else
jo $(pwd) ${@}
fi
}
# Jump around a git repo
g() {
REPO_ROOT=`git rev-parse --show-toplevel`
j "$REPO_ROOT" "$@"
}
else
echo "ERROR: autojump was sourced twice"
fi