add swayprop

This commit is contained in:
2025-06-26 19:01:39 +02:00
parent 7829af72ac
commit 8741765c2d
9 changed files with 848 additions and 0 deletions

43
.config/swayprop/swayprop Executable file
View File

@@ -0,0 +1,43 @@
#!/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