-
Notifications
You must be signed in to change notification settings - Fork 542
/
show_progress
executable file
·66 lines (60 loc) · 2.46 KB
/
show_progress
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
#!/usr/bin/env bash
# show_progress -- Shows data flow progress by lines and bytes
# > show_progress input_to NAME [OPTION...] -- COMMAND [ARG...]
# > show_progress output_from NAME [OPTION...] -- COMMAND [ARG...]
#
# Recognized OPTIONs are:
# total_bytes=NUM_BYTES
# total_lines=NUM_LINES
# where NUM_BYTES and NUM_LINES are estimates and can be bash expressions,
# e.g., $(deepdive sql eval "SELECT COUNT(*) FROM ...").
##
set -euo pipefail
[[ $# -gt 0 ]] || usage "$0" "Missing input_to or output_from"
InOut=$1; shift
[[ $# -gt 0 ]] || usage "$0" "Missing NAME"
Name=$1; shift
[[ $# -gt 0 ]] || usage "$0" "Missing COMMAND"
# parse extra options until --
Options=()
while [[ $# -gt 0 && $1 != "--" ]]; do
Options+=("$1"); shift
done
[[ $1 = "--" ]] || usage "$0" "Missing -- before COMMAND"
shift
# map options to pv flags
PipeViewOpts=()
pv_format="%N %t %b %a (%r)"
[[ ${#Options[@]} -eq 0 ]] || declare -- "${Options[@]}"
: ${total_bytes:=} ${total_lines:=}
# if total size estimate is given show progress bar and ETA
pv_format_bytes=$pv_format; [[ -z $total_bytes ]] || pv_format_bytes+=" %p %e"
pv_format_lines=$pv_format; [[ -z $total_lines ]] || pv_format_lines+=" %p %e"
# the file descriptor to show the progress bar
: ${DEEPDIVE_PROGRESS_FD:=2}
if ! [[ -t $DEEPDIVE_PROGRESS_FD ]]; then
# just execute given COMMAND if the file descriptor is not a tty
exec "$@"
else
# add some default pv(1) options
PipeViewOpts+=(
${Name:+--name "$Name"}
--cursor
)
trap "stty echo <&'"$DEEPDIVE_PROGRESS_FD"' || true" EXIT # to restore sometimes garbled terminal
case $InOut in
input_to) eval '
pv 2>&'"$DEEPDIVE_PROGRESS_FD"' "${PipeViewOpts[@]}" --format="$pv_format_bytes" '"${total_bytes:+--size="$total_bytes"}"' |
pv 2>&'"$DEEPDIVE_PROGRESS_FD"' "${PipeViewOpts[@]}" --format="$pv_format_lines" '"${total_lines:+--size="$total_lines"}"' --line-mode |
"$@"
' ;;
output_from) eval '
"$@" |
pv 2>&'"$DEEPDIVE_PROGRESS_FD"' "${PipeViewOpts[@]}" --format="$pv_format_bytes" '"${total_bytes:+--size="$total_bytes"}"' |
pv 2>&'"$DEEPDIVE_PROGRESS_FD"' "${PipeViewOpts[@]}" --format="$pv_format_lines" '"${total_lines:+--size="$total_lines"}"' --line-mode
' ;;
*)
trap - EXIT # no need to mess around with the terminal
error "$InOut: Cannot show progress, only 'input_to' and 'output_from' are supported"
esac
fi