summarylogtreecommitdiffstats
path: root/pbar
blob: 1b25005ccbfd4b12d13d00616a857863c14f7611 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
# Copyright Matthew Bruenig <matthewbruenig@gmail.com> (packer)
#           Moritz Lüdecke <ritze@skweez.net>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

DASH="-"
HASH="#"
BEGINBAR="["
ENDBAR="]"
INFBAR_UPDATE="0.1"

usage() {
	echo 'usage: pbar [OPTION] [-t DEVICE] DONE TOTAL [TITLE] [DETAILS]...'
	echo
	echo '  -i    infinite progressbar,'
	echo '  -n    outputs a new line at the end'
	echo '  -t    set the tty device'
	echo '  -h    display this help and exit'
	echo
	echo 'Examples:'
	echo '  pbar -n 3 5 "testing..." 3 of 5   Print the follow line:'
	echo 'testing...                      3 of 5 [##########--------]  60%'
	echo '  pbar -i 0 0 "testing..."          Print a infinite working progressbar:'
	echo 'testing...                             [------###---------]'
	exit
}

screenwidth() {
	tty=$1
	width="$(stty -F $tty size)"
	echo "${width##* }"
}

removeformat() {
	echo "$(expr "$1" | sed -r "s/\\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")"
}

info() {
	width=$1
	title=$2
	misc=$3
	
	# Get vars for output
	beginline=$title
	misclen=0
	# Remove color code
	if [[ $misc != "" ]]; then
		misctext=$(removeformat "$misc")
		misclen=$((1+${#misctext}))
	fi
	beginlinetext=$(removeformat "$beginline")
	if [[ $width -lt $((${#beginlinetext}+2+$misclen-1)) ]]; then
		# Remove color code, cut the text and print it without color code
		beginlinetext=${beginlinetext::$((width-$misclen-4))}
		beginline=$beginlinetext"..."
		beginlinetext=$(removeformat "$beginline")
	fi
	spaces="$(($width-${#beginlinetext}-1-$misclen))"

	# Print output
	printf "$beginline %${spaces}s"
	[[ $misc != "" ]] && printf " $misc"
}

drawsymbol() {
	symbol=$1
	num=$2

	for ((n=0; n<$num; n++)); do
		printf "$symbol"
	done
}

# This method is a fork from the packer method aurbar
progbar() {
	width=$1
	infinite=$2
	done=$3
	total=$4

	# Get vars for output
	barchars="$(($width-${#BEGINBAR}-${#ENDBAR}-6))"
	hashes="$(($barchars*$done/$total))"
	dashes="$(($barchars-$hashes))"

	# Print output
	printf " ${BEGINBAR}"

	if [[ $infinite ]]; then
		offset="$(($hashes-2))"
		hashes=3
		[[ $offset -lt 0 ]] && offset=0 && hashes=2
		drawsymbol $DASH $offset

		offset="$(($offset+$hashes))"
		[[ $offset -gt $barchars ]] && hashes=2
		drawsymbol $HASH $hashes

		dashes="$(($barchars-$offset))"
		drawsymbol $DASH $dashes
		printf "%s     \r" ${ENDBAR}
	else
		drawsymbol $HASH $hashes
		drawsymbol $DASH $dashes
		perc="$(($done*100/$total))"
		printf "%s%4s%%\r" ${ENDBAR} ${perc}
	fi
}

printline() {
	tty=$1
	infinite=$2
	done=$3
	total=$4
	title=$5
	misc=$6

	width=$(screenwidth $tty)
	infolen="$(($width*6/10))"
	[ $width -lt 50 ] && width=50
	progbarlen="$(($width-$infolen))"

	if [[ $total -eq 0 ]]; then
		total=$(($progbarlen-9))

		newdone="$(($done % $total))"
		if [[ $(($done % $((2*$total)))) -ge $total ]]; then
			newdone="$(($total-$newdone))"
		fi	
		done=$(($newdone+1))
		total=$(($total+1))
	fi

	# Delete line
	printf "\033[0G"

	info $infolen "$title" "$misc"
	progbar $progbarlen "$infinite" $done $total
}

# Argument parsing

[[ $1 ]] || usage

while getopts ":hint:" optname; do
	case "$optname" in
		"i")
			infinite=1
			;;
		"n")
			newline=1
			;;
		"t")
			tty=$OPTARG
			;;
		"h")
			usage
			;;
		"?")
			echo "Unknown option $OPTARG"
			usage
			;;
		":")
			echo "No argument value for option $OPTARG"
			;;
		*)
			echo "Unknown error while processing options"
			;;
		esac
done

done=${@:$OPTIND:1}
total=${@:$OPTIND+1:1}
title="${@:$OPTIND+2:1}"
misc="${@:$OPTIND+3}"

int='^[0-9]+$'

if [[ $tty == "" ]]; then
	tty=$(tty)
	if [[ $? -ne 0 ]]; then
		echo "Can't get tty device!"
		usage
	fi
fi

if [[ $total -eq "" ]] ||
	! [[ $done =~ $int ]] || ! [[ $total =~ $int ]]; then
	
	if [[ $done -ne 0 ]] || [[ $total -ne 0 ]] || [[ $infinite -ne 1 ]]; then
		usage
	fi
fi

if [[ $done -gt $total ]]; then
	done=$total
fi

if [[ $done -eq 0 ]] && [[ $total -eq 0 ]] && [[ $infinite -eq 1 ]]; then
	trap "exit=true" SIGINT SIGTERM
	i=0
	while [ ! $exit ]; do
		printline $tty "$infinite" $i 0 "$title" "$misc"
		i="$(($i+1))"
		sleep $INFBAR_UPDATE
	done

	# Delete line
	printf "\033[0G"

	[ $newline ] && printline $tty "" 1 1 "$title" "$misc" 
else
	printline $tty "$infinite" $done $total "$title" "$misc"
fi

[ $newline ] && echo 

exit