From cfec9334ef7a21842bdb01d9253a9af84cf2e8a0 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Fri, 7 Oct 2022 04:48:25 +0200 Subject: [PATCH] monitor-setup: Support resolution and refresh rate Also place multiple monitors side-by-side. --- .local/bin/monitor-setup | 54 +++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/.local/bin/monitor-setup b/.local/bin/monitor-setup index 0323b67..3d4c2bb 100755 --- a/.local/bin/monitor-setup +++ b/.local/bin/monitor-setup @@ -1,13 +1,7 @@ #!/usr/bin/env bash - -################################################################################ # -# Small script using `xrandr` and `fzf` to quickly select connected monitors. -# -# TODO: -# - Add possibility to not only mirror onto all outputs but use the total -# screen space. -################################################################################ +# monitor setup script using xrandr and fzf to manage multiple monitors and/or +# set resolutions and refresh rates. command -v xrandr &>/dev/null || exit 1 command -v fzf &>/dev/null || exit 1 @@ -62,9 +56,51 @@ primary="$(<<<"$formatted" \ # Build the `xrandr` command xrandr_cmd=(xrandr) +# Keep track of width to place the outputs side-by-side +width=0 + for out in "${selected[@]}"; do - xrandr_cmd+=(--output "$out" --rotate normal --auto) + xrandr_cmd+=(--output "$out" --rotate normal) [[ $out != $primary ]] || xrandr_cmd+=(--primary) + + all_res="$(<<<"$xrandr_q" sed -En " + # all resolution lines for selected output (+ junk lines first and last) + /$out/,/^[^ ]/ { + # on lines that contain a resolution + /^ / { + # only print the resolution + s/^ *([^ ]*).*/\1/p + } + }" + )" + res="$(<<<"$all_res" fzf --cycle --header "Select resolution for $out")" + [[ $res ]] || exit 1 + + xrandr_cmd+=(--mode "$res") + + rates="$(<<<"$xrandr_q" sed -En " + # all resolution lines for selected output + /$out/,/^[^ ]/ { + # on line with the selected resolution + /^ *$res/ { + # remove resolution and whitespace + s/^ *$res *// + # fix formatting when preferred mode is not the current one + s/ \+/+/ + # put each rate on a new line + s/ +/\n/g + + p + } + }" + )" + rate="$(<<<"$rates" fzf --cycle --header "Select refresh rate for $out")" + [[ $rate ]] || exit 1 + rate="${rate%%[*+]*}" # remove current and preferred mode indicator + xrandr_cmd+=(--rate "$rate") + + xrandr_cmd+=(--pos "${width}x0") + : "$((width+=${res%%x*}))" done for out in "${non_selected[@]}"; do