i3: Simulate monitor-independent workspaces

Often when I want to open a new workspace, I look at my bar to see which
workspaces are still free. This does not work sometimes when having
multiple monitors connected, as workspace numbers are unique between all
outputs.

Add a script that handles workspace switching/moving by prepending the
index with the index of the current monitor.
This commit is contained in:
2025-06-05 17:38:57 +02:00
parent e121743d8a
commit a67f8c9ec6
2 changed files with 84 additions and 42 deletions

View File

@@ -0,0 +1,54 @@
#!/bin/sh
# SPDX-License-Identifier: MIT
# Copyright (c) 2025 Julian Prein
#
# Script to simulate output-independent workspaces. Can be used to switch and/or
# move a container to the workspace.
set -eu
switch=
move=
usage() {
printf >&2 "Usage: %s [-s] [-m] <index>\n" "$0"
exit "${1:-0}"
}
while getopts "sm" flag; do
case $flag in
s) switch=1;;
m) move=1;;
*) usage 1;;
esac
done
shift $((OPTIND - 1))
[ $# -gt 0 ] || usage
outputs="$(i3-msg -t get_outputs | jq -r '.[] | select(.active).name')"
num_outs="$(printf "%s\n" "$outputs" | wc -l)"
if [ "$num_outs" -lt 2 ]; then
# only one monitor
workspace="$1"
else
name="$(i3-msg -t get_tree \
| jq -r '.. | objects | select(.focused).output')"
num="$(printf "%s\n" "$outputs" \
| grep -Fxn "$name" \
| cut -d: -f1)"
num="$((num - 1))"
# Omit the number on the first monitor
[ "$num" -gt 0 ] || num=
workspace="$num$1"
fi
if [ -z "$switch" ] && [ -z "$move" ]; then
printf "%s\n" "$workspace"
exit 0
fi
[ -z "$move" ] || i3-msg move container to workspace "$workspace"
[ -z "$switch" ] || i3-msg workspace "$workspace"