Disk: Rewrite get_disk.

This commit is contained in:
Dylan Araps 2016-12-16 22:32:46 +11:00
parent 03ad65d066
commit 5abba17564

View file

@ -1539,63 +1539,28 @@ get_term_font() {
get_disk() { get_disk() {
type -p df >/dev/null 2>&1 || { err "Disk requires 'df' to function. Install 'df' to get disk info."; return; } type -p df >/dev/null 2>&1 || { err "Disk requires 'df' to function. Install 'df' to get disk info."; return; }
# df flags # Get the info for /
case "$os" in disks=($(df /))
"Linux" | "iPhone OS" | "Windows" | "GNU")
df_flags=(-h -l --total)
df_dir="total"
case "$distro" in # Split the info
"OpenWRT"*) df_flags=(-h); df_dir="rootfs" ;; disk_used="$((disks[9] / 1024 / 1024))GB"
"Android"*) return ;; disk_total="$((disks[8] / 1024 / 1024))GB"
esac
;;
"Mac OS X" | "BSD" | "Haiku") # Handle TB values
case "$distro" in (( "${#disk_used}" == 6 )) && disk_used="${disk_used:0:1}.${disk_used:1:1}TB"
"FreeBSD"* | *"OS X"* | "Mac"*) (( "${#disk_total}" == 6 )) && disk_total="${disk_total:0:1}.${disk_total:1:1}TB"
df_flags=(-l -H /)
df_dir="/"
;;
*) return ;; disk_total_per="${disks[11]}"
esac
;;
esac
# Get the disk info
disk="$(df "${df_flags[@]}" | awk -v dir="$df_dir" '$0 ~ dir {print $2 ":" $3 ":" $5}')"
# Format the output
disk_used="${disk#*:}"
disk_used="${disk_used%%:*}"
disk_total="${disk%%:*}"
disk_total_per="${disk#*:*:}"
# Put it all together # Put it all together
disk="${disk_used} / ${disk_total} (${disk_total_per})" disk="${disk_used:+${disk_used} / }${disk_total} ${disk_total_per:+(${disk_total_per})}"
# Add info bar
disk_used="${disk_used/G}"
disk_total="${disk_total/G}"
# Convert Terabytes to Gigabytes.
if [[ "$disk_display" != "off" ]]; then
disk_used="${disk_used/\.}"
disk_total="${disk_total/\.}"
[[ "${disk_used: -1}" == "T" ]] && \
disk_used="$((${disk_used/T} * 100))"
[[ "${disk_total: -1}" == "T" ]] && \
disk_total="$((${disk_total/T} * 100))"
fi
# Bar
case "$disk_display" in case "$disk_display" in
"bar") disk="$(bar "${disk_used/'.'*}" "${disk_total/'.'*}")" ;; "bar") disk="$(bar "${disk_used//[a-z]}" "${disk_total//[a-z]}")" ;;
"infobar") disk+=" $(bar "${disk_used/'.'*}" "${disk_total/'.'*}")" ;; "infobar") disk+=" $(bar "${disk_used//[a-z]}" "${disk_total//[a-z]}")" ;;
"barinfo") disk="$(bar "${disk_used/'.'*}" "${disk_total/'.'*}") $disk" ;; "barinfo") disk="$(bar "${disk_used//[a-z]}" "${disk_total//[a-z]}") $disk" ;;
"perc") disk="$disk_total_per $(bar "${disk_used/'.'*}" "${disk_total/'.'*}")" ;; "perc") disk="$disk_total_per $(bar "${disk_used//[a-z]}" "${disk_total//[a-z]}")" ;;
esac esac
} }