From 6251cb5e4383af8c28620e1a03a6072f4d9921d9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= <zhangsongcui3371@sina.com>
Date: Tue, 8 Aug 2023 14:43:54 +0800
Subject: [PATCH] Resolution (macOS): improve detection

1. fix detection when `screenresolution` is not available
2. make scale factor detection actually work
3. remove `screenresolution` dependency
---
 neofetch | 62 +++++++++++++++++++++++++++++++-------------------------
 1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/neofetch b/neofetch
index 48b96d21..fdd5a590 100755
--- a/neofetch
+++ b/neofetch
@@ -3018,36 +3018,42 @@ get_song() {
 get_resolution() {
     case $os in
         "Mac OS X"|"macOS")
-            if type -p screenresolution >/dev/null; then
-                resolution="$(screenresolution get 2>&1 | awk '/Display/ {printf $6 "Hz, "}')"
-                resolution="${resolution//x??@/ @ }"
+            resolution=""
+            temp_plist="/tmp/neofetch_system_profiler_SPDisplaysDataType.xml" # PlistBuddy doesn't support reading from /dev/stdin
+            if system_profiler SPDisplaysDataType -xml > $temp_plist; then
+                for ((gpu=0; gpu<999; gpu++)); do
+                    if PlistBuddy -c "print 0:_items:${gpu}" $temp_plist &> /dev/null; then
+                        for ((display=0; display<999; display++)); do
+                            if spdisplays_resolution="$(PlistBuddy -c "print 0:_items:${gpu}:spdisplays_ndrvs:${display}:_spdisplays_resolution" $temp_plist)" 2>/dev/null; then
+                                spdisplays_resolution="${spdisplays_resolution//.[0-9][0-9]/}"
+                                if spdisplays_pixels="$(PlistBuddy -c "print 0:_items:${gpu}:spdisplays_ndrvs:${display}:_spdisplays_pixels" $temp_plist)" 2>/dev/null; then
+                                  scaled_x="$(echo $spdisplays_resolution | awk '{print $1}')"
+                                  output_x="$(echo $spdisplays_pixels | awk '{print $1}')"
+                                  let scale_factor=$output_x/$scaled_x
+                                  [[ $scale_factor > 1 ]] && spdisplays_resolution="${spdisplays_resolution// @/ @${scale_factor}x @}"
+                                fi
+                                spdisplays_resolution="${spdisplays_resolution// x /x}"
+                                [[ $gpu > 0 || $display > 0 ]] && resolution+=", "
+                                resolution+="${spdisplays_resolution}"
+                            else
+                                break
+                            fi
+                        done
+                    else
+                        break
+                    fi
+                done
 
-            else
-                resolution="$(system_profiler SPDisplaysDataType |\
-                              awk '/Resolution:/ {printf $2"x"$4" @ "$6"Hz, "}')"
+                if [[ "$refresh_rate" == "off" ]]; then
+                    resolution="${resolution/ @ [0-9][0-9][0.9]Hz}"
+                    resolution="${resolution/ @ [0-9][0.9]Hz}"
+                    resolution="${resolution/ @ [0-9]Hz}"
+                else
+                    resolution="${resolution// @ 0Hz}"
+                fi
+
+                rm $temp_plist
             fi
-
-            if [[ -e "/Library/Preferences/com.apple.windowserver.plist" ]]; then
-                scale_factor="$(PlistBuddy -c "Print DisplayAnyUserSets:0:0:Resolution" \
-                                /Library/Preferences/com.apple.windowserver.plist)"
-            else
-                scale_factor=""
-            fi
-
-            # If no refresh rate is empty.
-            [[ "$resolution" == *"@ Hz"* ]] && \
-                resolution="${resolution//@ Hz}"
-
-            [[ "${scale_factor%.*}" == 2 ]] && \
-                resolution="${resolution// @/@2x @}"
-
-            if [[ "$refresh_rate" == "off" ]]; then
-                resolution="${resolution// @ [0-9][0-9]Hz}"
-                resolution="${resolution// @ [0-9][0-9][0-9]Hz}"
-            fi
-
-            [[ "$resolution" == *"0Hz"* ]] && \
-                resolution="${resolution// @ 0Hz}"
         ;;
 
         "Windows")