Last active
November 26, 2023 05:09
sway-swap-workspaces.bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# All my gist code is licensed under the MIT license. | |
# Add this to your PATH | |
green='\033[0;32m' | |
red='\033[0;31m' | |
bold='\033[1m' | |
reset='\033[0m' | |
# 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=$(swaymsg -t get_workspaces | jq -r '.[] | select(.focused==true).name') | |
if [[ "$1" == "left" ]]; then | |
directional_argument="left" | |
wp2=$(swaymsg -t get_workspaces | jq -r '.[] | select(.focused==true).num-1') | |
else | |
directional_argument="right" | |
wp2=$(swaymsg -t get_workspaces | jq -r '.[] | select(.focused==true).num+1') | |
fi | |
elif [[ "$#" -eq 2 ]]; then | |
wp1=$1 | |
wp2=$2 | |
else | |
echo -e "${bold}Usage:${bold}${reset} ${green}sway-swap-workspaces${reset} <workspace1> <workspace2>" | |
echo -e " ${green}sway-swap-workspaces${reset} left|right" | |
exit 1 | |
fi | |
# If one of the workspaces doesn't exist, exit | |
check_workspace() { | |
wokspace=$(swaymsg -t get_workspaces | jq -r '.[] | select(.name=="'$1'")') | |
if [[ "$wokspace" == "" ]]; 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 | |
swaymsg "rename workspace $wp1 to __tmp__" | |
swaymsg "rename workspace $wp2 to $wp1" | |
swaymsg "rename workspace __tmp__ to $wp2" | |
# generate display name of swapped workspaces. | |
# - if one of the swapped workspaces is the current one, mark it with a | |
# "(current)" | |
currentwp=$(swaymsg -t get_workspaces | jq -r '.[] | select(.focused==true).name') | |
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}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment