-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux_select_pane.py
91 lines (64 loc) · 2.26 KB
/
tmux_select_pane.py
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
import libtmux
import sys
import sh
import argparse
from pprint import pprint
from collections import defaultdict
from pyfzf.pyfzf import FzfPrompt
parser = argparse.ArgumentParser()
parser.add_argument("--no-open", action="store_true", help="Do not open tmux window. Mostly for debugging")
parser_args = parser.parse_args()
# Get active tmux sessions
srv = libtmux.Server()
fzf = FzfPrompt()
commands = defaultdict(list)
all_tty = [p.pane_tty for p in srv.panes]
cmd = f"-t {' -t '.join(all_tty)} -o pid:10 -o tty:10 -o command -ww" # -f
sh_commands = sh.ps(cmd.split(' ')).stdout.decode().strip().split("\n")
# Ignore first lines (i.e: table headers)
for cmd in sh_commands[1:]:
pid = cmd[:10].strip()
tty = cmd[10:20].strip()
command = cmd[20:].strip()
tty_number = int(tty.replace("pts/", ""))
if command in ["-zsh", "/bin/zsh"]:
continue
commands[tty_number].append(
{
"pid": int(pid),
"command": command,
}
)
def format_pane(pane):
global commands
tty_number = int(pane.pane_tty.replace("/dev/pts/", ""))
running_commands = sorted(commands[tty_number], key=lambda c: c["pid"])
if len(running_commands) >= 1:
cmd = running_commands[0]
else:
cmd = {"pid": "-", "command": "*command not found*"}
path = pane.pane_current_path.replace("/home/legrems/Documents/Arcanite", "~/D/A")
path = path.replace("/home/legrems/Documents", "~/D")
path = path.replace("/home/legrems", "~")
return [f"{pane.pane_tty}: [{pane.session_name}: {pane.window_name}, {path}]: {cmd['command']}"]
panes = []
for pane in srv.panes:
panes.extend(format_pane(pane))
selections = fzf.prompt(["Select one pane you want to switch to"] + panes, "--cycle --header-lines 1")
if not selections:
sys.exit(0)
pane_name = selections[0]
tty = pane_name.split(":")[0]
selected_pane = srv.panes.get(pane_tty=tty)
if parser_args.no_open:
print(selected_pane)
print(tty)
tty_number = int(tty.replace("/dev/pts/", ""))
pprint(commands[tty_number])
else:
# Go to this session
selected_pane.session.switch_client()
# Select the correct window
selected_pane.window.select()
# And switch to this pane
selected_pane.window.select_pane(selected_pane.pane_id)