Skip to content

Commit c505323

Browse files
committed
Resolution (macOS): improve detection
1. fix detection when `screenresolution` is not available 2. make scale factor detection actually work 3. remove `screenresolution` dependency EDIT: applied changes suggested by @hykilpikonna and fixed a [bug](#196) that neofetch fails to print scale factors when `system_profiler` fails to detect refresh rates.
1 parent ccd5d9f commit c505323

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

neofetch

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,36 +3018,48 @@ get_song() {
30183018
get_resolution() {
30193019
case $os in
30203020
"Mac OS X"|"macOS")
3021-
if type -p screenresolution >/dev/null; then
3022-
resolution="$(screenresolution get 2>&1 | awk '/Display/ {printf $6 "Hz, "}')"
3023-
resolution="${resolution//x??@/ @ }"
3024-
3025-
else
3026-
resolution="$(system_profiler SPDisplaysDataType |\
3027-
awk '/Resolution:/ {printf $2"x"$4" @ "$6"Hz, "}')"
3028-
fi
3029-
3030-
if [[ -e "/Library/Preferences/com.apple.windowserver.plist" ]]; then
3031-
scale_factor="$(PlistBuddy -c "Print DisplayAnyUserSets:0:0:Resolution" \
3032-
/Library/Preferences/com.apple.windowserver.plist)"
3033-
else
3034-
scale_factor=""
3035-
fi
3036-
3037-
# If no refresh rate is empty.
3038-
[[ "$resolution" == *"@ Hz"* ]] && \
3039-
resolution="${resolution//@ Hz}"
3021+
resolution=""
3022+
temp_plist="/tmp/neofetch_system_profiler_SPDisplaysDataType.xml" # PlistBuddy doesn't support reading from /dev/stdin
3023+
if system_profiler SPDisplaysDataType -xml > $temp_plist; then
3024+
for ((gpu=0; gpu<999; gpu++)); do
3025+
if PlistBuddy -c "print 0:_items:${gpu}" $temp_plist &> /dev/null; then
3026+
for ((display=0; display<999; display++)); do
3027+
if spdisplays_resolution="$(PlistBuddy -c "print 0:_items:${gpu}:spdisplays_ndrvs:${display}:_spdisplays_resolution" $temp_plist)" 2>/dev/null; then
3028+
spdisplays_resolution="${spdisplays_resolution//.[0-9][0-9]/}"
3029+
if spdisplays_pixels="$(PlistBuddy -c "print 0:_items:${gpu}:spdisplays_ndrvs:${display}:_spdisplays_pixels" $temp_plist)" 2>/dev/null; then
3030+
scaled_x="$(echo "$spdisplays_resolution" | awk '{print $1}')"
3031+
output_x="$(echo "$spdisplays_pixels" | awk '{print $1}')"
3032+
(( scale_factor=output_x/scaled_x ))
3033+
if [[ $scale_factor -gt 1 ]]; then
3034+
if [[ "$spdisplays_resolution" == *"@"* ]]; then
3035+
spdisplays_resolution="${spdisplays_resolution// @/ @${scale_factor}x @}"
3036+
else
3037+
spdisplays_resolution="${spdisplays_resolution} @ ${scale_factor}x"
3038+
fi
3039+
fi
3040+
fi
3041+
spdisplays_resolution="${spdisplays_resolution// x /x}"
3042+
[[ $gpu -gt 0 || $display -gt 0 ]] && resolution+=", "
3043+
resolution+="${spdisplays_resolution}"
3044+
else
3045+
break
3046+
fi
3047+
done
3048+
else
3049+
break
3050+
fi
3051+
done
30403052

3041-
[[ "${scale_factor%.*}" == 2 ]] && \
3042-
resolution="${resolution// @/@2x @}"
3053+
if [[ "$refresh_rate" == "off" ]]; then
3054+
resolution="${resolution/ @ [0-9][0-9][0.9]Hz}"
3055+
resolution="${resolution/ @ [0-9][0.9]Hz}"
3056+
resolution="${resolution/ @ [0-9]Hz}"
3057+
else
3058+
resolution="${resolution// @ 0Hz}"
3059+
fi
30433060

3044-
if [[ "$refresh_rate" == "off" ]]; then
3045-
resolution="${resolution// @ [0-9][0-9]Hz}"
3046-
resolution="${resolution// @ [0-9][0-9][0-9]Hz}"
3061+
rm $temp_plist
30473062
fi
3048-
3049-
[[ "$resolution" == *"0Hz"* ]] && \
3050-
resolution="${resolution// @ 0Hz}"
30513063
;;
30523064

30533065
"Windows")

0 commit comments

Comments
 (0)