Skip to content

Commit

Permalink
added _help function to standardize formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dmalan committed Jan 6, 2024
1 parent a0783ce commit cadd890
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
3 changes: 1 addition & 2 deletions etc/profile.d/help50.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ _help50() {

_helpful() {
echo -n "🦆 "
# https://www.gnu.org/software/termutils/manual/termutils-2.0/html_chapter/tput_1.html#SEC8
cat "$1" | sed "s/\`\([^\`]*\)\`/$(tput smso)\1$(tput rmso)/g"
cat "$1" | _help
}

_helpless() { :; }
Expand Down
13 changes: 13 additions & 0 deletions opt/cs50/bin/_help
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# If command-line arguments
if [[ -t 0 ]]; then
input="$*"

# If standard input
else
input=$(cat)
fi

# https://www.gnu.org/software/termutils/manual/termutils-2.0/html_chapter/tput_1.html#SEC8
echo "$input" | sed "s/\`\([^\`]*\)\`/$(tput smso)\1$(tput rmso)/g"
8 changes: 2 additions & 6 deletions opt/cs50/bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@ port="-p 8080"
options="--no-dotfiles"
t="-t0"

# Formatting
smso=$(tput smso)
rmso=$(tput sgr0)

# Check for app.py or wsgi.py
if [[ -f app.py ]] || [[ -f wsgi.py ]]; then
read -p "Are you sure you want to run ${smso}http-server${rmso} and not ${smso}flask${rmso}? [y/N] " -r
read -p "$(_help "Are you sure you want to run \`http-server\` and not \`flask\`? [y/N] ")" -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
fi

# Check for path
if [[ $# -eq 1 ]] && [[ $1 != -* ]] && [[ ! $1 =~ ^\./?$ ]]; then
read -p "Are you sure you want to serve ${smso}${1}${rmso} and not your current directory? [y/N] " -r
read -p "$(_help "Are you sure you want to serve \`${1}\` and not your current directory? [y/N] ")" -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
Expand Down
10 changes: 3 additions & 7 deletions opt/cs50/bin/sqlite3
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#!/bin/bash

# Formatting
smso=$(tput smso)
rmso=$(tput sgr0)

# If data is coming from stdin (pipe or redirection)
if [[ -p /dev/stdin || ! -t 0 ]]; then
/usr/local/bin/sqlite3 -nullvalue NULL -table "$@" < /dev/stdin
Expand All @@ -12,7 +8,7 @@ fi

# If no command-line argument
if [[ $# -eq 0 ]]; then
read -p "Are you sure you want to run ${smso}sqlite3${rmso} without a command-line argument (e.g., the filename of a database)? [y/N] " -r
read -p "$(_help "Are you sure you want to run \`sqlite3\` without a command-line argument (e.g., the filename of a database)? [y/N] ")" -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
Expand All @@ -21,12 +17,12 @@ if [[ $# -eq 0 ]]; then
elif [[ $# -eq 1 ]] && [[ ! "$1" =~ ^- ]]; then
if [[ ! -f "$1" ]]; then
if [[ ! "$1" =~ \.db$ ]]; then
read -p "Are you sure you want to create ${smso}$1${rmso}? SQLite filenames usually end in ${smso}.db${rmso}. [y/N] " -r
read -p "$(_help "Are you sure you want to create \`$1\`? SQLite filenames usually end in \`.db\`. [y/N] ")" -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
else
read -p "Are you sure you want to create ${smso}$1${rmso}? [y/N] " -r
read -p "$(_help "Are you sure you want to create \`$1\`? [y/N] ")" -r
if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then
exit 1
fi
Expand Down
6 changes: 1 addition & 5 deletions opt/cs50/bin/valgrind
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#!/bin/bash

# Formatting
smso=$(tput smso)
rmso=$(tput sgr0)

# If run on Python program
if [[ "$1" == "python" || "$1" == *.py ]]; then
echo "Afraid ${smso}valgrind${rmso} does not support Python programs!"
echo "$(_help "Afraid \`valgrind\` does not support Python programs!")"
exit 1
fi

Expand Down

0 comments on commit cadd890

Please sign in to comment.