Skip to content

Commit cde4914

Browse files
hykilpikonnaTriMoon
andcommitted
[PR] dylanaraps/neofetch#2225 from TriMoon - Enhancing --memory_unit functionality
Upstream PR: dylanaraps/neofetch#2225 Thanks to @TriMoon Co-authored-by: ©TriMoon™ <TriMoon@users.noreply.github.com>
2 parents e8c7266 + 5012c99 commit cde4914

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

neofetch

+41-42
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ memory_percent="on"
188188
# gib: ' 0.98GiB / 6.79GiB'
189189
memory_unit="gib"
190190
191+
# Change memory output precision.
192+
#
193+
# Default: '2'
194+
# Values: integer ≥ 0
195+
# Flag: --memory_precision
196+
mem_precision=2
191197
192198
# Packages
193199
@@ -3114,30 +3120,13 @@ get_memory() {
31143120
;;
31153121

31163122
"Mac OS X" | "macOS" | "iPhone OS")
3117-
if [[ $osx_version == 10.[4-5]* ]]; then
3118-
mem_total="$(system_profiler SPHardwareDataType | grep Memory:)"
3119-
mem_total="${mem_total/Memory\: /}"
3120-
mem_total="${mem_total/ MB/}"
3121-
3122-
mem_used="$(vm_stat | grep Pages\ active:)"
3123-
mem_used="${mem_used/Pages active\: /}"
3124-
mem_used="${mem_used/\./}"
3125-
3126-
pages_inactive=$(vm_stat | grep "Pages inactive")
3127-
pages_inactive=${pages_inactive/Pages inactive\: /}
3128-
pages_inactive=${pages_inactive/\./}
3129-
3130-
mem_used=$((mem_used + pages_inactive))
3131-
mem_used=$((mem_used * 4096 / 1048576))
3132-
else
3133-
hw_pagesize="$(sysctl -n hw.pagesize)"
3134-
mem_total="$(($(sysctl -n hw.memsize) / 1024 / 1024))"
3135-
pages_app="$(($(sysctl -n vm.page_pageable_internal_count) - $(sysctl -n vm.page_purgeable_count)))"
3136-
pages_wired="$(vm_stat | awk '/ wired/ { print $4 }')"
3137-
pages_compressed="$(vm_stat | awk '/ occupied/ { printf $5 }')"
3138-
pages_compressed="${pages_compressed:-0}"
3139-
mem_used="$(((pages_app + ${pages_wired//.} + ${pages_compressed//.}) * hw_pagesize / 1024 / 1024))"
3140-
fi
3123+
hw_pagesize="$(sysctl -n hw.pagesize)"
3124+
mem_total="$(($(sysctl -n hw.memsize) / 1024 / 1024))"
3125+
pages_app="$(($(sysctl -n vm.page_pageable_internal_count) - $(sysctl -n vm.page_purgeable_count)))"
3126+
pages_wired="$(vm_stat | awk '/ wired/ { print $4 }')"
3127+
pages_compressed="$(vm_stat | awk '/ occupied/ { printf $5 }')"
3128+
pages_compressed="${pages_compressed:-0}"
3129+
mem_used="$(((pages_app + ${pages_wired//.} + ${pages_compressed//.}) * hw_pagesize / 1024 / 1024))"
31413130
;;
31423131

31433132
"BSD" | "MINIX" | "ravynOS")
@@ -3228,43 +3217,51 @@ get_memory() {
32283217

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

3220+
# Creates temp variables: memory_unit_divider, memory_unit_multiplier
3221+
memory_unit_divider=1
3222+
memory_unit_multiplier=1
3223+
3224+
# Keep a copy of the original megabyte values because progress bar need them
3225+
mu_mb="$mem_used"
3226+
mt_mb="$mem_total"
3227+
32313228
case $memory_unit in
32323229
tib)
32333230
mem_label=TiB
32343231
memory_unit_divider=$((1024 * 1024))
3235-
printf -v mem_used "%'.*f" \
3236-
${mem_precision:-2} \
3237-
$(($mem_used / $memory_unit_divider)).$(($mem_used % $memory_unit_divider))
3238-
printf -v mem_total "%'.*f" \
3239-
${mem_precision:-2} \
3240-
$(($mem_total / $memory_unit_divider)).$(($mem_total % $memory_unit_divider))
32413232
;;
32423233

32433234
gib)
32443235
mem_label=GiB
32453236
memory_unit_divider=1024
3246-
printf -v mem_used "%'.*f" \
3247-
${mem_precision:-2} \
3248-
$(($mem_used / $memory_unit_divider)).$(($mem_used % $memory_unit_divider))
3249-
printf -v mem_total "%'.*f" \
3250-
${mem_precision:-2} \
3251-
$(($mem_total / $memory_unit_divider)).$(($mem_total % $memory_unit_divider))
32523237
;;
32533238

32543239
kib)
3255-
mem_used=$((mem_used * 1024))
3256-
mem_total=$((mem_total * 1024))
32573240
mem_label=KiB
3241+
memory_unit_multiplier=1024
32583242
;;
32593243
esac
32603244

3245+
# Uses temp variables from above: memory_unit_divider, memory_unit_multiplier
3246+
if test "$memory_unit_divider" -ge 1; then
3247+
printf -v mem_used "%'.*f" \
3248+
"${mem_precision}" \
3249+
$((mem_used / memory_unit_divider)).$((mem_used % memory_unit_divider))
3250+
printf -v mem_total "%'.*f" \
3251+
"${mem_precision}" \
3252+
$((mem_total / memory_unit_divider)).$((mem_total % memory_unit_divider))
3253+
elif test "$memory_unit_multiplier" -ge 1; then
3254+
mem_used=$((mem_used * memory_unit_multiplier))
3255+
mem_total=$((mem_total * memory_unit_multiplier))
3256+
fi
3257+
32613258
memory="${mem_used} ${mem_label:-MiB} / ${mem_total} ${mem_label:-MiB} ${mem_perc:+(${mem_perc}%)}"
32623259

32633260
# Bars.
32643261
case $memory_display in
3265-
"bar") memory="$(bar "${mem_used}" "${mem_total}")" ;;
3266-
"infobar") memory="${memory} $(bar "${mem_used}" "${mem_total}")" ;;
3267-
"barinfo") memory="$(bar "${mem_used}" "${mem_total}")${info_color} ${memory}" ;;
3262+
"bar") memory="$(bar "${mu_mb}" "${mt_mb}")" ;;
3263+
"infobar") memory="${memory} $(bar "${mu_mb}" "${mt_mb}")" ;;
3264+
"barinfo") memory="$(bar "${mu_mb}" "${mt_mb}")${info_color} ${memory}" ;;
32683265
esac
32693266
}
32703267

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

0 commit comments

Comments
 (0)