Skip to content

Commit

Permalink
Enhancing --memory_unit functionality
Browse files Browse the repository at this point in the history
Improving dylanaraps/neofetch@0435dcd
- Implementing dylanaraps/neofetch#1170 (comment) to avoid awk usage.
- Adding ability to configure precision of output using `mem_precision` which defaults to `2`.
- Added `tib` to accommodate TiB mentioned in dylanaraps/neofetch#1170 (comment)
  • Loading branch information
TriMoon authored Oct 31, 2022
1 parent ccd5d9f commit 67cf02c
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions neofetch
Original file line number Diff line number Diff line change
Expand Up @@ -2802,10 +2802,26 @@ get_memory() {
[[ "$memory_percent" == "on" ]] && ((mem_perc=mem_used * 100 / mem_total))

case $memory_unit in
tib)
mem_label=TiB
memory_unit_divider=$((1024 * 1024))
printf -v mem_used "%'.*f" \
${mem_precision:-2} \
$(($mem_used / $memory_unit_divider)).$(($mem_used % $memory_unit_divider))
printf -v mem_total "%'.*f" \
${mem_precision:-2} \
$(($mem_total / $memory_unit_divider)).$(($mem_total % $memory_unit_divider))
;;

gib)
mem_used=$(awk '{printf "%.2f", $1 / $2}' <<< "$mem_used 1024")
mem_total=$(awk '{printf "%.2f", $1 / $2}' <<< "$mem_total 1024")
mem_label=GiB
memory_unit_divider=1024
printf -v mem_used "%'.*f" \
${mem_precision:-2} \
$(($mem_used / $memory_unit_divider)).$(($mem_used % $memory_unit_divider))
printf -v mem_total "%'.*f" \
${mem_precision:-2} \
$(($mem_total / $memory_unit_divider)).$(($mem_total % $memory_unit_divider))
;;

kib)
Expand Down

0 comments on commit 67cf02c

Please sign in to comment.