Skip to content

Commit

Permalink
renamed helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dmalan committed Jan 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a69df22 commit ebaec05
Showing 2 changed files with 33 additions and 11 deletions.
40 changes: 31 additions & 9 deletions etc/profile.d/help50.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions opt/cs50/lib/help50/make
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ebaec05

Please sign in to comment.