forked from jytrowbridge/i3-next-available-workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next_available_ws.sh
executable file
·52 lines (44 loc) · 1.29 KB
/
next_available_ws.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
i3config="$HOME/.config/i3/config"
# Array for i3config Workspace names
declare -a WSpaces=("")
getWorkspaceConfigNames () {
local ws_num=$1
local re1="^set[[:space:]]+.ws[0-9]+[[:space:]]+[\"\']([0-9]+)(:([[:alnum:][:space:]]+))?[\"\'][[:space:]]*$"
while read -r line
do
[[ $line =~ $re1 ]] && WSpaces+=("${BASH_REMATCH[1]}${BASH_REMATCH[2]}")
done < $i3config
[[ -v WSpaces[$ws_num] ]] && echo ${WSpaces[$ws_num]} || echo $ws_num
}
# find free workspace
getWorkspaceFree() {
local pcount=0
for ws_num in $(i3-msg -t get_workspaces | jq '.[] | (.num)')
do
[[ $ws_num -gt 0 ]] && (( pcount++ ))
# if first call, $ws_num should = 0, if not, then $pcount == 0,
# and then this is true, and we return 0
if [[ $pcount -ne $ws_num ]] ; then
getWorkspaceConfigNames $pcount
return
fi
done
# Deals with case of full pack. Add another workspace at end.
getWorkspaceConfigNames $(( pcount + 1 ))
}
# if -m flag is passed, move current container to new workspace
moveContainer () {
local ws=$1
i3-msg move container to workspace $ws
i3-msg workspace $ws
}
WS_free=$(getWorkspaceFree)
# check for move container flag
_move=''
getopts 'm' opt
case $opt in
m) moveContainer $WS_free ;;
?) i3-msg workspace $WS_free ;;
*) echo 'Error in command line parsing' >&2; exit 1 ;;
esac