Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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). \
Expand Down
29 changes: 23 additions & 6 deletions scripts/rename_session_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -185,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
Expand Down Expand Up @@ -255,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:]
Expand All @@ -287,6 +287,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()

Expand Down Expand Up @@ -331,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]
Expand Down Expand Up @@ -458,7 +474,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'
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_exclusive_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/')

Expand Down