This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
/
progress.sh
executable file
·145 lines (136 loc) · 4.21 KB
/
progress.sh
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
133
134
135
136
137
138
139
140
141
142
143
144
145
#! usr/bin/env bash
function _eraseLine() {
printf "\\r%${progress_line_length}s"
}
function _printMessageWithRunner() {
printf "\\r%0s%s%s%${time_padding}s\\e[33m" '' "$progress_message" "$accumulated_runner" ''
}
function _show_longrunner() {
local pad_right=$progress_pad_left
local pos=1
while :; do
printf "\\r%${progress_pad_left}s%s%${pos}s%s" '' "$progress_message" '' "$progress_runner"
pos=$((pos + 1))
sleep "$progress_speed"
if [[ "$pos" == "$pad_right" ]]; then
pos=0
_eraseLine
fi
done
}
function _show() {
local accumulated_runner pad_right right_offset time_padding i
accumulated_runner=$progress_runner
pad_right=$progress_pad_left
right_offset=$((progress_line_length / 10))
i=0
SECONDS=0
while :; do
time_padding=$((progress_line_length - ${#progress_message} - ${#accumulated_runner} - 5 - right_offset))
_printMessageWithRunner
_print_time $SECONDS
printf "\\e[0m"
i=$((i + 1))
accumulated_runner=$accumulated_runner$progress_runner
sleep "$progress_speed"
if [[ "$i" == "$((pad_right / 2))" ]]; then
i=0
accumulated_runner=$progress_runner
_eraseLine
fi
done
}
function _print() {
local time_padding=$((progress_line_length - progress_pad_left - ${#progress_message} - ${#accumulated_runner} - 5))
_printMessageWithRunner
printf " "
printf "\\e[0m"
}
function progress() {
case "$1" in
"start")
if [[ "$progress_pid" ]]; then
printf "\\r%2s\\e[31m✘ %b\\n" '' "Progress is already running. Kill it with \\e[100;37mkill $progress_pid\\e[0m"
exit 1
fi
shift
progress_speed=0.075 #0.050 good for _show_longrunner
progress_runner="."
progress_message="$*"
progress_line_length=$(tput cols)
progress_pad_left=$(((progress_line_length - ${#progress_message} - ${#progress_runner}) / 2))
_show &
#removes last job put to bg from shell list. Now when killed, it says nothing to stdout.
disown
progress_pid="$!"
;;
"finish")
# hard to find a good value here. ${#accumulated_runner} can't be referenced because _show is called with &
local time_padding=$(((progress_pad_left / 2) + ${#progress_message} + 4)) # +4: the emoji (3) + 1 space
# echo time_padding $time_padding >foo.txt
# echo progress_line_length "$progress_line_length" >>foo.txt
# echo progress_pad_left $progress_pad_left >>foo.txt
# echo progress_message ${#progress_message} >>foo.txt
printf "\\r%${time_padding}s" '' #erases line from beginning to the clock
if [ "${#progress_pid}" == "0" ]; then
true # noop
else
kill "$progress_pid"
fi
progress_pad_left=$((progress_pad_left - 2))
case "$2" in
# will reprint the message with color indicating the status
"0") #success
printf "\\r%0s\\e[32m✔ %s\\e[0m\\n" '' "$progress_message"
;;
"1") #failure
printf "\\r%0s\\e[31m✘ %s\\e[0m\\n" '' "$progress_message"
;;
"*") #other status, or no status passed
printf "\\r%0s\\e[33m● %s\\e[0m\\n" '' "$progress_message"
;;
esac
unset progress_speed progress_runner progress_message progress_pid progress_line_length progress_pad_left
;;
"total")
shift
progress_message="Total "
local progress_message2=''
if [[ -f "$1" ]]; then
_print_time "$SECONDS" >>"$1"
progress_message="Total✝ "
fi
progress_line_length=$(tput cols)
progress_pad_left=$((progress_line_length - ${#progress_message} - 5))
printf "%0s\\e[33;1m%s%s%s\\e[0m\\n" '' "$progress_message" "$(_print_time $SECONDS)" "$progress_message2"
;;
"print")
shift
progress_message="$*"
progress_line_length=$(tput cols)
progress_pad_left=$(((progress_line_length - ${#progress_message} - ${#progress_runner}) / 2))
_print
;;
"error")
local last="$?"
if [ "$last" == 0 ]; then
last=1
fi
shift
progress finish "$last"
;;
"die")
progress error
[[ ! "$-" =~ i ]] && exit 1
return 1
;;
*)
echo "Please give a valid command"
echo "start, finish, total or print"
;;
esac
}
function _print_time() { #$1 - a number in seconds
if [[ "$#" == 0 ]]; then exit 1; fi
printf "%02d:%02d" "$(($1 / 60))" "$(($1 % 60))"
}