Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates wrappers #195

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions etc/profile.d/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 3 additions & 9 deletions opt/cs50/bin/http-server
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -l

# Default options
a="-a 0.0.0.0"
Expand All @@ -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
Expand Down
35 changes: 16 additions & 19 deletions opt/cs50/bin/make
Original file line number Diff line number Diff line change
@@ -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 "$@"
17 changes: 5 additions & 12 deletions opt/cs50/bin/sqlite3
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
36 changes: 28 additions & 8 deletions opt/cs50/bin/valgrind
Original file line number Diff line number Diff line change
@@ -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 "$@"