monitor-setup: Rewrite
This commit is contained in:
@@ -12,55 +12,65 @@
|
|||||||
command -v xrandr &>/dev/null || exit 1
|
command -v xrandr &>/dev/null || exit 1
|
||||||
command -v fzf &>/dev/null || exit 1
|
command -v fzf &>/dev/null || exit 1
|
||||||
|
|
||||||
# Grep for all connected outs including their highest resolution,
|
xrandr_q="$(xrandr -q)"
|
||||||
# throw away additional information and finally merge the out name and
|
|
||||||
# resolution lines separating them with a tab.
|
# Get outputs currently in use as regex matching either one (e.g. `HDMI-0|DP-0`)
|
||||||
connected_outs="$(
|
in_use="$( <<<"$xrandr_q" \
|
||||||
xrandr -q \
|
tac | # Reverse for non-greedy matches
|
||||||
| grep -A1 ' connected' \
|
sed -En '/\*/,/ connected/p' | # lines from selected config to it's name
|
||||||
| sed 's/^[[:space:]]*//' \
|
grep -Po '^.*(?= connected)' | # only the name
|
||||||
| grep -Eo "^([^ ]* connected|[0-9]+x[0-9]+)" \
|
tr '\n' '|' # format into regex
|
||||||
| paste -d " " - - \
|
|
||||||
| sed 's/ connected \(.*\)/\t(\1)/'
|
|
||||||
)"
|
)"
|
||||||
[[ -n "$connected_outs" ]] || exit 1
|
in_use="${in_use%|}" # remove trailing `|`
|
||||||
|
|
||||||
# Multi select on connected outs
|
# Get all outputs in a table in the form `<name> <res> [*]`
|
||||||
selected_outs="$(fzf -m --cycle --header "Select outputs" <<<"$connected_outs")"
|
formatted="$(<<<"$xrandr_q" \
|
||||||
[[ -n "$selected_outs" ]] || exit 1
|
grep -wA1 connected | # output line and highest res
|
||||||
|
awk '{ print $1 }' | # only name and resolution
|
||||||
|
grep -ve -- | # remove grep's match separator
|
||||||
|
paste - - | # join name and res
|
||||||
|
sed -E "s/($in_use).*/&\t*/" | # append `*` to all in-use
|
||||||
|
column -t # tabularize
|
||||||
|
)"
|
||||||
|
|
||||||
# Selection of primary out out of the selected ones if there is more than one.
|
# Select all outputs to-use
|
||||||
if [[ $(wc -l <<<"$selected_outs") -gt 1 ]]; then
|
selected=( $(<<<"$formatted" \
|
||||||
primary_out="$(fzf --cycle --header "Select primary" <<<"$selected_outs")"
|
fzf -m --cycle --header "Select outputs" | # select
|
||||||
else
|
cut -d' ' -f1 # only output name
|
||||||
primary_out="$selected_outs"
|
) )
|
||||||
fi
|
# Abort if nothing was selected
|
||||||
# Strip trailing resolution
|
(( ${#selected[@]} )) || exit 1
|
||||||
primary_out="$(cut -d$'\t' -f1 <<<"$primary_out")"
|
|
||||||
[[ -n "$primary_out" ]] || exit 1
|
|
||||||
|
|
||||||
# "Convert" to array and strip trailing resolutions
|
# Regex to match either one of the selected outputs
|
||||||
# Only done here to display the resolutions in the fzf dialogs.
|
selected_regex="$(<<<"${selected[@]}" tr ' ' '|')"
|
||||||
typeset -a selected_outs=($(cut -d$'\t' -f1 <<<"$selected_outs"))
|
|
||||||
typeset -a connected_outs=($(cut -d$'\t' -f1 <<<"$connected_outs"))
|
|
||||||
|
|
||||||
# Build `xrandr` command
|
# Gather non-selected outputs
|
||||||
typeset -a xrandr_command
|
non_selected=( $(<<<"$xrandr_q" \
|
||||||
xrandr_command+="xrandr"
|
grep connected | # list lines with output ({,dis}connected)
|
||||||
for out in "${connected_outs[@]}"; do
|
grep -Ev "$selected_regex" | # filter out selected ones
|
||||||
xrandr_command+=(--output "$out")
|
cut -d' ' -f1 # only output name
|
||||||
|
) )
|
||||||
|
|
||||||
# TODO: Solution with better performance?
|
# Choose primary
|
||||||
if printf "%s\n" "${selected_outs[@]}" | grep -qE "^$out$"; then
|
primary="$(<<<"$formatted" \
|
||||||
# Current out was selected by user
|
grep -E "$selected_regex" | # use formatted for more information
|
||||||
xrandr_command+=(--auto)
|
fzf --cycle --select-1 --header "Select primary" |
|
||||||
else
|
cut -d' ' -f1 # name only
|
||||||
xrandr_command+=(--off)
|
)"
|
||||||
fi
|
[[ $primary ]] || exit 1
|
||||||
|
|
||||||
[[ "$out" != "$primary_out" ]] || xrandr_command+=(--primary)
|
# Build the `xrandr` command
|
||||||
|
xrandr_cmd=(xrandr)
|
||||||
|
|
||||||
|
for out in "${selected[@]}"; do
|
||||||
|
xrandr_cmd+=(--output "$out" --rotate normal --auto)
|
||||||
|
[[ $out != $primary ]] || xrandr_cmd+=(--primary)
|
||||||
|
done
|
||||||
|
|
||||||
|
for out in "${non_selected[@]}"; do
|
||||||
|
xrandr_cmd+=(--output "$out" --off)
|
||||||
done
|
done
|
||||||
|
|
||||||
# Print for debugging purposes and finally execute it
|
# Print for debugging purposes and finally execute it
|
||||||
printf "%s\n" "${xrandr_command[*]}"
|
printf "%s\n" "${xrandr_cmd[*]}"
|
||||||
"${xrandr_command[@]}"
|
"${xrandr_cmd[@]}"
|
||||||
|
|||||||
Reference in New Issue
Block a user