#!/bin/sh # setWindowSize.sh: Sets the selected window size # # Usage: setWindowSize.sh [-p|-n] usageMessage="Usage: ${0} [-p|-n]" error() { printf '%s error: %s\n' "$0" "$*" >&2 } errorAndUsage() { printf '%s error: %s\n%s\n' "$0" "$*" "$usageMessage" >&2 } resolutions='214 120 427 240 720 480 800 600 854 480 1280 720 1920 1080' prev='' next='' newResolution='' # Process options while getopts ':pn' opt; do case $opt in 'p' ) prev='y' ;; 'n' ) next='y' ;; '?' ) printf '%s\n' "$usageMessage" >&2 exit 1 esac done shift $((OPTIND - 1)) windowId="$(xdotool getwindowfocus)" currentWidth="$(xwininfo -id "$windowId" | grep 'Width:' | awk '{print $2}')" currentHeight="$(xwininfo -id "$windowId" | grep 'Height:' | awk '{print $2}')" currentResolution="${currentWidth} ${currentHeight}" if [ -n "$prev" ] && [ -n "$next" ]; then errorAndUsage '-p and -n can not be used together.' exit 1 elif [ -n "$next" ]; then newResolution="$(printf '%s' "$resolutions" | grep -A 1 "^${currentResolution}$" | tail -n 1)" # If resolution is the biggest, pick the smallest if [ "$newResolution" = "$currentResolution" ]; then newResolution="$(printf '%s' "$resolutions" | sed '1q')" fi elif [ -n "$prev" ]; then newResolution="$(printf '%s' "$resolutions" | grep -B 1 "^${currentResolution}$" | sed '1q')" # If resolution is the smallest, pick the biggest if [ "$newResolution" = "$currentResolution" ]; then newResolution="$(printf '%s' "$resolutions" | tail -n 1)" fi else resolutionCount="$(printf '%s' "$resolutions" | wc -l)" resolutionCount="$((resolutionCount + 1))" newResolution="$(printf '%s' "$resolutions" | dmenu -l "$resolutionCount" -p 'Resolution:')" fi xdotool getwindowfocus -- windowsize $newResolution