Add capturable output to kd

This commit is contained in:
Loic Nageleisen 2020-04-13 12:00:27 +02:00
parent 2cd00b16f2
commit 00d9c4051d

View file

@ -1,13 +1,22 @@
# vim: ft=sh
# shellcheck shell=bash
# worKDir # worKDir
function kd() { function kd() {
local conf="$HOME/.kdrc" local conf="$HOME/.kdrc"
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
local candidate="$PWD" local candidate="$PWD"
while [[ -n "$candidate" ]]; do while [[ -n "$candidate" ]]; do
for file in Gemfile Procfile; do for file in Gemfile Procfile .git; do
if [[ -f "$candidate/$file" ]]; then if [[ -e "$candidate/$file" ]]; then
cd "$candidate" if [[ -t 1 ]]; then
return $? cd "$candidate" || return 1
return
else
[[ -d "$target" ]] || return 1
echo "$candidate"
return
fi
fi fi
done done
candidate="${candidate%/*}" candidate="${candidate%/*}"
@ -18,16 +27,23 @@ function kd() {
local target local target
target="$(grep -e "^$1" "$conf" | awk '{ print $2 }' | tail -1)" target="$(grep -e "^$1" "$conf" | awk '{ print $2 }' | tail -1)"
if [[ -n "$target" ]]; then if [[ -n "$target" ]]; then
cd "$target" if [[ -t 1 ]]; then
return $? cd "$target" || return 1
return
else
[[ -d "$target" ]] || return 1
echo "$target"
return
fi
fi fi
return 1 return 1
fi fi
if [[ $# -eq 2 ]]; then if [[ $# -eq 2 ]]; then
local target local target
local name
name="$1" name="$1"
if [[ -d "$2" ]]; then if [[ -d "$2" ]]; then
target=$(cd "$2"; pwd) target=$(cd "$2" && pwd)
if [[ -n "$target" ]]; then if [[ -n "$target" ]]; then
sed -i -e "/^$name/d" "$conf" sed -i -e "/^$name/d" "$conf"
echo "$name" "$target" >> "$conf" echo "$name" "$target" >> "$conf"
@ -36,5 +52,3 @@ function kd() {
return 1 return 1
fi fi
} }
# vim: ft=sh