-
Notifications
You must be signed in to change notification settings - Fork 0
/
bashtutor_helper
executable file
·90 lines (73 loc) · 2.57 KB
/
bashtutor_helper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
function bashtutor_greet() {
echo -n -e "\033[32m"
echo "Welcome! You are running bashtutor ${BASHTUTOR_VERSION}."
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
if [ -d "$1" ]
then BASHTUTOR_TASK_FILE_LIST_FILE="$1/task_list.sh"
else BASHTUTOR_TASK_FILE_LIST_FILE="$1"
fi
declare -a BASHTUTOR_TASK_FILE_LIST
source "$BASHTUTOR_TASK_FILE_LIST_FILE" ||
(echo "'$BASHTUTOR_TASK_FILE_LIST' 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
BASHTUTOR_MODULE_DIRNAME=$(realpath "$(dirname "$BASHTUTOR_TASK_FILE_LIST_FILE")")
BASHTUTOR_TMPDIR=$(mktemp -d)
cp bashtutor_helper ${BASHTUTOR_TMPDIR}
cd ${BASHTUTOR_TMPDIR}
alias task='bashtutor_wrap description'
alias hint='bashtutor_wrap hint'
BASHTUTOR_PROGRESS=0
BASHTUTOR_PS1='(bashtutor) $ '
PS1=$BASHTUTOR_PS1
PROMPT_COMMAND=()
PROMPT_COMMAND="BASHTUTOR_R=\$?; source ${PWD}/bashtutor_helper"
IGNOREEOF=3 # We dont want the user to be able to quit by mistake
BASHTUTOR_VERSION="v1.2"
NORMAL="\033[0m"
BOLD="\033[1m"
ITALICS="\033[3m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
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