Add attribute for enabled/disabled monitors

This commit is contained in:
dabruh 2024-04-19 09:30:18 +02:00
parent b433ef57d3
commit 7128f6c74e

View file

@ -4,21 +4,40 @@
set -euo pipefail
_monitors="" # Cache for the xrandr output
_monitors=() # Cache for the xrandr output
# Get the list of connected and disconnected monitors.
# Includes information about the primary monitor
# E.g:
# eDP-1,connected,primary
# HDMI-1,disconnected
# DP-1,disconnected
# eDP-1,connected,primary,1920x1080+0+0,enabled
# HDMI-1,disconnected,1920x1080+1920+0,enabled
# DP-1,disconnected,disabled
_get_monitors() {
if [ -z "$_monitors" ]; then
# Cache the xrandr output
_monitors=$(xrandr | grep -Eo '^.* ((dis|)connected)( primary|) ' | sed 's/ /,/g' | sed 's/,$//g')
local monitors enabled_regex="[[:digit:]]+x[[:digit:]]+\+[[:digit:]]+\+[[:digit:]]+" # Regex for enabled monitors
# Use caching to avoid multiple calls to xrandr
if [ ${#_monitors[@]} -eq 0 ]; then
# Get the list of connected and disconnected monitors
monitors=$(xrandr | grep -Eo "^.* ((dis|)connected)( primary|) ($enabled_regex|)" | sed 's/ /,/g' | sed 's/,$//g')
for monitor in $monitors; do
# Set "enabled" if it matches the enabled regex and remove the matching string, else set "disabled"
if [[ "$monitor" =~ $enabled_regex ]]; then
monitor="$monitor,enabled"
else
monitor="$monitor,disabled"
fi
# Add to the list of monitors
_monitors+=("$monitor")
done
# _monitors="$monitors"
fi
echo "$_monitors"
for monitor in "${_monitors[@]}"; do
echo "$monitor"
done
}
# Function that gets all monitors and takes an optional amount of arguments to filter the list.