Skip to content

Instantly share code, notes, and snippets.

@unhammer
Last active February 15, 2017 12:16
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 unhammer/975e062eafda946be99c to your computer and use it in GitHub Desktop.
Save unhammer/975e062eafda946be99c to your computer and use it in GitHub Desktop.
requires wmctrl, xrandr. I bind these to super-left / super-right to make windows cover 50% or fullscreen; and DTRT with multiple monitors
#!/bin/bash
set -e -u
declare -ir slack=6
declare cur_winid
if [[ $# -eq 1 ]]; then
cur_winid=$1
else
cur_winid="$(current-winid)"
fi
# Important: sorted s.t. largest width is first!
declare -a dims=($(xrandr |gawk '/ connected /{print gensub(/.* ([0-9]+x[0-9]+\+[0-9]+\+[0-9]+) \(.*/,"\\1","1")}' | sort -nr))
declare -a widths=(${dims[*]%x*})
declare -a x_offsets=(${dims[*]%+*}); x_offsets=(${x_offsets[*]#*+})
declare -i cur_x cur_w
cur_x=$(wmctrl -l -G |awk -v id="${cur_winid}" '$1==id{print $3}')
cur_w=$(wmctrl -l -G |awk -v id="${cur_winid}" '$1==id{print $5}')
declare -i w x half
for i in "${!widths[@]}"; do
w=${widths[$i]}
x=${x_offsets[$i]}
half=$(( w/2 ))
if [[ ${cur_w} -eq $w ]]; then
break
fi
if [[ ${cur_x} -gt $(( x + slack )) ]]; then
break
fi
done
declare -i half_offset
half_offset=$(( x + half - cur_x )); half_offset=${half_offset#-} # abs
if [[ ${half_offset} -le ${slack} || ${cur_x} -gt $(( x + w )) ]]; then
wmctrl -ir "${cur_winid}" -b add,maximized_horz
wmctrl -ir "${cur_winid}" -e "0,$x,0,$w,-1"
wmctrl -ir "${cur_winid}" -b add,maximized_vert
else
wmctrl -ir "${cur_winid}" -b remove,maximized_horz
wmctrl -ir "${cur_winid}" -e "0,$x,0,${half},-1"
wmctrl -ir "${cur_winid}" -b add,maximized_vert
fi
#!/bin/bash
set -e -u
declare -ir slack=6
declare cur_winid
if [[ $# -eq 1 ]]; then
cur_winid=$1
else
cur_winid="$(current-winid)"
fi
# Important: sorted s.t. largest width is last!
declare -a dims=($(xrandr |gawk '/ connected /{print gensub(/.* ([0-9]+x[0-9]+\+[0-9]+\+[0-9]+) \(.*/,"\\1","1")}' | sort -n))
declare -a widths=(${dims[*]%x*})
declare -a x_offsets=(${dims[*]%+*}); x_offsets=(${x_offsets[*]#*+})
declare -i cur_x cur_w
cur_x=$(wmctrl -l -G |awk -v id="${cur_winid}" '$1==id{print $3}')
cur_w=$(wmctrl -l -G |awk -v id="${cur_winid}" '$1==id{print $5}')
declare -i w x half
for i in "${!widths[@]}"; do
w=${widths[$i]}
x=${x_offsets[$i]}
half=$(( w/2 ))
if [[ ${cur_w} -eq $w ]]; then
break
fi
if [[ ${cur_x} -lt $(( half - slack )) ]]; then
break
fi
done
declare -i half_offset
half_offset=$(( w - half - cur_w )); half_offset=${half_offset#-} # abs
if [[ ${half_offset} -le ${slack} || ${cur_x} -lt $x ]]; then
wmctrl -ir "${cur_winid}" -b add,maximized_horz
wmctrl -ir "${cur_winid}" -e "0,$x,0,$w,-1"
wmctrl -ir "${cur_winid}" -b add,maximized_vert
else
wmctrl -ir "${cur_winid}" -b remove,maximized_horz
wmctrl -ir "${cur_winid}" -e "0,$(( x + half )),0,${half},-1"
wmctrl -ir "${cur_winid}" -b add,maximized_vert
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment