-
-
Save haselwarter/5e1b16bf6288a4a0376ca6480da4d6d6 to your computer and use it in GitHub Desktop.
#!/usr/bin/bash | |
# Goal: move workspace $1 to the currently focused output, move the workspace on the current output | |
# to the output where $1 was. | |
# The workspace we want to end up on | |
dest_ws=$1 | |
# The output where the destination ws resides | |
output_of_dest_ws=$(i3-msg -t get_workspaces | jq .[] | jq -r 'select(.name == "'$dest_ws'").output') | |
# The currently focused output | |
current_output=$(i3-msg -t get_workspaces | jq .[] | jq -r "select(.focused == true).output") | |
# The workspace on the current output | |
current_ws=$(i3-msg -t get_workspaces | jq .[] | jq -r "select(.focused == true).name") | |
# Send away the current workspace | |
i3-msg move workspace to output $output_of_dest_ws | |
# Select the destination workspace | |
i3-msg workspace $dest_ws | |
# Move the destination workspace to what was the current output in the beginning | |
i3-msg move workspace to output $current_output | |
# Re-activate the destination workspace | |
# sleep 0.2 | |
i3-msg workspace $dest_ws |
that python script takes ~300ms to execute on my machine while my bash version takes ~40ms. Mine also does smarter tests, like why is the python script moving the target ws and judging by the result of the command if it was possible, it is easy to do without issuing a command. The bash script also has no dependencies, besides bash and i3-msg.
@budRich can you help me convert my python script to bash too, it is for following case,
i have two monitors, and when i press a keybind
i want to move workspace on unfocused output to focused one and
the workspace on focused output to unfoucsed one
like why is the python script moving the target ws and judging by the result of the command if it was possible
i gave my script for swapping, it's not same as yours, i was just giving a example of how i use the ipc-python
this is the script which works same as your one, ans yes it is slow than the bash one, that's why i want to convert my one to the bash one
#!/usr/bin/env python3
# Switches workspaces in i3 like xmonad.
# Always bring the requested workspace to the
# focused monitor ("output" in i3's terminology).
# If the requested workspace is on another monitor,
# the current workspace and requested workspace
# will swap positions with each other.
import i3ipc
import sys
# Create the Connection object that can be used to send commands and subscribe
# to events.
i3 = i3ipc.Connection()
ws_target = None
target = None # target workspace
focused = None # currently focused workspace
ws_target=sys.argv[1] # $1 == target workspace
workspaces = i3.get_workspaces()
for workspace in workspaces:
if workspace.name == ws_target:
target = workspace
if workspace.visible and workspace.focused:
focused = workspace
if target and focused:
break
if target == None:
# target doesn't exist
command1 = "workspace " + ws_target
command1_ret = i3.command(command1)
elif target.output != focused.output:
# only swap monitors if the workspaces are on different outputs
command1 = "[workspace=" + target.name + "] move workspace to output " + focused.output
command2 = "[workspace=" + focused.name + "] move workspace to output " + target.output
command1_ret = i3.command(command1)
command2_ret = i3.command(command2)
command3 = "workspace " + focused.name
command4 = "workspace " + target.name
command3_ret = i3.command(command3)
command4_ret = i3.command(command4)
else:
# target workspace on same monitor
command1 = "workspace --no-auto-back-and-forth " + target.name
command1_ret = i3.command(command1)
@totoro-ghost Yeah I think i can brew up that monitor ws swapper thing in bash. Sounds useful and more sober then the xmonad workspace summoning shenanigans :)
so i found this https://github.com/altdesktop/i3ipc-python and this works perfectly for one of my scripts, this swapping one
i got no error, i ran it nearly 50 times, in dfferent cases, so you can make something like this for your case.