Files
bashtutor/bashtutor_helper
2024-06-12 22:02:50 +02:00

90 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
function bashtutor_greet() {
echo -n -e "\033[32m"
echo "Welcome! You are running bashtutor v1.0."
echo "The currently loaded module is '$1' with ${#BASHTUTOR_TASK_FILE_LIST[@]} tasks."
echo "You may run 'task' or 'hint' anytime to display" \
"the current task again or a help message respectably."
echo -e "\033[0m"
}
function bashtutor_completed() {
echo -n -e "\033[32m"
echo "Module completed. Thanks for flying bashtutor!"
echo -n -e "\033[0m"
exit
}
function bashtutor_wrap() {
source $BASHTUTOR_MODULE_DIRNAME/${BASHTUTOR_TASK_FILE_LIST[$BASHTUTOR_PROGRESS]}
$@
r=$?
unset description
unset hint
unset validate
return $r
}
if [ $# -ne 0 ]; then # Initial call
set -o errexit
BASHTUTOR_PROGRESS=0
BASHTUTOR_MODULE_DIRNAME=$(dirname $1)
BASHTUTOR_PS1="(bashtutor) \\$ "
NORMAL="\033[0m"
ITALICS="\033[3m"
BOLD="\033[1m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
BASHTUTOR_TMPDIR=$(mktemp -d)
cp -r ${BASHTUTOR_MODULE_DIRNAME} ${BASHTUTOR_TMPDIR}
cp bashtutor_helper ${BASHTUTOR_TMPDIR}
cd ${BASHTUTOR_TMPDIR}
declare -a BASHTUTOR_TASK_FILE_LIST
source $1 ||
(echo "'$1' does not seem like a module. Exiting..." && exit 1)
! [ -v BASHTUTOR_TASK_FILE_LIST ] &&
echo "Module sourced, but no tasks were specified. Exiting..." &&
exit 1
alias task='bashtutor_wrap description'
alias hint='bashtutor_wrap hint'
PS1=$BASHTUTOR_PS1
PROMPT_COMMAND=()
PROMPT_COMMAND="BASHTUTOR_R=\$?; source ${PWD}/bashtutor_helper"
IGNOREEOF=3
bashtutor_greet $1
bashtutor_wrap description
set +o errexit
else # Event callback
LAST_CMD="$(history | tail -n 1 | cut -c 8-)"
bashtutor_wrap validate $LAST_CMD
if [ $? == 1 ]; then
BASHTUTOR_PROGRESS=$(expr $BASHTUTOR_PROGRESS + 1)
[ -z "${BASHTUTOR_TASK_FILE_LIST[$BASHTUTOR_PROGRESS]}" ] && bashtutor_completed
echo ""
echo -e "${GREEN}($((${BASHTUTOR_PROGRESS}))/$((${#BASHTUTOR_TASK_FILE_LIST[@]})))${NORMAL}"
echo ""
task
fi
fi