Repository navigation
Expand file tree
/
Copy pathprogressbar
More file actions
executable file
·69 lines (60 loc) · 1.5 KB
/
Copy pathprogressbar
File metadata and controls
executable file
·69 lines (60 loc) · 1.5 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
#!/bin/bash
# written by Shotaro Fujimoto (https://github.com/ssh0)
# first edited: 2015-12-03
#=#=#=
# progressbar - make progress bar easily
#
# **Usage:**
#
# ```
# progressbar n [ barchar_empty barchar_fill ]
# ```
#
# (int `n`: 0 ~ 100)
#
# > [bash - How to add a progress bar to a shell script? - Stack Overflow](http://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script)
#=#=
if [ "$1" = '-h' ]; then
usage_all "$0"
exit 0
fi
progressbar() {
# Usage:
# progressbar progress(int: 0 ~ 100) [ barchar_empty barchar_fill ]
barchar_empty="${2:--}"
barchar_fill="${3:-#}"
# TODO: バー以外の文字数をautodetect
columns=$(( $(tput cols) - 18 ))
if [[ ${columns} -gt 100 ]]; then
columns=100
fi
# Process data
_done=$(( ($1 * ${columns}) / 100 ))
_left=$(( ${columns} - ${_done} ))
# Build progressbar string lengths
_fill=$(printf "%${_done}s")
_empty=$(printf "%${_left}s")
# Generate progressbar
printf "\rProgress : [${_fill// /"$barchar_fill"}${_empty// /"$barchar_empty"}] ${1}%%"
}
progressbar $@
# EXAMPLE:
#
# progressbar_test(){
# # Variables
# varstart=1
#
# # This accounts as the "totalState" variable for the ProgressBar function
# varend=99
#
# # Proof of concept
# for number in $(seq ${varstart} ${varend})
# do
# sleep 0.01
# # progress (int: 0 ~ 100)
# progress=$(( (${number}*100/${varend}*100) / 100 ))
# progressbar ${progress} "_" "█"
# done
# printf '\nFinished!\n'
# }
# . progressbar_test