aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/tmux_sys_win_aware_navigation.sh
blob: 4fed4ae4452247e8443236af9de7c8452dde4df2 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash

#export PS4="\$LINENO: "     # Prints out the line number being executed by debug
#set -xv                     # Turn on debugging

exists_another_pane_in_direction() {
    local direction=$1
    local result=1 #false

    local paneList=$(
        tmux list-panes -F '#{pane_top} #{pane_left} #{?pane_active,active,} '
    )

    local paneActive=$(
        echo "$paneList" | \
        grep 'active' | \
        tr -s ' '
    )

    local paneNoActive=$(
        echo "$paneList" | \
        grep -v 'active'
    )

    local paneActiveTop="$(echo "$paneActive" | cut -d' ' -f1)"
    local paneActiveLeft="$(echo "$paneActive" | cut -d' ' -f2)"

    while read -r line; do
        local left="$(echo "$line" | cut -d' ' -f2 | tr -d "[:blank:]" | tr -d "\n")"
        local top="$(echo "$line" | cut -d' ' -f1)"

        if [ "$top" = '' ] && [ "$left" = '' ]; then
            break # There is not other window
        fi

        if [ "$direction" = 'up' ]
        then

            if [ "$top" -lt "$paneActiveTop" ] && [ "$left" -eq "$paneActiveLeft" ]
            then
                local result=0 #true
                break
            fi
        fi

        if [ "$direction" = 'right' ]
        then

            if [ "$left" -gt "$paneActiveLeft" ]
            then
                local result=0 #true
                break
            fi
        fi

        if [ "$direction" = 'down' ]
        then
            if [ "$top" -gt "$paneActiveTop" ] && [ "$left" -eq "$paneActiveLeft" ]
            then
                local result=0 #true
                break
            fi
        fi

        if [ "$direction" = 'left' ]
        then
            if [ "$left" -lt "$paneActiveLeft" ]
            then
                local result=0 #true
                break
            fi
        fi
    done <<< "$paneNoActive"

    echo "$result"
}

dir=$1 #direction
mode=$2 #if value  = "tmux-only", navigation shortcuts won't apply in system's window scope
        # ....so this script won't be used and navigation will be handled by tmux only

if [ "$dir" = "up" ]
then
    tmuxArg="U"
elif [ "$dir" = "right" ]
then
    tmuxArg="R"
elif [ "$dir" = "down" ]
then
    tmuxArg="D"
elif [ "$dir" = "left" ]
then
    tmuxArg="L"
else
    echo -e "Invalid dirrection parameter supplied\nUsage: tmux_sys_win_aware_navigation up|right|down|left"
    exit 1
fi

if [ "$mode" = "tmux-only" ]
then
    tmux select-pane -$tmuxArg
    exit 0
fi

exists=$(exists_another_pane_in_direction $dir)


if [ $exists = '0' ]
then
    tmux select-pane -$tmuxArg
else
    i3-msg -q focus $dir
fi
exit 0
#set +xv #end of debug