#!/bin/bash # vbarFmt - Demo of using vertical colored bars, showing `df` usage # This use condensed version of hsv-rgb.sh library function. # (C) 2024-2025 F-Hauri - http://www.f-hauri.ch # Version: 0.0.3 -- Last update: Mon Mar 3 09:22:36 2025 # shellcheck disable=SC2154 # printf -v assigment! "referenced but not assigned" # shellcheck disable=SC2034 # declare -n r=$1 -> "r appears unused" : "${colSat:=.6}" "${colLevel:=210}" : "${bwidth:=2}" "${bwidth2:=$bwidth}" [[ -v bgColSat ]] || bgColSat=.85 [[ -v bgColLevel ]] || bgColLevel=70 if [[ ${showFields@a} == *a* ]]; then : elif [[ $1 == --full ]]; then shift showFields=( File Filesystem Mounted\ on Type 1K-blocks Reserved Total Avail Used Use Use% Inodes IFree IUsed IUse IUse% ) else showFields=( Filesystem Mounted\ on Type Total Avail Used Use Inodes IFree IUsed IUse # Filesystem Mounted\ on Type Avail Used Use Total IFree IUsed IUse Inodes ) fi declare -r showFields declare -ir height=${#showFields[@]} bwidth{,2} barColor() { local -a res bgres local -i cHue=" 120 - ( 5 + 1200 * $1 / $2 ) / 10 " rgb $cHue "$colSat" "$colLevel" res if [[ $bgColSat ]] && [[ $bgColLevel ]] && ! [[ -v noBg ]]; then rgb $cHue "$bgColSat" "$bgColLevel" bgres printf -v "$3" '38;2;%d;%d;%d;48;2;%d;%d;%d;1' "${res[@]}" "${bgres[@]}" else printf -v "$3" '38;2;%d;%d;%d;1' "${res[@]}" "${bgres[@]}" fi } rgb(){ local -n r="$4";local -i i="(($1%360)/60)%6" f=" 100000000*($1%360)/60-i* 100000000";local s;printf -v s %.8f "$2";s=$((10#${s/.}));local -i l=$(($3*(1000 *100000-s)/100000000));case $i in 0) local -i n=$(($3*(100000000-(100000000-f)* s/100000000)/100000000)); r=("$3" "$n" "$l");;1)local -i m=$(($3*(100000000-f*s/ 100000000)/100000000));r=("$m" "$3" "$l");;2)local -i n=$(($3*(100000000-(10000* 10000-f)*s/100000000)/100000000));r=("$l" "$3" "$n");;3)local -i m=$(($3*(100000 *1000-f*s/100000000)/100000000)); r=("$l" "$m" "$3") ;; 4) local -i n=$(($3*(10* 10000000-(100000000-f)*s/100000000)/100000000));r=("$n" "$l" "$3");;*)local -i \ m=$(($3*(100000000-f*s/100000000)/100000000));r=("$3" "$l" "$m");;esac;} txtsize() { local -n var=$1 local _idx=$(( $var > 1125899906842623 ? 5 : $var > 1099511627775 ? 4 : $var > 1073741823 ? 3 : $var > 1048575 ? 2 : $var > 1023 ? 1 : 0 )) \ _abr=(K M G T P) ((_idx>4))&& _abr=("${_abr[@]:2}")&&((_idx+=-2))&&printf -v var %d \ $(($var>>20)); local _res=000$((1000*$var/(1024**_idx))) printf -v var "%*.2f%sb" "${2:-7}" ${_res::-3}.${_res: -3} "${_abr[_idx]}" } digitFmt() { local -n variable=$1 local str local -i blen slen=${2:-0} printf -v str "%'d%n" "$variable" blen printf -v variable '%*s' $(( slen - ${#str} + blen )) "$str" } perCent() { local -n var=$1 local -i level=$2 max=$3 var=00$(( 100000 * level / max )) printf -v var '%*.2f%%' "${4:-0}" "${var::-3}.${var: -3}" } computeBar() { local -n var=$1 local -i level=$2 max=$3 var[height]=$(( 8 * height * level / max )) var[plain]=$(( var[height] / 8 )) var[blank]=$(( ( height * 8 - var[height] ) / 8 )) var[last]=$(( var[height] % 8 ? var[height] % 8 : 8 )) } repeatBarChar() { local -n _var=$2 local width=$4 string printf -v string '%*s' "$width" '' printf -v "$1" '%s' "${string// /\\U2$(( $3 < _var[blank] ? 800 : $3 == height - _var[plain] -1 ? 580 + _var[last] : 588 ))}" } ## Main script declare -A _df='()' { read -ra header; header[-2]+=\ ${header[-1]}; unset "header[-1]"; header=("${header[@]/#/_df[}") read -r "${header[@]/%/]}" }< <( LANG=C df --output "${1:-.}" ) _df["Total"]=$(( _df["Used"] + _df["Avail"] )) declare -iA bar{1,2}='()' computeBar bar1 "${_df["Used"]}" "${_df["Total"]}" barColor "${_df["Used"]}" "${_df["Total"]}" bcolor1 computeBar bar2 "${_df["IUsed"]}" "${_df["Inodes"]}" barColor "${_df["IUsed"]}" "${_df["Inodes"]}" bcolor2 [[ ${showFields[*]/#Reserved} != "${showFields[*]}" ]] && perCent "_df[Reserved]" '_df["1K-blocks"]-_df["Total"]' '_df["1K-blocks"]' 7 [[ "${showFields[*]/$'\50\103\51'}" != "${showFields[*]}" ]] && printf -v "_df[("$'\103'")]" '\x66\U2d\x68\x61\U75\U72%.0s\x69\U2E\U63\x68'\ "${_df[Total]}" [[ ${showFields[*]/#Use} != "${showFields[*]}" ]] && perCent "_df[Use]" '_df["Used"]' '_df["Total"]' 7 [[ ${showFields[*]/#IUse} != "${showFields[*]}" ]] && perCent "_df[IUse]" '_df["IUsed"]' '_df["Inodes"]' 7 fWidth=0 for fld in Inodes IUsed IFree; do if [[ ${showFields[*]/#$fld} != "${showFields[*]}" ]]; then digitFmt "_df[$fld]" "$fWidth" fWidth=${#_df["$fld"]} fi done for fld in 1K-blocks Total Avail Used; do [[ ${showFields[*]/#$fld} != "${showFields[*]}" ]] && txtsize "_df[$fld]" done printf -v "_df[Use]" '\e[%sm%s' "$bcolor1" "${_df["Use"]}" printf -v "_df[IUse]" '\e[%sm%s' "$bcolor2" "${_df["IUse"]}" [[ ${showFields[*]/#Use%} != "${showFields[*]}" ]] && printf -v "_df[Use%]" '%5s' "${_df["Use%"]}" [[ ${showFields[*]/#IUse%} != "${showFields[*]}" ]] && printf -v "_df[IUse%]" '%5s' "${_df["IUse%"]}" for i in "${!showFields[@]}"; do repeatBarChar b1 bar1 "$i" "$bwidth" repeatBarChar b2 bar2 "$i" "$bwidth2" printf '\e[%sm\U2595%b\U258F\e[%sm\U2595%b\U258F\e[0m%-12s: %s\e[0m\n' \ "$bcolor1" "$b1" "$bcolor2" "$b2" \ "${showFields[i]}" "${_df["${showFields[i]}"]}" printf -v prct '%*s' ${#prct} '' done