__sibling_parse() {
count=1
file=
while [ $# -gt 0 ]; do
case "$1" in
-f | --file)
if [ -z "${2:-}" ]; then
echo "sibling: $1: no file is given" >&2
return 2
fi
file=$2
shift 2
;;
*)
count=$1
shift
;;
esac
done
}
__sibling_find() {
sibling --type "$1" --step "$2" -- "${3:-$PWD}"
}
__sibling_position() {
sibling --progress --type keep -- "${1:-$PWD}"
}
__sibling_report() {
if [ "$1" -eq 1 ]; then
echo "sibling: no more sibling directory" >&2
fi
return "$1"
}
__sibling_cd() {
local type=$1 count file next code
shift
__sibling_parse "$@" || return $?
next=$(__sibling_find "$type" "$count" "$file")
code=$?
if [ $code -ne 0 ]; then
__sibling_report $code
return $code
fi
cd -- "$next" || return $?
__sibling_position "$file"
}
__sibling_ls() {
local type=$1 count file next code
shift
__sibling_parse "$@" || return $?
next=$(__sibling_find "$type" "$count" "$file")
code=$?
if [ $code -ne 0 ]; then
__sibling_report $code
return $code
fi
echo "$next"
ls -- "$next"
}
__sibling_cd_with_filter() {
local filter=$1 count file selected code
shift
__sibling_parse "$@" || return $?
selected=$(sibling --format list --type keep -- "${file:-$PWD}" | "$filter")
code=$?
if [ $code -ne 0 ] || [ -z "$selected" ]; then
return $code
fi
selected=$(printf '%s\n' "$selected" | sed -E 's/^ *[0-9]+ (\* |> | )//')
cd -- "$selected" || return $?
__sibling_position "$file"
}
__sibling_print() {
local type=$1 count file next code
shift
__sibling_parse "$@" || return $?
next=$(__sibling_find "$type" "$count" "$file")
code=$?
if [ $code -ne 0 ]; then
return $code
fi
echo "$next"
}
__sibling_hook() {
NEXTDIR=$(__sibling_find next 1 "" 2> /dev/null) || NEXTDIR=
PREVDIR=$(__sibling_find previous 1 "" 2> /dev/null) || PREVDIR=
export NEXTDIR PREVDIR
}
sibling_hook_enable() {
if [ -n "${ZSH_VERSION:-}" ]; then
typeset -ga chpwd_functions
case " ${chpwd_functions[*]} " in
*" __sibling_hook "*) ;;
*) chpwd_functions+=(__sibling_hook) ;;
esac
else
case "${PROMPT_COMMAND:-}" in
*__sibling_hook*) ;;
*) PROMPT_COMMAND="__sibling_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}" ;;
esac
fi
__sibling_hook
}
sibling_hook_disable() {
local kept f
if [ -n "${ZSH_VERSION:-}" ]; then
kept=()
for f in "${chpwd_functions[@]}"; do
[ "$f" = "__sibling_hook" ] || kept+=("$f")
done
chpwd_functions=("${kept[@]}")
else
PROMPT_COMMAND=$(printf '%s' "${PROMPT_COMMAND:-}" | sed -e 's/__sibling_hook;\{0,1\}//')
fi
unset NEXTDIR PREVDIR
}
nextdir() {
__sibling_print next "$@"
}
prevdir() {
__sibling_print previous "$@"
}
cdnext() {
__sibling_cd next "$@"
}
cdprev() {
__sibling_cd previous "$@"
}
cdfirst() {
__sibling_cd first "$@"
}
cdlast() {
__sibling_cd last "$@"
}
cdrand() {
__sibling_cd random "$@"
}
lsnext() {
__sibling_ls next "$@"
}
lsprev() {
__sibling_ls previous "$@"
}
lsfirst() {
__sibling_ls first "$@"
}
lslast() {
__sibling_ls last "$@"
}
lsrand() {
__sibling_ls random "$@"
}
sibling_peco() {
__sibling_cd_with_filter peco "$@"
}
sibling_fzf() {
__sibling_cd_with_filter fzf "$@"
}