diff --git a/etc/profile.d/cli.sh b/etc/profile.d/cli.sh index 971de08..bb1251f 100644 --- a/etc/profile.d/cli.sh +++ b/etc/profile.d/cli.sh @@ -14,6 +14,36 @@ if [ "$(whoami)" != "root" ]; then PS1='\[$(printf "\x0f")\033[01;34m\]$(_cwdSlashAtEnd)\[\033[00m\]$(__git_ps1 " (%s)") $ ' fi + # Are you sures + function _ansi() { + + # If command-line arguments + if [[ $# -ne 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" + } + function _sure() { + if [[ $# -ne 1 ]]; then + return 1 + fi + prompt=$(echo "$1" | _ansi) + while true; do + read -p "$prompt [y/N] " -r + if [[ "${REPLY,,}" =~ ^(y|yes)$ ]]; then + return 0 + elif [[ "${REPLY,,}" =~ ^(n|no)$ ]]; then + return 1 + fi + done + } + # Aliases alias cd="HOME=\"$WORKDIR\" cd" alias cowsay="/usr/games/cowsay" diff --git a/opt/cs50/bin/http-server b/opt/cs50/bin/http-server index a0f1c26..28f8207 100755 --- a/opt/cs50/bin/http-server +++ b/opt/cs50/bin/http-server @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -l # Default options a="-a 0.0.0.0" @@ -9,22 +9,16 @@ port="-p 8080" options="--no-dotfiles" t="-t0" -# Formatting -bold=$(tput bold) -normal=$(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 ${bold}http-server${normal} and not ${bold}flask${normal}? [y/N] " -r - if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then + if ! _sure "Are you sure you want to run \`http-server\` and not \`flask\`?"; then exit 1 fi fi # Check for path if [[ $# -eq 1 ]] && [[ $1 != -* ]] && [[ ! $1 =~ ^\./?$ ]]; then - read -p "Are you sure you want to serve ${bold}${1}${normal} and not your current directory? [y/N] " -r - if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then + if ! _sure "Are you sure you want to serve \`${1}\` and not your current directory?"; then exit 1 fi fi diff --git a/opt/cs50/bin/make b/opt/cs50/bin/make index 4658e3e..d4275e8 100755 --- a/opt/cs50/bin/make +++ b/opt/cs50/bin/make @@ -1,23 +1,20 @@ #!/bin/bash -# Ensure no targets end with .c -args="" -invalid_args=0 -for arg; do - case "$arg" in - (*.c) arg=${arg%.c}; invalid_args=1;; - esac - args="$args $arg" -done -if [ $invalid_args -eq 1 ]; then - echo "Did you mean 'make$args'?" - exit 1 -fi +# If a single target and not an option +if [[ $# -eq 1 ]] && [[ "$1" != -* ]]; then + + # If no Makefile + if [[ ! -f Makefile && ! -f makefile ]]; then -# Run make -if [[ -d "$1" ]]; then - echo "$1 is a directory" - exit 1 -else - /usr/bin/make -B -s $* + # If target ends with .c or is a directory + if [[ "$1" == *?.c || -d "$1" ]]; then + + # Don't suppress "Nothing to be done" with --silent + /usr/bin/make "$1" + exit $? + fi + fi fi + +# Don't echo recipes +/usr/bin/make --always-make --silent "$@" diff --git a/opt/cs50/bin/sqlite3 b/opt/cs50/bin/sqlite3 index 3c1c0e7..fc2c61b 100755 --- a/opt/cs50/bin/sqlite3 +++ b/opt/cs50/bin/sqlite3 @@ -1,8 +1,4 @@ -#!/bin/bash - -# Formatting -bold=$(tput bold) -normal=$(tput sgr0) +#!/bin/bash -l # If data is coming from stdin (pipe or redirection) if [[ -p /dev/stdin || ! -t 0 ]]; then @@ -12,22 +8,19 @@ fi # If no command-line argument if [[ $# -eq 0 ]]; then - read -p "Are you sure you want to run ${bold}sqlite3${normal} without a command-line argument (e.g., the filename of a database)? [y/N] " -r - if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then + if ! _sure "Are you sure you want to run \`sqlite3\` without a command-line argument (e.g., the filename of a database)?"; then exit 1 fi -# If one command-line argument +# If one command-line argument and not an option elif [[ $# -eq 1 ]] && [[ ! "$1" =~ ^- ]]; then if [[ ! -f "$1" ]]; then if [[ ! "$1" =~ \.db$ ]]; then - read -p "Are you sure you want to create ${bold}$1${normal}? SQLite filenames usually end in ${bold}.db${normal}. [y/N] " -r - if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then + if ! _sure "Are you sure you want to create \`$1\`? SQLite filenames usually end in \`.db\`."; then exit 1 fi else - read -p "Are you sure you want to create ${bold}$1${normal}? [y/N] " -r - if [[ ! "${REPLY,,}" =~ ^y|yes$ ]]; then + if ! _sure "Are you sure you want to create \`$1\`?"; then exit 1 fi fi diff --git a/opt/cs50/bin/valgrind b/opt/cs50/bin/valgrind index 60a6429..6e54252 100755 --- a/opt/cs50/bin/valgrind +++ b/opt/cs50/bin/valgrind @@ -1,13 +1,33 @@ -#!/bin/bash +#!/bin/bash -l -# Formatting -bold=$(tput bold) -normal=$(tput sgr0) +# If one command-line argument and not an option +if [[ $# -eq 1 ]] && [[ "$1" != -* ]]; then -# If run on Python program -if [[ "$1" == "python" || "$1" == *.py ]]; then - echo "Afraid ${bold}valgrind${normal} does not support Python programs!" - exit 1 + # If run on Python program + if [[ "$1" =~ ^(python|python3)$ ]]; then + if ! _sure "Are you sure you want to run \`valgrind\` on \`$1\`? Valgrind does not support Python programs."; then + exit 1 + fi + fi + + # If not run on ./* + if [[ ! "$1" =~ ^./ ]]; then + + # If $1 is in $PATH + if command -v "$1" &> /dev/null; then + + # Discourage user from debugging $1 + if [[ -f "$1" ]]; then + if ! _sure "Are you sure you want to run \`valgrind $1\` and not, e.g., \`valgrind ./$1\`?"; then + exit 1 + fi + else + if ! _sure "Are you sure you want to run \`valgrind\` on \`$1\`, which isn't in your current directory?"; then + exit 1 + fi + fi + fi + fi fi /usr/bin/valgrind "$@"