Skip to content

Instantly share code, notes, and snippets.

@rkok
Last active April 16, 2024 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkok/bd4e3972086caef9358197b8a58d80db to your computer and use it in GitHub Desktop.
Save rkok/bd4e3972086caef9358197b8a58d80db to your computer and use it in GitHub Desktop.
X screen dimmer toggler - Uses xrandr to toggle between dimming/brightening connected displays
#!/usr/bin/env bash
#####################
# toggle-screen-dimmer.sh - X screen dimmer toggler
# Uses xrandr to toggle between dimming/brightening connected displays
# See --help for usage
# Recommended: create a launcher for this script on the task bar!
#####################
list_displays() {
xrandr --verbose --current | awk '/ connected/ {print $1}'
}
if [ "$1" == "--list" ]; then
echo "Available displays:"
list_displays | sed 's/^/ - /g'
exit 0
elif [ "$1" == "--help" ]; then
echo "X screen dimmer toggler"
echo
echo "Usage: $0 [--help|--list] [displayname [dim_level]]"
echo
echo "Examples:"
echo
echo " List available displays:"
echo " $0 --list"
echo
echo " Toggle for all connected displays:"
echo " $0"
echo
echo " Toggle for a single display:"
echo " $0 eDP-1"
echo
echo " Toggle for a single display with a specific dim level:"
echo " $0 eDP-1 0.30"
exit 1
fi
val_dim="0.30"
if [ -n "$2" ]; then
if ! echo "$2" | grep -qE '^0(\.[0-9]{2})?$'; then
echo "Invalid dim level: '$2'. Try: 0.30" >&2
exit 1
fi
val_dim="$2"
fi
val_normal=1
if [ -n "$1" ]; then
displays="$1"
else
displays="$(list_displays)"
fi
for display in $displays; do
curdim="$(xrandr --verbose --current | grep "^$display" -A5 | awk '/Bright/ {print $2}')"
newval="$val_dim"
if [ "$curdim" == "$val_dim" ]; then
newval=$val_normal
fi
set -x
xrandr --output "$display" --brightness "$newval"
{ set +x; } 2>/dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment