Skip to content

Commit

Permalink
WIP8
Browse files Browse the repository at this point in the history
  • Loading branch information
dmalan committed May 8, 2024
1 parent 2801456 commit 35dd708
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 38 deletions.
24 changes: 14 additions & 10 deletions etc/profile.d/help50.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions opt/cs50/bin/help50
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 10 additions & 26 deletions opt/cs50/lib/cli
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function _alert() {
echo -e "\033[33m${1}\033[39m" # Yellow
}

function _ansi() {

# If command-line arguments
Expand All @@ -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"
}

Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions opt/cs50/lib/help50/bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

output=$(cat)

# touch foo.py && foo.py
regex="bash: (.*\.py): command not found"
if [[ "$output" =~ $regex ]]; then

Expand All @@ -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

Expand All @@ -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
Expand All @@ -38,6 +41,7 @@ if [[ "$output" =~ $regex ]]; then
fi
fi

# touch foo && /.foo
regex="bash: /\.([^:]*): No such file or directory"
if [[ "$output" =~ $regex ]]; then

Expand Down
19 changes: 19 additions & 0 deletions opt/cs50/lib/help50/cd
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 35dd708

Please sign in to comment.