From dd4051bfd5e17c9d323d120864a912200163ee98 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 17 Nov 2022 22:03:00 +0530 Subject: [PATCH] Allow getting the exe name of the active foreground process in the tab title template --- kitty/child.py | 9 +++++++++ kitty/options/definition.py | 5 ++++- kitty/tab_bar.py | 15 +++++++++++++++ kitty/tabs.py | 4 ++++ kitty/window.py | 3 +++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/kitty/child.py b/kitty/child.py index c0561907a..b7bcf4c44 100644 --- a/kitty/child.py +++ b/kitty/child.py @@ -444,6 +444,15 @@ class Child: return cwd_of_process(pid) or None return None + def get_foreground_exe(self, oldest: bool = False) -> Optional[str]: + with suppress(Exception): + pid = self.get_pid_for_cwd(oldest) + if pid is not None: + c = cmdline_of_pid(pid) + if c: + return c[0] + return None + @property def foreground_cwd(self) -> Optional[str]: return self.get_foreground_cwd() diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 23f7bd72c..d2bf84f4e 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -1177,7 +1177,10 @@ use :code:`{sup.index}`. All data available is: The number of window groups (not counting overlay windows) in the tab. :code:`tab.active_wd` The working directory of the currently active window in the tab (expensive, - requires syscall). + requires syscall). Use :code:`active_oldest_wd` to get the directory of the oldest foreground process rather than the newest. +:code:`tab.active_exe` + The name of the executable running in the foreground of the currently active window in the tab (expensive, + requires syscall). Use :code:`active_oldest_exe` for the oldest foreground process. :code:`max_title_length` The maximum title length available. diff --git a/kitty/tab_bar.py b/kitty/tab_bar.py index 0c074dfbb..68003784f 100644 --- a/kitty/tab_bar.py +++ b/kitty/tab_bar.py @@ -188,6 +188,21 @@ class TabAccessor: tab = get_boss().tab_for_id(self.tab_id) return (tab.get_cwd_of_active_window() if tab else '') or '' + @property + def active_oldest_wd(self) -> str: + tab = get_boss().tab_for_id(self.tab_id) + return (tab.get_cwd_of_active_window(oldest=True) if tab else '') or '' + + @property + def active_exe(self) -> str: + tab = get_boss().tab_for_id(self.tab_id) + return os.path.basename((tab.get_exe_of_active_window() if tab else '') or '') + + @property + def active_oldest_exe(self) -> str: + tab = get_boss().tab_for_id(self.tab_id) + return os.path.basename((tab.get_exe_of_active_window(oldest=True) if tab else '') or '') + safe_builtins = { 'max': max, 'min': min, 'str': str, 'repr': repr, 'abs': abs, 'len': len, 're': re, diff --git a/kitty/tabs.py b/kitty/tabs.py index aa93b7210..565c6c5ab 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -235,6 +235,10 @@ class Tab: # {{{ w = self.active_window return w.get_cwd_of_child(oldest) if w else None + def get_exe_of_active_window(self, oldest: bool = False) -> Optional[str]: + w = self.active_window + return w.get_exe_of_child(oldest) if w else None + def set_title(self, title: str) -> None: self.name = title or '' self.mark_tab_bar_dirty() diff --git a/kitty/window.py b/kitty/window.py index 0310bbb51..7206b3497 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -1391,6 +1391,9 @@ class Window: def get_cwd_of_root_child(self) -> Optional[str]: return self.child.current_cwd + def get_exe_of_child(self, oldest: bool = False) -> str: + return self.child.get_foreground_exe(oldest) or self.child.argv[0] + @property def cwd_of_child(self) -> Optional[str]: return self.get_cwd_of_child()