diff --git a/etc/profile.d/help50.sh b/etc/profile.d/help50.sh index f33f7c6..490e09f 100644 --- a/etc/profile.d/help50.sh +++ b/etc/profile.d/help50.sh @@ -10,15 +10,19 @@ HELPERS="/opt/cs50/lib/help50" . /opt/cs50/lib/cli # Disable yes, lest users type it at prompt -if command -v yes &> /dev/null; then - function yes() { - if [[ -t 0 ]]; then - : - else - command yes - fi - } -fi +function yes() { + if [[ -t 0 ]]; then + _alert "That was a rhetorical question. :)" + else + command yes + fi +} +alias y=yes + +# Don't override `n`, though +function no() { + _alert "That was a rhetorical question. :)" +} # Ignore duplicates (but not commands that begin with spaces) export HISTCONTROL="ignoredups" @@ -98,7 +102,7 @@ function _help50() { if ! type _helpful >/dev/null 2>&1; then function _helpful() { local output=$(_ansi "$1") - echo -e "\033[33m${output}\033[39m" # Yellow + _alert "$output" } fi if ! type _helpless >/dev/null 2>&1; then diff --git a/opt/cs50/bin/help50 b/opt/cs50/bin/help50 index 7e46f6d..1120587 100755 --- a/opt/cs50/bin/help50 +++ b/opt/cs50/bin/help50 @@ -57,6 +57,13 @@ function _start() { } function _stop() { + + # If not helping + if [[ -z "$HELP50" ]]; then + return 0 + fi + + # Kill grandparent process (i.e., `script` itself) local ppid=$(ps -o ppid= -p $$) # bash --login local gppid=$(ps -o ppid= -p $ppid) # sh -c local ggppid=$(ps -o ppid= -p $gppid) # script diff --git a/opt/cs50/lib/cli b/opt/cs50/lib/cli index 9c1384d..0ea76be 100644 --- a/opt/cs50/lib/cli +++ b/opt/cs50/lib/cli @@ -1,3 +1,7 @@ +function _alert() { + echo -e "\033[33m${1}\033[39m" # Yellow +} + function _ansi() { # If command-line arguments @@ -10,8 +14,8 @@ function _ansi() { fi # Format backticks as bold - bold=$(printf '\033[1m') - normal=$(printf '\033[22m') + local bold=$(printf '\033[1m') + local normal=$(printf '\033[22m') echo "$input" | sed "s/\`\\([^\`]*\\)\`/${bold}\\1${normal}/g" } @@ -22,31 +26,11 @@ function _search() { return fi - # Find any $1 in descendants - paths=$(find $(pwd) -name "$1" 2> /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 - - # Find any $1 relative to `cd` - pushd "$(cd && pwd)" > /dev/null - paths=$(find $(pwd) -name "$1" 2> /dev/null) - popd > /dev/null - fi - fi + # Find any $1 in descendants of $WORKDIR + paths=$(find "$WORKDIR" -name "$1" -printf "%T+ %p\n" | sort -nr | awk '{print $2}' 2> /dev/null) # Count paths - count=$(echo "$paths" | grep -c .) + local count=$(echo "$paths" | grep -c .) # If just one if [[ "$count" -eq 1 ]]; then @@ -60,7 +44,7 @@ function _sure() { if [[ $# -ne 1 ]]; then return 1 fi - prompt=$(echo "$1" | _ansi) + local prompt=$(echo "$1" | _ansi) while true; do read -p "$prompt [y/N] " -r if [[ "${REPLY,,}" =~ ^(y|yes)$ ]]; then diff --git a/opt/cs50/lib/help50/bash b/opt/cs50/lib/help50/bash index 556b6e3..e0be583 100755 --- a/opt/cs50/lib/help50/bash +++ b/opt/cs50/lib/help50/bash @@ -2,6 +2,7 @@ output=$(cat) +# touch foo.py && foo.py regex="bash: (.*\.py): command not found" if [[ "$output" =~ $regex ]]; then @@ -12,12 +13,14 @@ if [[ "$output" =~ $regex ]]; then fi fi +# mkdir foo && ./foo regex="bash: \./([^:]*): Is a directory" if [[ "$output" =~ $regex ]]; then - echo "Cannot execute a directory. Did you mean to \`cd\` into \`${BASH_REMATCH[1]}\` instead?" + echo "Cannot execute a directory. Did you mean to run \`cd ${BASH_REMATCH[1]}\`?" exit fi +# touch foo.c && ./foo.c regex="bash: \./(([^:]*)\.c): Permission denied" if [[ "$output" =~ $regex ]]; then @@ -28,7 +31,7 @@ if [[ "$output" =~ $regex ]]; then fi fi -regex="bash: (\./.*\.py): Permission denied" +regex="bash: \./(.*\.py): Permission denied" if [[ "$output" =~ $regex ]]; then # If file exists @@ -38,6 +41,7 @@ if [[ "$output" =~ $regex ]]; then fi fi +# touch foo && /.foo regex="bash: /\.([^:]*): No such file or directory" if [[ "$output" =~ $regex ]]; then diff --git a/opt/cs50/lib/help50/cd b/opt/cs50/lib/help50/cd index a9bf588..c451e02 100755 --- a/opt/cs50/lib/help50/cd +++ b/opt/cs50/lib/help50/cd @@ -1 +1,20 @@ #!/bin/bash + +. /opt/cs50/lib/cli + +output=$(cat) + +# mkdir -p foo/bar && cd bar +regex="cd: (.*): No such file or directory" +if [[ "$output" =~ $regex ]]; then + + # Search recursively for directory + dir="${BASH_REMATCH[1]}" + parent=$(_search "$dir") + echo -n "There isn't a directory called \`$dir\` in your current directory." + if [[ ! -z "$parent" ]]; then + echo " Did you mean to \`cd $parent\` first?" + else + echo + fi +fi