Skip to content

Instantly share code, notes, and snippets.

@BelkaDev
Last active April 18, 2020 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BelkaDev/359c84bce3562b76611fa6f087385f34 to your computer and use it in GitHub Desktop.
Save BelkaDev/359c84bce3562b76611fa6f087385f34 to your computer and use it in GitHub Desktop.
dock a terminal (or any window) to any parent window under x environement
#!/bin/bash
# requires: bspwm,xdotool,wmctrl,wmutils...
# usage: attach.sh [n]
# n is the size of the docked window, 1 equals 100% the parent size, 2 is 50% and so on..
unset x y w h
# kill old instance of the script to save resources: only one terminal allowed
script_name=${BASH_SOURCE[0]}
for pid in $(pidof -x $script_name); do
if [ $pid != $$ ]; then
kill -9 $pid
if [[ $(xdotool search --classname "status") ]]; then
xdotool search --classname "status" windowkill
fi
fi
done
function refresh_parent_geometry(){
eval $(xwininfo -id "$parent" |
sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
-e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
-e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
-e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p")
n="$1"
(( h/= $n ))
(( y= y+($n-1)*h ))
g="0,$x,$y,$w,$h"
}
parent=$(bspc query -N -n focused)
# TERMINAL SIZE: 1 equals 100% the window
n=4
if [ ! -z "$1" ]; then
n="$1"
fi
refresh_parent_geometry $n
bspc rule -a URxvt:status state=floating follow=on focus=on layer=above
urxvt -cd $(thunar_lastdir) -name status &
eval $(bspc rule -a URxvt:status state=floating follow=on focus=on layer=above)
PID=$!
while [ "$WID" == "" ]; do
WID=$(xdo id -p $!)
done
wmctrl -i -r $WID -e $g
bspc node $parent -f
bspc subscribe all | while read -a log ; do
# update geometry
if [[ ${log[0]} == "node_geometry" ]]; then
if [[ ${log[3]} == $parent ]]; then
refresh_parent_geometry $n
wmctrl -i -r $WID -e $g
fi
fi
# detach term if parent floats #
# if [[ ${log[0]} == "node_state" ]]; then
# if [[ ${log[3]} == $parent ]]; then
# if [[ ${log[4]} == "floating" ]]; then
# bspc node $WID -t tiled
# fi
# fi
# fi
# xdotool search --name status
# fix term geometry (unfinished: allow vertical resize)
if [[ ${log[0]} == "node_geometry" ]]; then
if [[ $(tr "[:upper:]" "[:lower:]" <<<"${log[3]}") = $(tr "[:upper:]" "[:lower:]" <<<"$WID") ]]; then
refresh_parent_geometry $n
wmctrl -i -r $WID -e $g
fi
fi
# detach if term changes state
if [[ ${log[0]} == "node_state" ]]; then
if [[ $(tr "[:upper:]" "[:lower:]" <<<"${log[3]}") = $(tr "[:upper:]" "[:lower:]" <<<"$WID") ]]; then
exit
fi
fi
if [[ ${log[0]} == "node_transfer" ]]; then
if [[ ${log[3]} == $parent ]]; then
bspc node $WID -d ${log[5]}
fi
fi
# destroy term once parent closes
if [[ ${log[0]} == "node_remove" ]]; then
if [[ ${log[3]} == $parent ]]; then
kill $PID
unset WID
exit 0
fi
fi
# end script when term is closed
if [[ ${log[0]} == "node_remove" ]]; then
if [[ ${log[3]} == $WID ]]; then
exit 0
fi
fi
done
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment