build-debian-toolbox: Avoid deb.debian.org
[bin.git] / shellpresent
1 #!/bin/bash
2 #
3 # Present a list of shell commands by single-stepping and echoing commands.
4 # Note that this only works well for self-contained single lines, no multi-line
5 # loops etc.
6 #
7 # (c) 2009 Martin Pitt <martin@piware.de>
8 # License: Public Domain
9
10 NORMAL=""
11 BOLD=";1"
12 STATE=$NORMAL
13
14 OFF=0
15 RED=31$STATE
16 GREEN=32$STATE
17 YELLOW=33$STATE
18 BLUE=34$STATE
19 MAGENTA=35$STATE
20 CYAN=36$STATE
21
22 # Presentation colors
23 COMMENT=$GREEN
24 PROMPT=$RED
25 CMD=$BLUE
26 OUTPUT=$OFF
27
28 if [ "x$1" = 'x--dark' ]; then
29     shift || true
30     STATE=$BOLD
31     CMD=$YELLOW
32 fi
33
34 [ -e "$1" ] || {
35     echo "Usage: $0 [--dark] <shell script>" >&2
36     exit 1
37 }
38
39 clear
40 echo -en "\e[${PROMPT}m\$\e[${CMD}m "
41
42 while read line; do
43     # paragraph clears screen
44     if [ -z "$line" ]; then
45         clear
46         echo -en "\e[${PROMPT}m\$\e[${CMD}m "
47         continue
48     fi
49
50     # comment
51     if [ "${line:0:1}" = "#" ]; then
52         if [ -n "${line:1}" ]; then
53             echo -e "\r\e[${COMMENT}m$line\e[${CMD}m"
54             echo -en "\e[${PROMPT}m\$\e[${CMD}m "
55         fi
56         continue
57     fi
58
59     # execute command
60     echo -n "$line"
61     read < /dev/tty
62     echo -ne "\e[${OUTPUT}m"
63     eval "$line"
64     echo -en "\n\e[${PROMPT}m\$\e[${CMD}m "
65     read -s < /dev/tty
66 done < "$1"