Advertisement
devinteske

dialog(1)/Xdialog(1) prototype for new bsdinstall distfetch

Jul 11th, 2013
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.67 KB | None | 0 0
  1. #!/bin/sh
  2. # Copyright (c)2013 Devin Teske. All rights reserved.
  3. increment=1   # How much to increment % each time (must be even divisor of 100)
  4. sleep_sec=    # Time to sleep in-between increments
  5. pbar_size=17  # How big is the mini-progressbar?
  6. txt_size=28   # Maximum width for labels (file names)
  7. files="base.txz dict.txz doc.txz games.txz ports.txz" # Files to fake-fetch
  8. ### Rest below shouldn't need editing ###
  9. if [ "$1" = "-X" ]; then
  10.     DIALOG=Xdialog USE_XDIALOG=1 USE_DIALOG= USE_COLOR=
  11. else
  12.     DIALOG=dialog USE_DIALOG=1
  13.     : ${USE_COLOR=1}
  14. fi
  15. spin="/-\|"
  16. pct_lsize=$(( ( $pbar_size - 4 ) / 2 ))
  17. pct_rsize=$pct_lsize
  18. [ $(( $pct_lsize * 2 + 4 )) -ne $pbar_size ] &&
  19.     pct_rsize=$(( $pct_rsize + 1 ))
  20. dun_lsize=$pct_lsize dun_rsize=$pct_rsize
  21. pen_lsize=$(( ( $pbar_size - 7 ) / 2 ))
  22. pen_rsize=$pen_lsize
  23. [ $(( $pen_lsize * 2 + 7 )) -ne $pbar_size ] &&
  24.     pen_rsize=$(( $pen_rsize + 1 ))
  25. n=
  26. nfiles=$( set -- $files; echo $# )
  27. HEIGHT=$(( $nfiles + 4 + 5 )) WIDTH=$(( $txt_size + $pbar_size + 9 ))
  28. [ "$USE_XDIALOG" ] && HEIGHT=$(( $HEIGHT + $HEIGHT / 4 ))
  29. while [ ${n:-0} -lt $nfiles ]; do
  30.     n=$(( ${n:-0} + 1 ))
  31.     pct=
  32.     while [ ${pct:-0} -lt 100 ]; do # ... incrementing 1% each sub-loop
  33.         [ "$sleep_sec" ] && sleep $sleep_sec
  34.         pct=$(( ${pct:--$increment} + $increment ))
  35.         #
  36.         # Create the progress bar
  37.         #
  38.         if [ "$USE_XDIALOG" ]; then
  39.             pbar=$( printf "%${pct_lsize}s%3u%%%%%${pct_rsize}s" \
  40.                            "" $pct "" )
  41.         else
  42.             pbar=$( printf "%${pct_lsize}s%3u%%%${pct_rsize}s" \
  43.                            "" $pct "" )
  44.         fi
  45.         if [ "$USE_DIALOG" ]; then
  46.             # Calculate the fill-width of the progress bar
  47.             width=$(( $pct * $pbar_size / 100 ))
  48.             # Round up based on one-tenth of a percent
  49.             [ $(( $pct * $pbar_size % 100 )) -gt 50 ] &&
  50.                 width=$(( $width + 1 ))
  51.             #
  52.             # Make a copy of the pbar and split the copy into two
  53.             # halves (we'll insert the ANSI delimiter in between)
  54.             #
  55.             lpbar="$pbar" rpbar="$pbar"
  56.             rwidth=$(( $pbar_size - $width ))
  57.             while [ ${#lpbar} -gt $width ]
  58.             do lpbar="${lpbar%?}"; done
  59.             while [ ${#rpbar} -gt $rwidth ]
  60.             do rpbar="${rpbar#?}"; done
  61.             #
  62.             # Finalize the mini progress bar
  63.             #
  64.             pbar=
  65.             [ "$USE_COLOR" ] && pbar="$pbar\Zb\Zr\Z4"
  66.             pbar="$pbar$lpbar"
  67.             [ "$USE_COLOR" ] && pbar="$pbar\ZR"
  68.             pbar="$pbar$rpbar"
  69.             [ "$USE_COLOR" ] && pbar="$pbar\Zn"
  70.         fi
  71.         #
  72.         # Build the prompt text
  73.         #
  74.         p=1 prompt=
  75.         for f in $files; do
  76.             flabel="$f"
  77.             while [ ${#flabel} -gt $txt_size ]; do
  78.                 flabel="${flabel%?}"
  79.             done
  80.             if [ ${#flabel} -ne ${#f} ]; then
  81.                 while [ ${#flabel} -gt $(( $txt_size - 3 )) ]
  82.                 do flabel="${flabel%?}"; done
  83.                 flabel="$flabel..."
  84.             fi
  85.             if [ $n -eq $p -a $pct -ne 100 ]; then
  86.                 # This is the file we're processing right now
  87.                 while [ ${#flabel} -gt $(( $txt_size - 3 )) ]
  88.                 do flabel="${flabel%?}"; done
  89.                 spin_char=$( echo "$spin" | sed -e "
  90.                     s/.\{0,$(( $pct % ${#spin} ))\}\(.\).*/\1/
  91.                     ${USE_XDIALOG:+s/\\\\/\\\\&/g}
  92.                 " )
  93.                 prompt="$prompt$(
  94.                     printf "%s%-${txt_size}s%s %s" \
  95.                            "${USE_COLOR:+\Zb}" \
  96.                            "${flabel%...}..." \
  97.                            "${USE_COLOR:+\ZB}" \
  98.                            "$spin_char"
  99.                 )"
  100.             else
  101.                 prompt="$prompt$(
  102.                     printf "%-${txt_size}s %s" \
  103.                            "$flabel" " "
  104.                 )"
  105.             fi
  106.             if [ $p -eq $n -a $pct -ne 100 ]; then
  107.                 prompt="$prompt [$pbar]\n"
  108.             elif [ $p -gt $n ]; then
  109.                 prompt="$prompt [$(
  110.                     printf "%${pen_lsize}s" ""
  111.                 )Pending$(
  112.                     printf "%${pen_rsize}s" ""
  113.                 )]\n"
  114.             else
  115.                 prompt="$prompt [$(
  116.                     printf "%s%${dun_lsize}s" \
  117.                            "${USE_COLOR:+\Zb\Zr\Z2}" ""
  118.                 )Done$(
  119.                     printf "%${dun_rsize}s%s" "" \
  120.                            "${USE_COLOR:+\Zn}"
  121.                 )]\n"
  122.             fi
  123.             p=$(( $p + 1 ))
  124.         done
  125.         #
  126.         # Add trailing information
  127.         #
  128.         prompt="$prompt\nFetching distribution files...\n"
  129.         prompt="$prompt\n  ${USE_COLOR:+\Zb}Overall Progress:"
  130.         [ "$USE_COLOR" ] && prompt="$prompt\Zn"
  131.         #
  132.         # Xdialog(1) requires newlines to be:
  133.         # (a) escaped and (b) on a line by themselves.
  134.         #
  135.         [ "$USE_XDIALOG" ] &&
  136.             prompt=$( echo "$prompt" | sed -e 's/\\n/&\\&&/g' )
  137.         #
  138.         # Calculate total overall progress
  139.         #
  140.         opct=$(( ( 100 * $n - 100 + $pct ) / $nfiles ))
  141.         # Round up based on one-tenth of a percent
  142.         [ $(( (100*$n - 100 + $pct) * 10 / $nfiles % 100 )) -gt 50 ] &&
  143.             opct=$(( $opct + 1 ))
  144.  
  145.         if [ "$USE_XDIALOG" ]; then
  146.             printf "XXX\n$prompt\nXXX\n%u\n" $opct
  147.         else
  148.             printf "XXX\n%s\nXXX\n%u\n" "$prompt" $opct
  149.         fi
  150.     done
  151. done | env ${USE_XDIALOG:+XDIALOG_HIGH_DIALOG_COMPAT=1} $DIALOG \
  152.     --title 'Fetching Distribution' \
  153.     --backtitle 'FreeBSD Installer' \
  154.     ${USE_COLOR:+--colors} ${USE_XDIALOG:+--left} \
  155.     --gauge "" $HEIGHT $WIDTH
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement