From 00d9c4051d04e6fa1fa6ed09d40c0b9451324450 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Mon, 13 Apr 2020 12:00:27 +0200 Subject: [PATCH] Add capturable output to kd --- shell/kd | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/shell/kd b/shell/kd index ea0933b..f7357c6 100755 --- a/shell/kd +++ b/shell/kd @@ -1,13 +1,22 @@ +# 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 Procfile; do - if [[ -f "$candidate/$file" ]]; then - cd "$candidate" - return $? + for file in Gemfile Procfile .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%/*}" @@ -18,16 +27,23 @@ function kd() { local target target="$(grep -e "^$1" "$conf" | awk '{ print $2 }' | tail -1)" if [[ -n "$target" ]]; then - cd "$target" - return $? + 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) + target=$(cd "$2" && pwd) if [[ -n "$target" ]]; then sed -i -e "/^$name/d" "$conf" echo "$name" "$target" >> "$conf" @@ -36,5 +52,3 @@ function kd() { return 1 fi } - -# vim: ft=sh