Skip to content

Commit

Permalink
[PR] dylanaraps/neofetch#2225 from TriMoon - Enhancing --memory_unit …
Browse files Browse the repository at this point in the history
…functionality

Upstream PR: dylanaraps/neofetch#2225
Thanks to @TriMoon

Co-authored-by: ©TriMoon™ <TriMoon@users.noreply.github.com>
  • Loading branch information
hykilpikonna and TriMoon committed Nov 4, 2022
2 parents e8c7266 + 5012c99 commit cde4914
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions neofetch
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ memory_percent="on"
# gib: ' 0.98GiB / 6.79GiB'
memory_unit="gib"
# Change memory output precision.
#
# Default: '2'
# Values: integer ≥ 0
# Flag: --memory_precision
mem_precision=2
# Packages
Expand Down Expand Up @@ -3114,30 +3120,13 @@ get_memory() {
;;

"Mac OS X" | "macOS" | "iPhone OS")
if [[ $osx_version == 10.[4-5]* ]]; then
mem_total="$(system_profiler SPHardwareDataType | grep Memory:)"
mem_total="${mem_total/Memory\: /}"
mem_total="${mem_total/ MB/}"

mem_used="$(vm_stat | grep Pages\ active:)"
mem_used="${mem_used/Pages active\: /}"
mem_used="${mem_used/\./}"

pages_inactive=$(vm_stat | grep "Pages inactive")
pages_inactive=${pages_inactive/Pages inactive\: /}
pages_inactive=${pages_inactive/\./}

mem_used=$((mem_used + pages_inactive))
mem_used=$((mem_used * 4096 / 1048576))
else
hw_pagesize="$(sysctl -n hw.pagesize)"
mem_total="$(($(sysctl -n hw.memsize) / 1024 / 1024))"
pages_app="$(($(sysctl -n vm.page_pageable_internal_count) - $(sysctl -n vm.page_purgeable_count)))"
pages_wired="$(vm_stat | awk '/ wired/ { print $4 }')"
pages_compressed="$(vm_stat | awk '/ occupied/ { printf $5 }')"
pages_compressed="${pages_compressed:-0}"
mem_used="$(((pages_app + ${pages_wired//.} + ${pages_compressed//.}) * hw_pagesize / 1024 / 1024))"
fi
hw_pagesize="$(sysctl -n hw.pagesize)"
mem_total="$(($(sysctl -n hw.memsize) / 1024 / 1024))"
pages_app="$(($(sysctl -n vm.page_pageable_internal_count) - $(sysctl -n vm.page_purgeable_count)))"
pages_wired="$(vm_stat | awk '/ wired/ { print $4 }')"
pages_compressed="$(vm_stat | awk '/ occupied/ { printf $5 }')"
pages_compressed="${pages_compressed:-0}"
mem_used="$(((pages_app + ${pages_wired//.} + ${pages_compressed//.}) * hw_pagesize / 1024 / 1024))"
;;

"BSD" | "MINIX" | "ravynOS")
Expand Down Expand Up @@ -3228,43 +3217,51 @@ get_memory() {

[[ "$memory_percent" == "on" ]] && ((mem_perc=mem_used * 100 / mem_total))

# Creates temp variables: memory_unit_divider, memory_unit_multiplier
memory_unit_divider=1
memory_unit_multiplier=1

# Keep a copy of the original megabyte values because progress bar need them
mu_mb="$mem_used"
mt_mb="$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_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)
mem_used=$((mem_used * 1024))
mem_total=$((mem_total * 1024))
mem_label=KiB
memory_unit_multiplier=1024
;;
esac

# Uses temp variables from above: memory_unit_divider, memory_unit_multiplier
if test "$memory_unit_divider" -ge 1; then
printf -v mem_used "%'.*f" \
"${mem_precision}" \
$((mem_used / memory_unit_divider)).$((mem_used % memory_unit_divider))
printf -v mem_total "%'.*f" \
"${mem_precision}" \
$((mem_total / memory_unit_divider)).$((mem_total % memory_unit_divider))
elif test "$memory_unit_multiplier" -ge 1; then
mem_used=$((mem_used * memory_unit_multiplier))
mem_total=$((mem_total * memory_unit_multiplier))
fi

memory="${mem_used} ${mem_label:-MiB} / ${mem_total} ${mem_label:-MiB} ${mem_perc:+(${mem_perc}%)}"

# Bars.
case $memory_display in
"bar") memory="$(bar "${mem_used}" "${mem_total}")" ;;
"infobar") memory="${memory} $(bar "${mem_used}" "${mem_total}")" ;;
"barinfo") memory="$(bar "${mem_used}" "${mem_total}")${info_color} ${memory}" ;;
"bar") memory="$(bar "${mu_mb}" "${mt_mb}")" ;;
"infobar") memory="${memory} $(bar "${mu_mb}" "${mt_mb}")" ;;
"barinfo") memory="$(bar "${mu_mb}" "${mt_mb}")${info_color} ${memory}" ;;
esac
}

Expand Down Expand Up @@ -5827,7 +5824,8 @@ INFO:
--song_format format Print the song data in a specific format (see config file).
--song_shorthand on/off Print the Artist/Album/Title on separate lines.
--memory_percent on/off Display memory percentage.
--memory_unit kib/mib/gib Memory output unit.
--memory_unit (k/m/g/t)ib Memory output unit.
--memory_precision integer Change memory output precision. (≥0, default=2)
--music_player player-name Manually specify a player to use.
Available values are listed in the config file
Expand Down Expand Up @@ -6046,6 +6044,7 @@ get_args() {
"--music_player") music_player="$2" ;;
"--memory_percent") memory_percent="$2" ;;
"--memory_unit") memory_unit="$2" ;;
"--memory_precision") mem_precision="$2" ;;
"--cpu_temp")
cpu_temp="$2"
[[ "$cpu_temp" == "on" ]] && cpu_temp="C"
Expand Down

0 comments on commit cde4914

Please sign in to comment.