Reputation: 29
I have a bash script that calculates the number of Compiz Viewports then dependent on that number defines variable values for each viewport to allow jumping to that viewport.
wmctrl -d
0 * DG: 19200x2160 VP: 0,0 WA: 0,38 3840x2084 Workspace 1
This output tells me the total viewport area is 19200
wmctrl -d | awk '{print $4}' | sed -e 's/x..*//'
19200
and each viewport is 3840 in size
wmctrl -d | awk '{print $9}' | sed -e 's/x..*//'
3840
therefore the total number of viewports is 19200/3840 or in this instance 5
what I now want to do is populate a number of variables (maximum total number of viewports) with the value of that viewport location. In the example above there are 5 viewports so:-
VIEWPORT(1 2 3 4 5)
3840, 7680, 11520, 15360, 19200
then using
wmctrl -o $VIEWPORT(x),0
(Above edited to show that $VIEWPORT(x) is actually a variable, I didn't make it clear originally, so the actual value of say $VIEWPORT3 would be 11520, sorry my bad in the original question)
takes us to that viewport.
The thing I'm struggling with is that as the number of variables required varies on the the number of viewports identified in the beginning.
I've read a bit about arrays but can't see an easy way to use an array to make this work (my lack of familiarity).
So any suggestions regarding how to do this would be appreciated.
Upvotes: 0
Views: 85
Reputation: 124
Bash arrays aren't too complex. If you want to have the variables for use later;
#Total Viewport Area
totview=$(wmctrl -d | awk '{print $4}' | sed -e 's/x..*//')
#Each Viewport size
viewsize=$(wmctrl -d | awk '{print $9}' | sed -e 's/x..*//')
#Loop as many times as totview/viewsize equals
for ((i=1;$i<=$(($totview / $viewsize));i++)); do
array[$i]=$(($viewsize * $i)) #sets the array entry at index $i to the next viewsize. Remember that $i increases by one every loop
done
Now you'll have a variable number of variables, each named array[#]. In your example, you'd have "array[1]" through "array[5]", with the numbers 3840, 7680, 11520, 15360 and 19200. Later on, when you want to use the variables, you can do:
wmctrl -o VIEWPORT(${array[$x]}),0
to get a specific viewport, with $x being a number from one to five. You can fetch the number of indexes in array with ${#array[*]}
. In this case, echo ${#array[*]}
would print 5.
Upvotes: 0
Reputation: 930
Maybe something like:
while read -r size
do
echo "wmctrl -o VIEWPORT($size),0"
done< <(wmctrl -d | awk -F'[ x]' '{for(i = $10; i <= $4; i += $10)print i}')
Remove the echo and quotes once your happy
Upvotes: 1