dotfiles/shell/kd

54 lines
1.4 KiB
Bash
Executable file

# vim: ft=sh
# shellcheck shell=bash
# worKDir
function kd() {
local conf="$HOME/.kdrc"
if [[ $# -eq 0 ]]; then
local candidate="$PWD"
while [[ -n "$candidate" ]]; do
for file in Gemfile .git; do
if [[ -e "$candidate/$file" ]]; then
if [[ -t 1 ]]; then
cd "$candidate" || return 1
return
else
[[ -d "$target" ]] || return 1
echo "$candidate"
return
fi
fi
done
candidate="${candidate%/*}"
done
return 1
fi
if [[ $# -eq 1 ]]; then
local target
target="$(awk "\$1 = /^${1}/ {a=\$2}; END { if (a) {print a}}" "$conf")"
if [[ -n "$target" ]]; then
if [[ -t 1 ]]; then
cd "$target" || return 1
return
else
[[ -d "$target" ]] || return 1
echo "$target"
return
fi
fi
return 1
fi
if [[ $# -eq 2 ]]; then
local target
local name
name="$1"
if [[ -d "$2" ]]; then
target=$(cd "$2" && pwd)
if [[ -n "$target" ]]; then
sed -i -e "/^$name/d" "$conf"
echo "$name" "$target" >> "$conf"
fi
fi
return 1
fi
}