Debian Codegraph @

examples/demo-nav.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 # Source & docs: https://github.com/bwplotka/demo-nav
4
5 # Script options (define those variables before registering commands):
6 #
7 # The speed to "type" the text (Default: 0 so no typing view).
8 # TYPE_SPEED=40
9 #
10 # If false next command will be shown only after enter (Default: false).
11 # IMMEDIATE_REVEAL=true
12 #
13 # If true prefix line with number; easier to navigate (Default: false).
14 # NUMS=true
15 #
16 # If NUMS = false this prefix will be used (Default: '').
17 # PREFIX="CustomPrefix"
18 #
19 # Color vars for pretty prompts.
20 # Feel free to use those colors in registered commands.
21 BLACK="\033[0;30m"
22 YELLOW="\033[1;33m"
23 BLUE="\033[0;34m"
24 GREEN="\033[0;32m"
25 CYAN="\033[0;36m"
26 RED="\033[0;31m"
27 PURPLE="\033[0;35m"
28 BROWN="\033[0;33m"
29 WHITE="\033[1;37m"
30 COLOR_RESET="\033[0m"
31
32 # Shortcuts bindings.
33 NEXT_KEY=$'\x6E' # n
34 PREV_KEY=$'\x70' # p
35 BEGIN_KEY=$'\x62' # b
36 END_KEY=$'\x65' # e
37 QUIT_KEY=$'\x71' # q
38 INVOKE_KEY=$'\x0' # enter
39
40 # Variables.
41 PRINT=()
42 CMDS=()
43 CLEAN_AFTER=()
44
45 # Strip ANSI escape codes/sequences [$1: input string, $2: target variable]
46 function strip_escape_codes_and_comments() {
47 local _input="$1" _i _j _token _escape=0
48 local -n _output="$2"; _output=""
49 for (( _i=0; _i < ${#_input}; _i++ )); do
50 if (( ${_escape} == 1 )); then
51 if [[ "${_input:_i:1}" =~ [a-zA-Z] ]]; then
52 _escape=0
53 fi
54 continue
55 fi
56 if [[ "${_input:_i:5}" == "\033[" ]]; then
57 _escape=1
58 continue
59 fi
60
61 if [[ "${_input:_i:1}" == '#' ]]; then
62 break
63 fi
64 _output+="${_input:_i:1}"
65 done
66 }
67
68 ##
69 # Registers a command into navigable script. Order of registration matters.
70 #
71 # Takes 1 or 2 parameters:
72 # 1) The string command to show.
73 # 2) Optionally: The string command to run. If empty, parameter 1 is used.
74 #
75 # usage:
76 #
77 # r "ls -l"
78 # r "list me please" "ls -l"
79 ##
80 function r() {
81 PRINT+=("${1}")
82
83 TO_RUN="${2:-${1}}"
84
85 # Sanitize.
86 strip_escape_codes_and_comments "${TO_RUN}" TO_RUN_SANITIZED
87 CMDS+=("${TO_RUN_SANITIZED}")
88 CLEAN_AFTER+=(false)
89 }
90
91 ##
92 # Same as 'r' but removes the command *AFTER* the execution.
93 ##
94 function rc() {
95 r "$1" "$2"
96
97 CLEAN_AFTER[-1]=true
98 }
99
100 ##
101 # Runs in a mode that enables easy navigation of the
102 # commands in the sequential manner.
103 #
104 # TODO(bwplotka): Add search (ctlr+r) functionality
105 ##
106 function navigate() {
107 CONTINUE=${1-false}
108
109 curr=0
110 if ${CONTINUE} && [[ -f ./.demo-last-step ]]; then
111 curr=$(< ./.demo-last-step)
112 fi
113
114 while true
115 do
116 # Check boundaries.
117 if (( ${curr} < 0 )); then
118 curr=0
119 fi
120 if (( ${curr} >= ${#CMDS[@]} )); then
121 let curr="${#CMDS[@]} - 1"
122 fi
123
124 print=${PRINT[${curr}]}
125 if [[ ${print} == "" ]]; then
126 print=${CMDS[${curr}]}
127 fi
128
129 prefix="${PREFIX}"
130 if ${NUMS}; then
131 prefix="${curr}) "
132 fi
133 # Make sure input will not break the print.
134 stty -echo
135 if [[ -z $TYPE_SPEED ]]; then
136 echo -en "${prefix}${YELLOW}$print${COLOR_RESET}"
137 else
138 echo -en "${prefix}${YELLOW}$print${COLOR_RESET}" | pv -qL $[$TYPE_SPEED+(-2 + RANDOM%5)];
139 fi
140 stty echo
141
142 # Ignore accidentally buffered input (introduces 0.5 input lag).
143 read -rst 0.3 -n 10000 discard
144
145 # Allow for interactive navigation.
146 read -rsn1 input
147 case "${input}" in
148 ${BEGIN_KEY})
149 # Skip this command and move to beginning.
150 curr=0
151 echo -en "\033[2K\r"
152 ;;
153 ${END_KEY})
154 # Skip this command and move to the end.
155 let curr="${#CMDS[@]} - 1"
156 echo -en "\033[2K\r"
157 ;;
158 ${NEXT_KEY})
159 # Skip this command and move to next.
160 ((curr++))
161 echo -en "\033[2K\r"
162 ;;
163 ${PREV_KEY})
164 # Skip this command and move to previous.
165 ((curr--))
166 echo -en "\033[2K\r"
167 ;;
168 ${INVOKE_KEY})
169 # enter - Eval this and move to next.
170 if ${CLEAN_AFTER[${curr}]}; then
171 echo -en "\033[2K\r"
172 else
173 echo ""
174 fi
175 eval "${CMDS[${curr}]}"
176 ((curr++))
177
178 if ${IMMEDIATE_REVEAL}; then
179 # Wait for enter at the end.
180 read -rst 0.3 -n 10000 discard
181 read -rsn1 input
182 case ${input} in
183 ${NEXT_KEY})
184 ((curr++))
185 echo -en "\033[2K\r"
186 ;;
187 ${PREV_KEY})
188 ((curr--))
189 echo -en "\033[2K\r"
190 ;;
191 ${QUIT_KEY})
192 echo ""
193 echo "Bye!"
194 exit 0
195 ;;
196 esac
197 fi
198 ;;
199 ${QUIT_KEY})
200 # q - Quit.
201 echo ""
202 echo "Bye!"
203 exit 0
204 ;;
205 *)
206 # Print again, not supported input.
207 echo -en "\r"
208 ;;
209 esac
210 echo ${curr} > ./.demo-last-step
211 done
212 }
213
214 # Internal function for checking pv tool that is used to simulate typing.
215 function _check_pv() {
216 command -v pv >/dev/null 2>&1 || {
217 echo ""
218 echo "'pv' tool is required, but it's not installed. Aborting." >&2;
219 echo ""
220 echo -e "${COLOR_RESET}Installing pv:"
221 echo ""
222 echo -e "${BLUE}Mac:${COLOR_RESET} $ brew install pv"
223 echo ""
224 echo -e "${BLUE}Other:${COLOR_RESET} http://www.ivarch.com/programs/pv.shtml"
225 echo -e "${COLOR_RESET}"
226 exit 1;
227 }
228 }
229
230 if ! [[ -z ${TYPE_SPEED} ]]; then
231 _check_pv
232 fi