On 08/24/2017 06:02 AM, Fabian Groffen wrote:
> Prior to this commit, the reported sizes would look like:
> 
>  * Final size of build directory: 34942 KiB
>  * Final size of installed tree: 5627 KiB
> 
> Because the sizes aren't aligned, it is hard to (visually) compare them.
> On top of this, because the numbers are sometimes bigger, print a human
> friendly size after the KiB size if applicable, like so:
> 
>  * Final size of build directory: 1906 KiB (1.8 MiB)
>  * Final size of installed tree:     7 KiB
> 
> It should be noted that in case both sizes have a human-readable
> variant, they are also aligned.
> ---
>  bin/phase-functions.sh | 49 +++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 45 insertions(+), 4 deletions(-)
> 
> diff --git a/bin/phase-functions.sh b/bin/phase-functions.sh
> index dfd8733c8..af45a0d49 100644
> --- a/bin/phase-functions.sh
> +++ b/bin/phase-functions.sh
> @@ -598,10 +598,51 @@ __dyn_install() {
>  
>       # record build & installed size in build log
>       if type -P du &>/dev/null; then
> -             local sz=( $(du -ks "${WORKDIR}") )
> -             einfo "Final size of build directory: ${sz[0]} KiB"
> -             sz=( $(du -ks "${D}") )
> -             einfo "Final size of installed tree: ${sz[0]} KiB"
> +             local nsz=( $(du -ks "${WORKDIR}") )
> +             local isz=( $(du -ks "${D}") )
> +
> +             # align $1 to the right to the width of the widest of $1 and $2
> +             padl() {
> +                     local s1=$1
> +                     local s2=$2
> +                     local width=${#s1}
> +                     [[ ${#s2} -gt ${width} ]] && width=${#s2}
> +                     printf "%*s" ${width} "${s1}"
> +             }
> +
> +             # transform number in KiB into MiB, GiB or TiB based on size
> +             human() {
> +                     local s1=$1
> +                     local units=( KiB MiB GiB TiB )
> +
> +                     s1=$((s1 * 10))
> +                     while [[ ${s1} -gt 10240 && ${#units[@]} -gt 1 ]] ; do
> +                             s1=$((s1 / 1024 ))
> +                             units=( ${units[@]:1} )
> +                     done
> +
> +                     local r=${s1: -1}
> +                     s1=$((s1 / 10))
> +                     printf "%s.%s %s" "${s1}" "${r}" "${units[0]}"
> +             }
> +
> +             size() {
> +                     local s1=$1
> +                     local s2=$2
> +                     local out="$(padl "${s1}" "${s2}") KiB"
> +
> +                     if [[ ${s1} -gt 1024 ]] ; then
> +                             s1=$(human ${s1})
> +                             if [[ ${s2} -gt 1024 ]] ; then
> +                                     s2=$(human ${s2})
> +                                     s1=$(padl ${s1} ${s2})
> +                             fi
> +                             out+=" (${s1})"
> +                     fi
> +                     echo "${out}"
> +             }
> +             einfo "Final size of build directory: $(size ${nsz[0]} 
> ${isz[0]})"
> +             einfo "Final size of installed tree:  $(size ${isz[0]} 
> ${nsz[0]})"
>               __vecho
>       fi
>  
> 

Since bash doesn't support local functions [1], please define and use
them in a subshell, so that they do not leak into the persistent
environment.

[1] https://stackoverflow.com/questions/34985408/achieve-local-function
-- 
Thanks,
Zac

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to