Auto-adapts sway workspace routing to however many monitors are connected (laptop vs. desktop) instead of hardcoding output names.
33 lines
899 B
Bash
Executable File
33 lines
899 B
Bash
Executable File
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
slot=${1:-}
|
|
action=${2:-select}
|
|
|
|
if [[ ! "$slot" =~ ^([1-9]|10)$ ]]; then
|
|
printf 'Usage: %s <1-10> [select|move]\n' "${0##*/}" >&2
|
|
exit 2
|
|
fi
|
|
|
|
# Outputs are ordered left-to-right by position, not by name, so this works
|
|
# unmodified whether there is one output (laptop) or several (desktop):
|
|
# the focused output's rank in that order selects its block of 10 workspaces.
|
|
outputs=$(swaymsg -t get_outputs -r | jq -c 'sort_by(.rect.x)')
|
|
index=$(jq -r '[.[] | .focused] | index(true)' <<<"$outputs")
|
|
if [[ "$index" == null ]]; then
|
|
printf 'No focused output found\n' >&2
|
|
exit 1
|
|
fi
|
|
workspace=$((slot + index * 10))
|
|
|
|
case "$action" in
|
|
select) swaymsg workspace number "$workspace" >/dev/null ;;
|
|
move)
|
|
swaymsg move container to workspace number "$workspace" >/dev/null
|
|
;;
|
|
*)
|
|
printf 'Unknown action: %s\n' "$action" >&2
|
|
exit 2
|
|
;;
|
|
esac
|