From ebaec05f73538101e58c80e603e2fe575f89096e Mon Sep 17 00:00:00 2001 From: "David J. Malan" Date: Thu, 18 Jan 2024 17:03:39 -0500 Subject: [PATCH] renamed helper --- etc/profile.d/help50.sh | 40 +++++++++++++++++++++++++++++++--------- opt/cs50/lib/help50/make | 4 ++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/etc/profile.d/help50.sh b/etc/profile.d/help50.sh index 5bd4601..2c0f1c8 100644 --- a/etc/profile.d/help50.sh +++ b/etc/profile.d/help50.sh @@ -90,23 +90,45 @@ function _help50() { truncate -s 0 "$SCRIPT" } -function _find() { +function _search() { # In $1 is path to find if [[ $# -ne 1 ]]; then return fi - # Find absolute paths of any $1 relative to `cd` - pushd "$(cd && pwd)" > /dev/null + # Find any $1 in descendants paths=$(find $(pwd) -name "$1" 2> /dev/null) - popd > /dev/null + if [[ -z "$paths" ]]; then + + # Find any $1 in ancestors + local dir="$(dirname "$(pwd)")" + while [[ "$dir" != "/" ]]; do + paths=$(find "$dir" -maxdepth 1 -name "$1") + if [[ -z "$paths" ]]; then + dir=$(dirname "$dir") + else + break + fi + done + if [[ -z "$paths" ]]; then - # Resolve absolute paths to relative paths - local line - while IFS= read -r path; do - realpath --relative-to=. "$(dirname "$path")" - done <<< "$paths" + # Find any $1 relative to `cd` + pushd "$(cd && pwd)" > /dev/null + paths=$(find $(pwd) -name "$1" 2> /dev/null) + popd > /dev/null + fi + fi + + # Count paths + count=$(echo "$paths" | grep -c .) + + # If just one + if [[ "$count" -eq 1 ]]; then + + # Resolve absolute path to relative path + realpath --relative-to=. "$(dirname "$paths")" + fi } if ! type _helpful >/dev/null 2>&1; then diff --git a/opt/cs50/lib/help50/make b/opt/cs50/lib/help50/make index 599127d..1cfc8ed 100755 --- a/opt/cs50/lib/help50/make +++ b/opt/cs50/lib/help50/make @@ -30,8 +30,8 @@ if [[ "$output" =~ $regex ]]; then if [[ ! -f "$c" ]]; then # Search recursively for .c file - path=$(_find "$c") - echo -n "There isn't a file called \`${c}\` in your current directory." + path=$(_search "$c") + echo -n "There isn't a file called \`${c}\` in your current directory. You are in \`$(basename "$PWD")\`." if [[ ! -z "$path" ]]; then echo " Did you mean to \`cd $path\` first?" else