aboutsummaryrefslogtreecommitdiff
path: root/setWindowSize.sh
blob: 522d5b459e89c842ca7f5900aa69422f4069fdd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/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