-
Notifications
You must be signed in to change notification settings - Fork 0
/
wm-swap-workspaces
executable file
·99 lines (86 loc) · 2.75 KB
/
wm-swap-workspaces
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env bash
# SCRIPT_DIR is set to the directory where this script is localed. It it was
# run by a symlink, it will be resolved to the directory of the target script
# (that is, the actual script directory).
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [[ -L "${BASH_SOURCE[0]}" ]]; then
SCRIPT_DIR="$(readlink "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$( cd "$( dirname "${SCRIPT_DIR}" )" >/dev/null 2>&1 && pwd )"
fi
# get platform info, populates 'platform' and 'apple_silicon' variables
source "${SCRIPT_DIR}"/lib/platform.sh
# wm-swap-workspaces
green='\033[0;32m'
red='\033[0;31m'
bold='\033[1m'
reset='\033[0m'
function usage () {
echo -e "${bold}Usage:${bold}${reset} ${green}wm-swap-workspaces${reset} <workspace1> <workspace2>"
echo -e " ${green}wm-swap-workspaces${reset} left|right"
}
# swaymsg, fallback to i3-msg
source "${SCRIPT_DIR}"/lib/swaymsg.sh
# print usage on -h/--help
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage
exit 0
fi
function get-current-workspace() {
if [[ "$platform" == "macos" ]]; then
yabai -m query --spaces --space | jq '.index' --monochrome-output
else
swaymsg -t get_workspaces | jq -r '.[] | select(.focused==true).name'
fi
}
# if called with "left" or "right", swap current workspace with the one in that
# direction
directional_argument=""
if [[ "$#" -eq 1 && ("$1" == "left" || "$1" == "right") ]]; then
wp1=$(get-current-workspace)
if [[ "$1" == "left" ]]; then
directional_argument="left"
wp2=$((wp1-1))
else
directional_argument="right"
wp2=$((wp1+1))
fi
elif [[ "$#" -eq 2 ]]; then
wp1=$1
wp2=$2
else
usage
exit 1
fi
# If one of the workspaces doesn't exist, exit
check_workspace() {
if ! wm-workspace-exists "$1" > /dev/null; then
if [[ "$directional_argument" != "" ]]; then
echo -e "${red}${bold}Error:${reset} Already on ${directional_argument}most workspace."
else
echo -e "${red}${bold}Error:${reset} Workspace $1 doesn't exist."
fi
exit 1
fi
}
check_workspace "$wp1"
check_workspace "$wp2"
# swap workspaces
if [[ "$platform" == "macos" ]]; then
yabai -m space "$wp1" --swap "$wp2"
else
swaymsg "rename workspace $wp1 to __tmp__"
swaymsg "rename workspace $wp2 to $wp1"
swaymsg "rename workspace __tmp__ to $wp2"
fi
# generate display name of swapped workspaces.
# - if one of the swapped workspaces is the current one, mark it with a
# "(current)" label
currentwp=$(get-current-workspace)
wp1_display="$wp1"
wp2_display="$wp2"
if [[ "$currentwp" == "$wp1" ]]; then
wp1_display="$wp1 (current)"
elif [[ "$currentwp" == "$wp2" ]]; then
wp2_display="$wp2 (current)"
fi
echo -e "${green}Swapped workspaces${reset} ${bold}${wp1_display}${reset} ${green}and${reset} ${bold}${wp2_display}${reset}."