Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- #
- #
- # Written by reddit user u/casualContributorNZ
- # Available under the GLWT Public Licence:
- # https://github.com/me-shaon/GLWTPL/blob/master/LICENSE
- #
- #
- # This script emulates the 'show desktop' functionality in i3wm
- #
- #
- # First finds the currently active workspace using i3-msg and jq
- #
- #
- # Then using i3-msg and (ugly) jq will determine whether there are any windows open on the
- # current work space. This is done by determining the number of nodes open, so if there are
- # some extra nodes open by default (ie. you are running something which automatically has a
- # node within a workspace) then you will need to change the comparator in the first if statement
- #
- #
- # If the workspace is empty, you will be bounced back to your most recently preiously accessed
- # workspace with the i3 back_and_forth builtin.
- # This means you can get some slightly weird behaviour if you close all of the windows in your
- # current workspace and then run this, because the workspace you will be taken to has no/minimal
- # pertinence to the current one.
- #
- #
- # If you are on a non-empty workspace then you will be taken to the lowest-number free workspace.
- # Note that if you have workspaces 1-10 already open then it will take you to workspace 11.
- #
- #
- # You can also pass a function to then execute on the new workspace. I suppose that actually this
- # is kinda pointless as it is just doing:
- # `i3-msg workspace ##; exec $1`
- # Which is equivalent to:
- # ./thisScript.sh; $1
- # So therefore then only saving is a semi-colon. Efficiency.
- #
- # If you run this script with an argument in an empty workspace the argument will be ignored,
- # thus you can run './thisScript firefox' close firefox, and then re-run it, and it will take you
- # back to where you were WITHOUT opening firefox when you get there
- #
- currentWS=$(i3-msg -t get_workspaces | jq '.[] | select(.focused==true).num')
- isEmpty=$(i3-msg -t get_tree | jq --arg currWS "$currentWS" '.nodes | .[] | .nodes | .[] |
- select(.type=="con") | .nodes | .[] | select(.type=="workspace") | select(.name==$currWS) |
- .nodes | length')
- if [ $isEmpty -eq 0 ]
- then
- i3-msg workspace back_and_forth
- else
- readarray -t activeWorkspaces < <(i3-msg -t get_workspaces | jq 'map(.num)' | tr -dc '[:alnum:]\n\r' | sort -g)
- number=$(echo "${activeWorkspaces[@]}" | awk -v RS='\\s+' '{ a[$1] } END { for(i=1; i in a; ++i);
- print i }')
- if [ $# -eq 0 ]
- then
- i3-msg workspace $number
- else
- i3-msg workspace $number; exec $1
- fi
- fi
Add Comment
Please, Sign In to add comment