-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown
More file actions
executable file
·133 lines (116 loc) · 3.05 KB
/
Copy pathcountdown
File metadata and controls
executable file
·133 lines (116 loc) · 3.05 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env bash
countdown_usage() {
cat <<EOusage | mdformat
**NAME**
${0##*/} - sleep with noise
**SYNOPSIS**
${0##*/} -h
${0##*/} [**-iq**] _s_
**DESCRIPTION**
Counts down from the given number, 1 second at a time. If **-q** is
specified, do so quietly, which is functionally like _sleep_.
If **-i** (interactive) is specified, then the countdown may be cut
short with user input producing no error. _s_ is the number of
seconds to count down, an integer.
In interactive (**-i**) mode, the following commands work:
**r** or **+** Rewind ${COUNTDOWN_JUMP:-10} seconds
**a** or **-** Advance ${COUNTDOWN_JUMP:-10} seconds
**q** Quit (return success)
**x** Quit (return fail)
**p** Pause (any key to continue)
**ENVIRONMENT**
Some variables affect the script's behaviour.
**COUNTDOWN_JUMP**
the interval added/removed in seconds
(default 10 if unset)
**COUNTDOWN_OPTIONS**
default options if none are specified
(command line overrides anything specified)
EOusage
}
mdformat() {
local md so us mr me
if [ -t 1 ]; then
md=$(tput md) # bold
so=$(tput so) # standout (inverse?)
us=$(tput us) # underline
mr=$(tput mr) # reverse
me=$(tput me) # exit attributes
fi
sed -E \
-e "s/\*\*([^*]+)\*\*/$md\1$me/g" \
-e "s/\*([^*]+)\*/$us\1$me/g" \
-e "s/_([^_]+)_/$us\1$me/g" \
-e "s/^#+ *(.+)/$so\1$me/g" \
-e "s/#([^#]+)#/$mr\1$me/g"
}
countdown_opts() {
declare -g -i countdown_s
declare -g countdown_opti countdown_optq
declare -g countdown_debug countdown_time
countdown_opti=false
countdown_optq=false
countdown_debug=false
countdown_time=""
while getopts diqh opt; do
case "$opt" in
h) countdown_usage; exit 0 ;;
q) countdown_optq=true ;;
i) countdown_opti=true ;;
d) countdown_debug=true; countdown_time=time ;;
j) COUNTDOWN_JUMP="$OPTARG" ;;
*) countdown_usage; exit 64 ;;
esac
done
shift $((OPTIND - 1))
if [[ -z "$1" ]]; then
exit 1
fi
countdown_s="$1"
$countdown_debug && echo "Debug on"
}
countdown() {
countdown_opts "$@"
local count
local what
local start
local pause_start
local el
printf -v start '%(%s)T'
el=$(tput el)
count="$countdown_s"
while [[ $count -ge 0 ]]; do
case "$what" in
r|+|=) (( count += ${COUNTDOWN_JUMP:-10} )) ;;
a|-|_) (( count -= ${COUNTDOWN_JUMP:-10} )) ;;
q) break ;;
x) echo; exit 1 ;;
p)
printf "\r%d ... ${el}" "$count"
printf -v pause_start '%(%s)T'
read -n 1 -p "PAUSED - press any key to continue"
printf -v pause_time '%(%s)T'
(( pause_time -= pause_start ))
(( start += pause_time ))
;;
esac
$countdown_optq || printf "\r%d ... ${el}" "$count"
if $countdown_opti; then
$countdown_optq || printf "Rewind/Advance/Quit/Exit/Pause ... " >&2
$countdown_time read -t 1 -n 1 what
if [[ $(date '+%s') -gt $(($start + $countdown_s)) ]]; then
$countdown_optq || printf "\r%d ... TIME${el}" "$count"
break
fi
else
sleep 1
fi
(( count-- ))
done
$countdown_optq || printf "\r%d ... Done!${el}\n" "$count"
}
# only execute if we're called directly
case "$-" in
*i*) : ;;
*) countdown "$@" ;;
esac