Browse Source

[PR] dylanaraps/neofetch#2225 from TriMoon - Enhancing --memory_unit functionality

Upstream PR: https://github.com/dylanaraps/neofetch/pull/2225
Thanks to @TriMoon

Co-authored-by: ©TriMoon™ <TriMoon@users.noreply.github.com>
Azalea (on HyDEV-Daisy) 2 years ago
parent
commit
cde4914c19
1 changed files with 41 additions and 42 deletions
  1. 41 42
      neofetch

+ 41 - 42
neofetch

@@ -188,6 +188,12 @@ memory_percent="on"
 # gib: ' 0.98GiB / 6.79GiB'
 # gib: ' 0.98GiB / 6.79GiB'
 memory_unit="gib"
 memory_unit="gib"
 
 
+# Change memory output precision.
+#
+# Default: '2'
+# Values: integer ≥ 0
+# Flag:    --memory_precision
+mem_precision=2
 
 
 # Packages
 # Packages
 
 
@@ -3114,30 +3120,13 @@ get_memory() {
         ;;
         ;;
 
 
         "Mac OS X" | "macOS" | "iPhone OS")
         "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")
         "BSD" | "MINIX" | "ravynOS")
@@ -3228,43 +3217,51 @@ get_memory() {
 
 
     [[ "$memory_percent" == "on" ]] && ((mem_perc=mem_used * 100 / mem_total))
     [[ "$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
     case $memory_unit in
         tib)
         tib)
             mem_label=TiB
             mem_label=TiB
             memory_unit_divider=$((1024 * 1024))
             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)
         gib)
             mem_label=GiB
             mem_label=GiB
             memory_unit_divider=1024
             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)
         kib)
-            mem_used=$((mem_used * 1024))
-            mem_total=$((mem_total * 1024))
             mem_label=KiB
             mem_label=KiB
+            memory_unit_multiplier=1024
         ;;
         ;;
     esac
     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}%)}"
     memory="${mem_used} ${mem_label:-MiB} / ${mem_total} ${mem_label:-MiB} ${mem_perc:+(${mem_perc}%)}"
 
 
     # Bars.
     # Bars.
     case $memory_display in
     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
     esac
 }
 }
 
 
@@ -5827,7 +5824,8 @@ INFO:
     --song_format format        Print the song data in a specific format (see config file).
     --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.
     --song_shorthand on/off     Print the Artist/Album/Title on separate lines.
     --memory_percent on/off     Display memory percentage.
     --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.
     --music_player player-name  Manually specify a player to use.
                                 Available values are listed in the config file
                                 Available values are listed in the config file
 
 
@@ -6046,6 +6044,7 @@ get_args() {
             "--music_player") music_player="$2" ;;
             "--music_player") music_player="$2" ;;
             "--memory_percent") memory_percent="$2" ;;
             "--memory_percent") memory_percent="$2" ;;
             "--memory_unit") memory_unit="$2" ;;
             "--memory_unit") memory_unit="$2" ;;
+	    "--memory_precision") mem_precision="$2" ;;
             "--cpu_temp")
             "--cpu_temp")
                 cpu_temp="$2"
                 cpu_temp="$2"
                 [[ "$cpu_temp" == "on" ]] && cpu_temp="C"
                 [[ "$cpu_temp" == "on" ]] && cpu_temp="C"