-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch-to
executable file
·85 lines (73 loc) · 1.7 KB
/
switch-to
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/bash
usage() {
printf '%s\n' "Usage: switch-to [-hcn] open_cmd
Switch focus to a particular X window, or open a new one if no matches are found.
where:
-h, --help - show this help text
-n, --name - the window title of the window to switch focus to
-c, --class - the WM_CLASS of the window to switch focus to"
}
main() {
# Switch focus using either window name or class
if [[ -n "${WINDOW_NAME}" ]]; then
wmctrl -a "${WINDOW_NAME}"
elif [[ -n "${WM_CLASS}" ]]; then
wmctrl -xa "${WM_CLASS}"
fi
# If it didn't switch, use the given cmd to open a new window
if (( "$?" != 0 )); then
${OPEN_CMD}
fi
}
# Option parsing
declare PARAMS=''
declare WINDOW_NAME
declare WM_CLASS
declare OPEN_CMD
while (( "$#" )); do
case $1 in
-h|--help) # display help message
usage
exit 1
;;
-n|--name) # set window name
WINDOW_NAME="$2"
shift 2
;;
-c|--class) # set WM_CLASS
WM_CLASS="$2"
shift 2
;;
--) # End argument parsing
shift
break
;;
-*|--*) # unsupported flags
echo "Unsupported flag: $1" >&2
usage
exit 1
;;
*) # preserve positional arguments
PARAMS="${PARAMS} '$1'"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "${PARAMS}"
# parse positional args
OPEN_CMD="$1"
# validate parameters
if [[ -z "${OPEN_CMD}" ]]; then
echo "Error: missing 'open_cmd' positional parameter" 1>&2
usage
exit 1
fi
if [[ -n "${WINDOW_NAME}" && -n "${WM_CLASS}" ]]; then
echo "Error: cannot set both '--name' and '--class' flags" 1>&2
usage
exit 1
fi
# Freeze configuration flags
readonly WINDOW_NAME WM_CLASS OPEN_CMD
main