From c2b6eaa87ac90e4c06d380c7640d141bb9753dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gremaud?= Date: Mon, 10 Jun 2024 16:43:57 +0200 Subject: [PATCH] Add tmux pane selection --- tmux_select_pane.py | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tmux_select_pane.py diff --git a/tmux_select_pane.py b/tmux_select_pane.py new file mode 100644 index 0000000..b42db7c --- /dev/null +++ b/tmux_select_pane.py @@ -0,0 +1,69 @@ +import libtmux +import sys +import sh +from collections import defaultdict + + +from pyfzf.pyfzf import FzfPrompt + +# 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" + +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/", "")) + + commands[tty_number].append( + { + "pid": pid, + "command": command, + } + ) + + +def format_pane(pane): + global commands + + tty_number = int(pane.pane_tty.replace("/dev/pts/", "")) + running_commands = commands[tty_number] + + if len(running_commands) == 2: + cmd = running_commands[-1] + + else: + cmd = {"pid": "-", "command": "-"} + + return f"{pane.pane_tty}: [Sess:{pane.session_name}, Win:{pane.window_name}] (cwd:{pane.pane_current_path}): {cmd['command']}" + + +panes = [format_pane(pane) for pane in srv.panes] +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) + +# 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)