summaryrefslogtreecommitdiff
path: root/fish/conf.d/tmux.fish
blob: c8c9906a6138daa1879b9c449020d25c26b88eb0 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
set -gx TMUX_FZF_GIT_INDEX ~/git/.index

function _tmux::cleanup_default
    tmux list-sessions | string match -r '^T.*: ' | string match -v -r attached | string split ':' | while read -l s
        echo "Killing $s"
        tmux kill-session -t "$s"
    end
end

function _tmux::connect_command
    set -l server_or_pod $argv[1]
    if test -z "$TMUX_KEXEC"
        echo "ssh -A -t $server_or_pod"
    else
        echo "kubectl exec -it $server_or_pod -- /bin/bash"
    end
end

function tmux::new
    set -l session $argv[1]
    _tmux::cleanup_default
    if test -z "$session"
        tmux::new (string join "" T (date +%s))
    else
        tmux new-session -d -s $session
        tmux -2 attach-session -t $session || tmux -2 switch-client -t $session
    end
end

function tmux::project
    if test (count $argv) -eq 0
        set -l git_root (basename (git rev-parse --show-toplevel 2>/dev/null))
        if test -n "$git_root"
            tmux::attach (basename $git_root)
            return
        else
            set -l filter .
        end
    else
        set -l filter $argv[1]
    end

    set -l git_dir ~/git
    set -l tmp_dir $TMPUTILS_DIR

    set -l index_age 0
    if test -f $TMUX_FZF_GIT_INDEX
        if test (uname) = Darwin
            set $TMUX_FZF_GIT_INDEX_age (math (date +%s) - (stat -f %m $TMUX_FZF_GIT_INDEX))
        else
            set $TMUX_FZF_GIT_INDEX_age (math (date +%s) - (stat -c %Y $TMUX_FZF_GIT_INDEX))
        end
    end
    if test $index_age -gt 86400
        rm $TMUX_FZF_GIT_INDEX
    end

    if not test -f $TMUX_FZF_GIT_INDEX
        find $git_dir -maxdepth 4 -type d -name .git \
            | sed 's|/.git$||' | sed "s|$git_dir/||" \
            | grep -F -v . | grep -v gitsyncer-workdir | grep -v upstream >$TMUX_FZF_GIT_INDEX
        # Add top-level directories from TMPUTILS_DIR (non-git)
        if test -d $tmp_dir
            for d in (ls $tmp_dir)
                if test -d $tmp_dir/$d
                    echo "tmp/$d" >>$TMUX_FZF_GIT_INDEX
                end
            end
        end
    end

    set -l matches (grep "$filter" $TMUX_FZF_GIT_INDEX)
    set -l session
    if test (count $matches) -eq 1
        set session $matches[1]
    else
        set session (printf "%s\n" $matches | fzf)
    end
    if string match -r '^tmp/' $session
        set -l real (string replace -r '^tmp/' '' $session)
        cd $TMPUTILS_DIR/$real
    else if test -d $git_dir/$session
        cd $git_dir/$session
    end
    tmux::attach $session
end

function tmux::project::reindex
    if test -f $TMUX_FZF_GIT_INDEX
        rm $TMUX_FZF_GIT_INDEX
    end
    tmux::project $argv
end

function tmux::attach
    set -l session $argv[1]
    if test -z "$session"
        tmux attach-session || tmux::new
    else
        tmux attach-session -t $session || tmux::new $session
    end
end

function tmux::remote
    set -l server $argv[1]
    tmux new -s $server "ssh -A -t $server 'tmux attach-session || tmux'" || tmux attach-session -d -t $server
end

function tmux::search
    set -l session (tmux list-sessions | fzf | cut -d: -f1)
    if test -z "$TMUX"
        tmux attach-session -t $session
    else
        tmux switch -t $session
    end
end

function tmux::cluster_ssh
    if test -f "$argv[1]"
        tmux::tssh_from_file $argv[1]
        return
    end
    tmux::tssh_from_argument $argv
end

function tmux::tssh_from_argument
    set -l session $argv[1]
    set first_server_or_container $argv[2]
    set remaining_servers $argv[3..-1]
    if test -z "$first_server_or_container"
        set first_server_or_container $session
    end

    tmux new-session -d -s $session (_tmux::connect_command "$first_server_or_container")
    if not tmux list-session | grep "^$session:"
        echo "Could not create session $session"
        return 2
    end
    for server_or_container in $remaining_servers
        tmux split-window -t $session "tmux select-layout tiled; $(_tmux::connect_command "$server_or_container")"
    end
    tmux setw -t $session synchronize-panes on
    tmux -2 attach-session -t $session || tmux -2 switch-client -t $session
end

function tmux::tssh_from_file
    set -l serverlist $argv[1]
    set -l session (basename $serverlist | cut -d. -f1)
    tmux::tssh_from_argument $session (awk '{ print $1 }' $serverlist | sed 's/.lan./.lan/g')
end

alias tn 'tmux::new'
alias ta 'tmux::attach'
alias tx 'tmux::remote'
alias tl 'tmux::search'
alias tssh 'tmux::cluster_ssh'
alias tp 'tmux::project'
alias tpr 'tmux::project::reindex'
alias notes 'cd ~/Notes; tmux::attach notes'
alias N 'cd ~/Notes; tmux::attach notes'
alias bar 'tmux::new bar'
alias baz 'tmux::new baz'
alias bay 'tmux::new bay'