summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2022-10-02 15:28:12 +0000
committerdavidovski <david@davidovski.xyz>2023-05-17 16:50:26 +0000
commit797b98a68976b4bb183292eacfa8a2636b82c00e (patch)
tree931d8a2b9940dee5f7bfb8b9540e0260c6182cd0
parentef46bc163713c6da7693c85368dc0c23ed4b4ed4 (diff)
Added hbar as sh
-rwxr-xr-xsrc/util/hbar.sh95
-rwxr-xr-xtest/hbar.sh10
2 files changed, 100 insertions, 5 deletions
diff --git a/src/util/hbar.sh b/src/util/hbar.sh
new file mode 100755
index 0000000..25343bd
--- /dev/null
+++ b/src/util/hbar.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+. /usr/lib/colors.sh
+
+move_up () {
+ [ ! "$1" = "-1" ] &&
+ printf "\033[%dA" "$1" "0"
+}
+
+move_down () {
+ [ ! "$1" = "-1" ] &&
+ printf "\033[%dA" "$1"
+}
+
+count_string () {
+ local c t
+ $human && {
+ c=$(format_bytes $completed)
+ t=$(format_bytes $total)
+ } || {
+ c=$1
+ t=$2
+ }
+ printf "[%s%s/%s%s]" $c $unit $t $unit
+}
+
+hbar () {
+ local width terminate human text color reset unit line count
+ width=$(tput cols)
+ color=$BG_BLUE
+ reset=$BG_DEFAULT
+ terminate=false
+ human=false
+ line=0
+
+ while getopts ":T:c:r:u:l:ht" opt; do
+ case "$opt" in
+ T)
+ text="$OPTARG"
+ ;;
+ c)
+ color="$OPTARG"
+ ;;
+ r)
+ reset="$OPTARG"
+ ;;
+ u)
+ unit="$OPTARG"
+ ;;
+ t)
+ terminate=true
+ ;;
+ h)
+ human=true
+ ;;
+ l)
+ line="$OPTARG"
+ ;;
+ esac
+ done
+ shift $((OPTIND-1))
+
+ [ "$#" -lt 2 ] && {
+ printf "$RESET\n"
+ exit 1
+ }
+
+ completed="$1"
+ total="$2"
+
+ move_up $line
+
+ count=$(count_string $completed $total)
+ printf "\r$text"
+ printf "$RESET\r"
+ printf "$color"
+
+ reset_at=0
+ [ "$total" -gt "0" ] && reset_at=$(((completed*width)/total))
+
+ i=0
+ while [ "$i" -lt "$width" ]; do
+ [ "$i" = "$reset_at" ] && printf "$reset"
+
+ printf " "
+ i=$((i+1))
+ done
+
+ move_down $line
+
+ $terminate && printf "$RESET\n"
+
+ exit 0
+}
+
+hbar $@
diff --git a/test/hbar.sh b/test/hbar.sh
index 71713ef..4a124f2 100755
--- a/test/hbar.sh
+++ b/test/hbar.sh
@@ -1,13 +1,13 @@
#!/bin/sh
-HBAR=./bin/hbar
+HBAR=./dist/hbar
UNIT="mb"
MAX=100
TEXT="✓ Привет World Привет World Привет World Привет World Привет World Привет World "
for x in $(seq $MAX); do
${HBAR} -T "${TEXT}" -u ${UNIT} $x $MAX
- sleep 0.01
+ #sleep 0.01
done
TEXT="Hello there"
@@ -15,7 +15,7 @@ TEXT="Hello there"
${HBAR} -t -T "${TEXT}" -u ${UNIT} $x $MAX
for x in $(seq $MAX); do
${HBAR} -T "${TEXT}" -u ${UNIT} $x $MAX
- sleep 0.01
+ #sleep 0.01
done
${HBAR} -t -T "${TEXT}" -u ${UNIT} $x $MAX
@@ -23,7 +23,7 @@ hbar
for x in $(seq $MAX); do
${HBAR} -l 0 -T "${TEXT}" $((MAX - x)) $MAX
${HBAR} -l 1 -T "${TEXT}" $x $MAX
- sleep 0.01
+ #sleep 0.01
done
${HBAR} -l 1 -T "${TEXT}" $x $MAX
${HBAR} -l 0 -t -T "${TEXT}" $((MAX-x)) $MAX
@@ -32,6 +32,6 @@ MAX=20000000
for x in $(seq 0 991 $MAX); do
${HBAR} -h -T "${TEXT}" $x $MAX
- sleep 0.01
+ #sleep 0.01
done
${HBAR} -ht -T "${TEXT}" $x $MAX