wms/wms_shepperd.sh
2024-01-21 00:05:04 -03:00

95 lines
1.4 KiB
Bash
Executable file

#!/bin/sh
## sort windows by herds/flock ##
. /tmp/wms_var
FLAG=$1
# focused window id.
FW=$(pfw)
# all maped windows.
AMW=$(lsw)
# names file path.
NAMES="$HOME/.config/wms/wms_names"
usage() {
cat<<EOF
usage:
wms_shepperd.sh [ -a, -r, -t ]
-a) add
-A) add all
-d) delete
-D) delete all
-t) togle
EOF
}
# create atom in focused window
add_focused() {
name="$($XMENU < $NAMES)"
if [ -n "$name" ]; then
atomx WM_HERD="$name $FW" $FW
else
exit 0
fi
}
# create atom in all maped windows
add_all() {
name="$($XMENU < $NAMES)"
for wid in $AMW; do
if [ -n "$name" ]; then
atomx WM_HERD="$name $wid" $wid
else
exit 0
fi
done
}
# delete atom from focused window
del_focused() {
atomx -d WM_HERD $FW
}
del_all() {
atomx -d WM_HERD $AMW
}
# togle groups
togle() {
# herds.
herds=$(atomx WM_HERD $(lsw -a) | cut -d ' ' -f1 | sort -u)
# check if any herd exist.
if [ -n "$herds" ]; then
# select herd target.
h_target=$(printf "$herds" | $XMENU)
# windwos to map.
wind_to_map=$(atomx WM_HERD $(lsw -u) | grep $h_target | cut -d ' ' -f2)
# windows to unmap.
wind_to_unmap=$(atomx WM_HERD $(lsw) | grep -v $h_target | cut -d ' ' -f2)
# do it.
mapw -m $wind_to_map & mapw -u $wind_to_unmap
fi
}
case $FLAG in
-a)
add_focused
;;
-A)
add_all
;;
-d)
del_focused
;;
-D)
del_all
;;
-t)
togle
;;
*)
usage # self-explained
;;
esac