Files
dotfiles/.config/swayprop/swayprop
2025-06-26 19:01:39 +02:00

44 lines
1.3 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
function prop () {
# Needs to be declared before its assigned or I cannot grab the exit code
# later. Fun race condition <.<
local selection
local window
# Find all visible windows and get their geometry, then
# format slurp-friendly, then
# run slurp!
selection=$(swaymsg -t get_tree | jq '.. | select(.visible?==true) | .rect | [ .x,.y,.width,.height ]' \
| jq -r '"\(.[0]),\(.[1]) \(.[2])x\(.[3])"' \
| slurp -r)
local ret=$?
[ $ret -ne 0 ] && exit $ret
local x=$(echo "$selection" | awk -F'[, x]' '{print $1}')
local y=$(echo "$selection" | awk -F'[, x]' '{print $2}')
local w=$(echo "$selection" | awk -F'[, x]' '{print $3}')
local h=$(echo "$selection" | awk -F'[, x]' '{print $4}')
# Find the window matching the selection.
window=$(swaymsg -t get_tree | \
jq ".. | select((.rect?.x==$x) and (.rect?.y==$y) and (.rect?.width==$w) and (.rect?.height=$h) and (.visible?==true))")
[ -z "$window" ] && exit 1
# Pretty print, since capturing the output of `swaymsg` makes it uglify.
echo $window | jq
}
function check_dependencies () {
local ret=0
for cmd in "$@"; do
! [ $(command -v $cmd) ] && echo "Missing dependency: $cmd." && ret=1
done
return $ret
}
( check_dependencies "jq" "slurp" "swaymsg" ) || exit 1
prop