View difference between Paste ID: eu4bW1RH and 433JSNmQ
SHOW: | | - or go back to the newest paste.
1
#!/bin/bash
2
3
#
4
#
5
#
6
# Written by reddit user u/casualContributorNZ
7
# Available under the GLWT Public Licence:                                            
8
# https://github.com/me-shaon/GLWTPL/blob/master/LICENSE
9
#
10
#
11
# This script emulates the 'show desktop' functionality in i3wm
12
#
13
#
14
# First finds the currently active workspace using i3-msg and jq
15
#
16
#
17
# Then using i3-msg and (ugly) jq will determine whether there are any windows open on the
18
# current work space. This is done by determining the number of nodes open, so if there are
19
# some extra nodes open by default (ie. you are running something which automatically has a
20
# node within a workspace) then you will need to change the comparator in the first if statement
21
#
22
#
23
# If the workspace is empty, you will be bounced back to your most recently preiously accessed
24
# workspace with the i3 back_and_forth builtin.                                                  
25
# This means you can get some slightly weird behaviour if you close all of the windows in your 
26
# current workspace and then run this, because the workspace you will be taken to has no/minimal
27
# pertinence to the current one.
28
#
29
#
30
# If you are on a non-empty workspace then you will be taken to the lowest-number free workspace. 
31
# Note that if you have workspaces 1-10 already open then it will take you to workspace 11.
32
#       
33
# 
34
# You can also pass a function to then execute on the new workspace. I suppose that actually this 
35
# is kinda pointless as it is just doing:
36
# `i3-msg workspace ##; exec $1`
37
# Which is equivalent to:
38
# ./thisScript.sh; $1
39
# So therefore then only saving is a semi-colon. Efficiency.
40
#
41
# If you run this script with an argument in an empty workspace the argument will be ignored, 
42
# thus you can run './thisScript firefox' close firefox, and then re-run it, and it will take you
43
# back to where you were WITHOUT opening firefox when you get there
44
#
45
46
47
48
currentWS=$(i3-msg -t get_workspaces | jq '.[] | select(.focused==true).num')
49
50
isEmpty=$(i3-msg -t get_tree | jq --arg currWS "$currentWS" '.nodes | .[] | .nodes | .[] |
51
select(.type=="con") | .nodes | .[] | select(.type=="workspace") | select(.name==$currWS) |
52
.nodes | length')
53
54
55
if [ $isEmpty -eq 0 ]
56
        then
57
                i3-msg workspace back_and_forth
58
        else
59
                readarray -t activeWorkspaces < <(i3-msg -t get_workspaces | jq 'map(.num)' | tr -dc '[:alnum:]\n\r' | sort -g)
60
61
                number=$(echo "${activeWorkspaces[@]}" | awk -v RS='\\s+' '{ a[$1] } END { for(i=1; i in a; ++i);
62
                print i }')
63
64
                if [ $# -eq 0 ]
65
                        then
66
                                i3-msg workspace $number
67
                        else
68
                                i3-msg workspace $number; exec $1
69
                fi
70
71
fi