Files
dotfiles/.config/i3/multi-monitor-workspaces.sh
Julian Prein 616298df72 i3:multi-monitor: Send only one i3-msg command
Moving and then switching in a separate command flickered sometimes.
2025-06-06 12:03:41 +02:00

57 lines
1.1 KiB
Bash
Executable File

#!/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
cmd=
[ -z "$move" ] || cmd="move container to workspace $workspace"
[ -z "$switch" ] || cmd="$cmd${cmd:+; }workspace $workspace"
i3-msg "$cmd"