Skip to content

Commit 391ff75

Browse files
[PR] dylanaraps/neofetch#2356 from CarterLi - Resolution (macOS): improve detection
Upstream PR: dylanaraps/neofetch#2356 Thanks to @CarterLi Co-authored-by: 李通洲 <[email protected]>
2 parents df0c2be + 6251cb5 commit 391ff75

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

neofetch

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3819,36 +3819,42 @@ get_song() {
38193819
get_resolution() {
38203820
case $os in
38213821
"Mac OS X"|"macOS")
3822-
if type -p screenresolution >/dev/null; then
3823-
resolution="$(screenresolution get 2>&1 | awk '/Display/ {printf $6 "Hz, "}')"
3824-
resolution="${resolution//x??@/ @ }"
3825-
3826-
else
3827-
resolution="$(system_profiler SPDisplaysDataType |\
3828-
awk '/Resolution:/ {printf $2"x"$4" @ "$6"Hz, "}')"
3829-
fi
3830-
3831-
if [[ -e "/Library/Preferences/com.apple.windowserver.plist" ]]; then
3832-
scale_factor="$(PlistBuddy -c "Print DisplayAnyUserSets:0:0:Resolution" \
3833-
/Library/Preferences/com.apple.windowserver.plist)"
3834-
else
3835-
scale_factor=""
3836-
fi
3837-
3838-
# If no refresh rate is empty.
3839-
[[ "$resolution" == *"@ Hz"* ]] && \
3840-
resolution="${resolution//@ Hz}"
3822+
resolution=""
3823+
temp_plist="/tmp/neofetch_system_profiler_SPDisplaysDataType.xml" # PlistBuddy doesn't support reading from /dev/stdin
3824+
if system_profiler SPDisplaysDataType -xml > $temp_plist; then
3825+
for ((gpu=0; gpu<999; gpu++)); do
3826+
if PlistBuddy -c "print 0:_items:${gpu}" $temp_plist &> /dev/null; then
3827+
for ((display=0; display<999; display++)); do
3828+
if spdisplays_resolution="$(PlistBuddy -c "print 0:_items:${gpu}:spdisplays_ndrvs:${display}:_spdisplays_resolution" $temp_plist)" 2>/dev/null; then
3829+
spdisplays_resolution="${spdisplays_resolution//.[0-9][0-9]/}"
3830+
if spdisplays_pixels="$(PlistBuddy -c "print 0:_items:${gpu}:spdisplays_ndrvs:${display}:_spdisplays_pixels" $temp_plist)" 2>/dev/null; then
3831+
scaled_x="$(echo $spdisplays_resolution | awk '{print $1}')"
3832+
output_x="$(echo $spdisplays_pixels | awk '{print $1}')"
3833+
let scale_factor=$output_x/$scaled_x
3834+
[[ $scale_factor > 1 ]] && spdisplays_resolution="${spdisplays_resolution// @/ @${scale_factor}x @}"
3835+
fi
3836+
spdisplays_resolution="${spdisplays_resolution// x /x}"
3837+
[[ $gpu > 0 || $display > 0 ]] && resolution+=", "
3838+
resolution+="${spdisplays_resolution}"
3839+
else
3840+
break
3841+
fi
3842+
done
3843+
else
3844+
break
3845+
fi
3846+
done
38413847

3842-
[[ "${scale_factor%.*}" == 2 ]] && \
3843-
resolution="${resolution// @/@2x @}"
3848+
if [[ "$refresh_rate" == "off" ]]; then
3849+
resolution="${resolution/ @ [0-9][0-9][0.9]Hz}"
3850+
resolution="${resolution/ @ [0-9][0.9]Hz}"
3851+
resolution="${resolution/ @ [0-9]Hz}"
3852+
else
3853+
resolution="${resolution// @ 0Hz}"
3854+
fi
38443855

3845-
if [[ "$refresh_rate" == "off" ]]; then
3846-
resolution="${resolution// @ [0-9][0-9]Hz}"
3847-
resolution="${resolution// @ [0-9][0-9][0-9]Hz}"
3856+
rm $temp_plist
38483857
fi
3849-
3850-
[[ "$resolution" == *"0Hz"* ]] && \
3851-
resolution="${resolution// @ 0Hz}"
38523858
;;
38533859

38543860
"Windows")

0 commit comments

Comments
 (0)