aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/window-switcher
blob: a184baaebb7f3b97c19765c0e3dfb158de95cbe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/sh -u
# https://git.suckless.org/sites/file/tools.suckless.org/dmenu/scripts/switch.html

windows=$(wmctrl -xl |
	tr --squeeze-repeats '[:blank:]' |
	tr '[:upper:]' '[:lower:]' |
	cut --delimiter ' ' --fields 3-3,5- |
	sed 's/^[a-zA-Z0-9-]*\.//' |
	sort |
	uniq)

windows=$(printf "%s" "$windows" |
	awk --assign list="$windows" \
		'{cur=length($1); printf $1; for(i=0; i < list - cur + 1; i++) printf " "; $1 = ""; printf "%s\n", $0}' 2> /dev/null)

selection=$(
	printf "%s" "$windows" | dmenu -nhf '#cf71e0' -sf '#cf71e0' -f -i -b -p ⠀:::⠀windows⠀:::
	[ "$?" = 127 ] && kill -15 "$$" && "$0"
)

target=$(printf "%s" "$selection" | tr --squeeze-repeats '[:blank:]' | cut --delimiter ' ' --fields 2-)

[ -n "$target" ] && wmctrl -a "$target"