aboutsummaryrefslogtreecommitdiff
path: root/.scripts/progress
blob: 052c6c2b10f3a3ebad92ebc6cc39b3c1a0f74d56 (plain) (blame)
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
#!/bin/env bash

set -e

percent() {
	local p=00$(($1 * 100000 / $2))
	printf -v "$3" %.3f ${p::-3}.${p: -3}
}

percent "$1" 33000 refresh

if [ -n "$2" ]; then
	pid=$(pgrep "$2")
fi

percentBar() {
	local prct totlen=$((8 * $2)) lastchar barstring blankstring
	printf -v prct %.2f "$1"
	((prct = 10#${prct/./} * totlen / 10000, prct % 8)) &&
		printf -v lastchar '\\U258%X' $((16 - prct % 8)) ||
		lastchar=''
	printf -v barstring '%*s' $((prct / 8)) ''
	printf -v barstring '%b' "${barstring// /\\U2588}$lastchar"
	printf -v blankstring '%*s' $(((totlen - prct) / 8)) ''
	printf -v "$3" '%s%s' "$barstring" "$blankstring"
}

clear
for i in {0..10000..33} 10000; do
	if [ -n "$2" ]; then
		if [ ! -d "/proc/$pid" ]; then
			percentBar 100 $((COLUMNS - 9)) bar
			printf '\r|%s|%6.2f%%' "$bar" 100
			exit 0
		fi
	fi

	i=0$i
	printf -v p %0.2f ${i::-2}.${i: -2}
	percentBar $p $((COLUMNS - 9)) bar
	printf '\r|%s|%6.2f%%' "$bar" $p
	read -srt $refresh _ && break # console sleep avoiding fork
done

while true; do
	if [ -n "$2" ]; then
		if [ ! -d "/proc/$pid" ]; then
			break
		fi
	fi
done

exit 0