Search code examples
linuxbashwindow-managers

Manipulate window size in linux via compiled code?


I wrote a couple of scripts to maximize a window to half the size of the screen (to make it easy to place windows side-by-side) using xrandr, grep, and wmctrl as follows:

#!/bin/bash

w=`xrandr 2> /dev/null | grep '*' | grep -Po '\d+(?=x)'`
h=`xrandr 2> /dev/null | grep '*' | grep -Po '(?<=x)\d+'`
wmctrl -r :ACTIVE: -b remove,maximized_horz,maximized,vert
wmctrl -r :ACTIVE: -e 0,0,0,$((w / 2)),$h

Is there a way to do this more natively? The script works well on my desktop, but on my laptop there's a half-second lag that is kind of annoying.


Solution

  • test code

    # w_h="$(print -- "1280x1024 0.0*" | awk '/.*\*$/{sub(/ .*$/, "");sub("x"," ");$1=$1/2 ;print}')"
    # w="${w_h% *}" ; h="${w_h#* }"
    

    actual code

    # awk matches only line ending with '*', remove everything from last space to EOL, replace X with " "
    # w_H looks like "640 1040", below splits on space char populating correct var
    w_h="$(xrandr | awk '/\*/{sub(/[0-9\.*\+]*$/, ""); sub("x", " "); $1=$1/2; print}')"
    w="${w_h% *}" ; h="${w_h#* }"
    wmctrl -r :ACTIVE: -b remove,maximized_horz,maximized,vert
    wmctrl -r :ACTIVE: -e 0,0,0,${w},${h}
    

    Note that I have done the div on W inside awk. Also, backticks are deprecated in posix shells. Make your life easier and use $() for command substituton ;-)

    I hope this helps.