From c97b08fc9b455bc6c3ea71acf7ccfda657831ed7 Mon Sep 17 00:00:00 2001 From: Tomer Harpaz Date: Thu, 12 Jun 2025 09:32:08 +0300 Subject: [PATCH 1/4] fixed ruff check issues and use of `X | Y` type annotation that requires Python 3.10 --- tests/test_exclusive_paths.py | 2 +- tests/test_icons.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_exclusive_paths.py b/tests/test_exclusive_paths.py index ded6f6e..d410c94 100644 --- a/tests/test_exclusive_paths.py +++ b/tests/test_exclusive_paths.py @@ -12,7 +12,7 @@ @dataclass class FakePane: - pane_current_path: str | None + pane_current_path: Optional[str] def _fake_pane(path: str, program: Optional[str]): diff --git a/tests/test_icons.py b/tests/test_icons.py index bb77b55..cc99b81 100644 --- a/tests/test_icons.py +++ b/tests/test_icons.py @@ -2,8 +2,7 @@ import sys from dataclasses import dataclass -from typing import Optional -from unittest.mock import Mock, patch, call +from unittest.mock import Mock, call sys.path.append('scripts/') From 0190c89d123ffb3b356f366ea2e0c89a000ae76f Mon Sep 17 00:00:00 2001 From: Tomer Harpaz Date: Thu, 12 Jun 2025 09:33:02 +0300 Subject: [PATCH 2/4] fixed logfile location for macOS --- scripts/rename_session_windows.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/rename_session_windows.py b/scripts/rename_session_windows.py index 8d07d48..7027b92 100755 --- a/scripts/rename_session_windows.py +++ b/scripts/rename_session_windows.py @@ -6,6 +6,7 @@ import subprocess import os import re +import platform from pathlib import Path from typing import Any, Iterator, List, Optional, Tuple from enum import Enum @@ -458,7 +459,8 @@ def main(): ) log_level = logging._nameToLevel.get(options.log_level, logging.WARNING) - log_file = os.path.join(tempfile.gettempdir(), 'tmux-window-name') + tempdir = "/tmp" if platform.system() == 'Darwin' else tempfile.gettempdir() + log_file = os.path.join(tempdir, 'tmux-window-name.log') logging.basicConfig( level=log_level, filename=log_file, format='%(levelname)s - %(filename)s:%(lineno)d %(funcName)s() %(message)s' ) From 41e086058316f6436f1fff885c3f473beea4ba4c Mon Sep 17 00:00:00 2001 From: Tomer Harpaz Date: Thu, 12 Jun 2025 09:33:34 +0300 Subject: [PATCH 3/4] added option to show only the basename of the running program --- README.md | 8 ++++++++ scripts/rename_session_windows.py | 3 +++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index a8f9ea3..75f15fd 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,14 @@ Show arguments that the program has been ran with. set -g @tmux_window_name_show_program_args "True" ``` +### `@tmux_window_name_show_program_basename` + +Show only the basename of the running program + +```tmux.conf +set -g @tmux_window_name_show_program_basename "False" +``` + ### `@tmux_window_name_substitute_sets` Replace program command lines with [re.sub](https://docs.python.org/3/library/re.html#re.sub). \ diff --git a/scripts/rename_session_windows.py b/scripts/rename_session_windows.py index 7027b92..3fafcec 100755 --- a/scripts/rename_session_windows.py +++ b/scripts/rename_session_windows.py @@ -186,6 +186,7 @@ class Options: ) dir_substitute_sets: List[Tuple] = field(default_factory=lambda: []) show_program_args: bool = True + show_program_basename: bool = False log_level: str = 'WARNING' @staticmethod @@ -288,6 +289,8 @@ def get_current_program(running_programs: List[bytes], pane: TmuxPane, options: logging.debug(f'its a shell, parsed shell program {shell_program}') return shell_program + if options.show_program_basename: + program[0] = Path(program[0].decode()).name.encode() if not options.show_program_args: return program[0].decode() From e338f2c562e37c1a401415968c4f2792230a2392 Mon Sep 17 00:00:00 2001 From: Tomer Harpaz Date: Thu, 12 Jun 2025 09:34:25 +0300 Subject: [PATCH 4/4] fixed support for programs that have spaces in their paths (e.g. a lot of macOS applications) --- scripts/rename_session_windows.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/scripts/rename_session_windows.py b/scripts/rename_session_windows.py index 3fafcec..0d6aa32 100755 --- a/scripts/rename_session_windows.py +++ b/scripts/rename_session_windows.py @@ -257,15 +257,13 @@ def parse_shell_command(shell_cmd: List[bytes]) -> Optional[str]: return ' '.join(shell_cmd_str[1:]) -def get_current_program(running_programs: List[bytes], pane: TmuxPane, options: Options) -> Optional[str]: +def get_current_program(running_programs: List[List[bytes]], pane: TmuxPane, options: Options) -> Optional[str]: if pane.pane_pid is None: raise ValueError(f'Pane id is none, pane: {pane}') logging.debug(f"searching for active pane's child with pane_pid={pane.pane_pid}") for program in running_programs: - program = program.split() - # if pid matches parse program if int(program[0]) == int(pane.pane_pid): program = program[1:] @@ -335,11 +333,25 @@ def rename_window(server: Server, window_id: str, window_name: str, max_name_len def get_panes_programs(session: Session, options: Options) -> List[Pane]: session_active_panes = get_session_active_panes(session) try: - running_programs = subprocess.check_output(['ps', '-a', '-oppid,command']).splitlines()[1:] + output = subprocess.check_output(['ps', '-a', '-opid,comm']) + pid_to_argv0 = dict(o.split(maxsplit=1) for o in output.splitlines()[1:]) + + running_programs = [] + output = subprocess.check_output(['ps', '-a', '-opid,ppid,command']) + for o in output.splitlines()[1:]: + pid, ppid, command = o.split(maxsplit=2) + + argv0 = pid_to_argv0.get(pid) + if argv0: + argv = [argv0] + command.lstrip(argv0).split() + else: + argv = command.split() + + running_programs.append([ppid] + argv) logging.debug(f'running_programs={running_programs}') # can occur if ps has empty output except subprocess.CalledProcessError: - logging.warning('nothing returned from `ps -a -oppid,command`') + logging.warning('nothing returned from ps') running_programs = [] return [Pane(p, get_current_program(running_programs, p, options)) for p in session_active_panes]