Manipulate window size in linux via compiled code?

跟風遠走 提交于 2019-12-14 04:01:21

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5424214/manipulate-window-size-in-linux-via-compiled-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!