Skip to content

Instantly share code, notes, and snippets.

@lamchau
Last active May 13, 2016 18:45
Show Gist options
  • Save lamchau/d28d07bf1ec7ce1b60a8ef95d28a63ee to your computer and use it in GitHub Desktop.
Save lamchau/d28d07bf1ec7ce1b60a8ef95d28a63ee to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
script_name="${0##*/}"
if [ "$#" -ne 2 ]; then
echo "Usage: $script_name width height refresh"
exit 1
fi
width=$1
height=$2
refresh=${3:-60}
if [ "$(xrandr | grep -c "${width}x${height}")" -eq 1 ]; then
xrandr -s "${width}x${height}"
exit 0
fi
_cvt=$(cvt "$width" "$height" "$refresh" | tail -1)
mode=${_cvt//Modeline}
name=$(echo "$_cvt" | awk '{ print $2 }' | tr -d '"')
display=$(xrandr | grep -e " connected [^(]" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
# shellcheck disable=SC2116
message="Do you wish to add and change the resolution to ${width}x${height} at ${refresh}hz?$(echo $'\ny/n> ')"
while true; do
read -rep "$message" yn
case $yn in
[Yy]*)
xrandr --newmode "$mode"
xrandr --addmode "$display" "$name"
xrandr -s "$name"
exit
;;
[Nn]*)
exit
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment