Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Copyright (c)2013 Devin Teske. All rights reserved.
- increment=1 # How much to increment % each time (must be even divisor of 100)
- sleep_sec= # Time to sleep in-between increments
- pbar_size=17 # How big is the mini-progressbar?
- txt_size=28 # Maximum width for labels (file names)
- files="base.txz dict.txz doc.txz games.txz ports.txz" # Files to fake-fetch
- ### Rest below shouldn't need editing ###
- if [ "$1" = "-X" ]; then
- DIALOG=Xdialog USE_XDIALOG=1 USE_DIALOG= USE_COLOR=
- else
- DIALOG=dialog USE_DIALOG=1
- : ${USE_COLOR=1}
- fi
- spin="/-\|"
- pct_lsize=$(( ( $pbar_size - 4 ) / 2 ))
- pct_rsize=$pct_lsize
- [ $(( $pct_lsize * 2 + 4 )) -ne $pbar_size ] &&
- pct_rsize=$(( $pct_rsize + 1 ))
- dun_lsize=$pct_lsize dun_rsize=$pct_rsize
- pen_lsize=$(( ( $pbar_size - 7 ) / 2 ))
- pen_rsize=$pen_lsize
- [ $(( $pen_lsize * 2 + 7 )) -ne $pbar_size ] &&
- pen_rsize=$(( $pen_rsize + 1 ))
- n=
- nfiles=$( set -- $files; echo $# )
- HEIGHT=$(( $nfiles + 4 + 5 )) WIDTH=$(( $txt_size + $pbar_size + 9 ))
- [ "$USE_XDIALOG" ] && HEIGHT=$(( $HEIGHT + $HEIGHT / 4 ))
- while [ ${n:-0} -lt $nfiles ]; do
- n=$(( ${n:-0} + 1 ))
- pct=
- while [ ${pct:-0} -lt 100 ]; do # ... incrementing 1% each sub-loop
- [ "$sleep_sec" ] && sleep $sleep_sec
- pct=$(( ${pct:--$increment} + $increment ))
- #
- # Create the progress bar
- #
- if [ "$USE_XDIALOG" ]; then
- pbar=$( printf "%${pct_lsize}s%3u%%%%%${pct_rsize}s" \
- "" $pct "" )
- else
- pbar=$( printf "%${pct_lsize}s%3u%%%${pct_rsize}s" \
- "" $pct "" )
- fi
- if [ "$USE_DIALOG" ]; then
- # Calculate the fill-width of the progress bar
- width=$(( $pct * $pbar_size / 100 ))
- # Round up based on one-tenth of a percent
- [ $(( $pct * $pbar_size % 100 )) -gt 50 ] &&
- width=$(( $width + 1 ))
- #
- # Make a copy of the pbar and split the copy into two
- # halves (we'll insert the ANSI delimiter in between)
- #
- lpbar="$pbar" rpbar="$pbar"
- rwidth=$(( $pbar_size - $width ))
- while [ ${#lpbar} -gt $width ]
- do lpbar="${lpbar%?}"; done
- while [ ${#rpbar} -gt $rwidth ]
- do rpbar="${rpbar#?}"; done
- #
- # Finalize the mini progress bar
- #
- pbar=
- [ "$USE_COLOR" ] && pbar="$pbar\Zb\Zr\Z4"
- pbar="$pbar$lpbar"
- [ "$USE_COLOR" ] && pbar="$pbar\ZR"
- pbar="$pbar$rpbar"
- [ "$USE_COLOR" ] && pbar="$pbar\Zn"
- fi
- #
- # Build the prompt text
- #
- p=1 prompt=
- for f in $files; do
- flabel="$f"
- while [ ${#flabel} -gt $txt_size ]; do
- flabel="${flabel%?}"
- done
- if [ ${#flabel} -ne ${#f} ]; then
- while [ ${#flabel} -gt $(( $txt_size - 3 )) ]
- do flabel="${flabel%?}"; done
- flabel="$flabel..."
- fi
- if [ $n -eq $p -a $pct -ne 100 ]; then
- # This is the file we're processing right now
- while [ ${#flabel} -gt $(( $txt_size - 3 )) ]
- do flabel="${flabel%?}"; done
- spin_char=$( echo "$spin" | sed -e "
- s/.\{0,$(( $pct % ${#spin} ))\}\(.\).*/\1/
- ${USE_XDIALOG:+s/\\\\/\\\\&/g}
- " )
- prompt="$prompt$(
- printf "%s%-${txt_size}s%s %s" \
- "${USE_COLOR:+\Zb}" \
- "${flabel%...}..." \
- "${USE_COLOR:+\ZB}" \
- "$spin_char"
- )"
- else
- prompt="$prompt$(
- printf "%-${txt_size}s %s" \
- "$flabel" " "
- )"
- fi
- if [ $p -eq $n -a $pct -ne 100 ]; then
- prompt="$prompt [$pbar]\n"
- elif [ $p -gt $n ]; then
- prompt="$prompt [$(
- printf "%${pen_lsize}s" ""
- )Pending$(
- printf "%${pen_rsize}s" ""
- )]\n"
- else
- prompt="$prompt [$(
- printf "%s%${dun_lsize}s" \
- "${USE_COLOR:+\Zb\Zr\Z2}" ""
- )Done$(
- printf "%${dun_rsize}s%s" "" \
- "${USE_COLOR:+\Zn}"
- )]\n"
- fi
- p=$(( $p + 1 ))
- done
- #
- # Add trailing information
- #
- prompt="$prompt\nFetching distribution files...\n"
- prompt="$prompt\n ${USE_COLOR:+\Zb}Overall Progress:"
- [ "$USE_COLOR" ] && prompt="$prompt\Zn"
- #
- # Xdialog(1) requires newlines to be:
- # (a) escaped and (b) on a line by themselves.
- #
- [ "$USE_XDIALOG" ] &&
- prompt=$( echo "$prompt" | sed -e 's/\\n/&\\&&/g' )
- #
- # Calculate total overall progress
- #
- opct=$(( ( 100 * $n - 100 + $pct ) / $nfiles ))
- # Round up based on one-tenth of a percent
- [ $(( (100*$n - 100 + $pct) * 10 / $nfiles % 100 )) -gt 50 ] &&
- opct=$(( $opct + 1 ))
- if [ "$USE_XDIALOG" ]; then
- printf "XXX\n$prompt\nXXX\n%u\n" $opct
- else
- printf "XXX\n%s\nXXX\n%u\n" "$prompt" $opct
- fi
- done
- done | env ${USE_XDIALOG:+XDIALOG_HIGH_DIALOG_COMPAT=1} $DIALOG \
- --title 'Fetching Distribution' \
- --backtitle 'FreeBSD Installer' \
- ${USE_COLOR:+--colors} ${USE_XDIALOG:+--left} \
- --gauge "" $HEIGHT $WIDTH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement