Dont incur the cost of checking the cwd of a process on every tabbar update unless actually needed

This commit is contained in:
Kovid Goyal 2022-07-26 08:19:14 +05:30
parent 4345ea6602
commit 04690c8c7c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 26 additions and 9 deletions

View File

@ -5,8 +5,9 @@ import os
from functools import lru_cache, partial, wraps from functools import lru_cache, partial, wraps
from string import Formatter as StringFormatter from string import Formatter as StringFormatter
from typing import ( from typing import (
Any, Callable, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union Any, Callable, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union, TYPE_CHECKING
) )
from weakref import ReferenceType
from .borders import Border, BorderColor from .borders import Border, BorderColor
from .config import build_ansi_color_table from .config import build_ansi_color_table
@ -21,11 +22,15 @@ from .typing import EdgeLiteral, PowerlineStyle
from .utils import color_as_int, log_error, sgr_sanitizer_pat from .utils import color_as_int, log_error, sgr_sanitizer_pat
if TYPE_CHECKING:
from .tabs import Tab
class TabBarData(NamedTuple): class TabBarData(NamedTuple):
title: str title: str
is_active: bool is_active: bool
needs_attention: bool needs_attention: bool
cwd: str tab_ref: ReferenceType['Tab']
num_windows: int num_windows: int
num_window_groups: int num_window_groups: int
layout_name: str layout_name: str
@ -173,14 +178,28 @@ def template_has_field(template: str, field: str) -> bool:
return False return False
class TabCWD:
def __init__(self, tab_ref: ReferenceType['Tab']):
self.tab_ref = tab_ref
self.saved_val: Optional[str] = None
def __str__(self) -> str:
if self.saved_val is None:
tab = self.tab_ref()
self.saved_val = (tab.get_cwd_of_active_window() or '') if tab else ''
return self.saved_val
def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int) -> None: def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int) -> None:
tcwd = TabCWD(tab.tab_ref)
data = { data = {
'index': index, 'index': index,
'layout_name': tab.layout_name, 'layout_name': tab.layout_name,
'num_windows': tab.num_windows, 'num_windows': tab.num_windows,
'num_window_groups': tab.num_window_groups, 'num_window_groups': tab.num_window_groups,
'title': tab.title, 'title': tab.title,
'cwd': tab.cwd, 'cwd': tcwd,
} }
ColorFormatter.draw_data = draw_data ColorFormatter.draw_data = draw_data
ColorFormatter.tab_data = tab ColorFormatter.tab_data = tab
@ -190,7 +209,7 @@ def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int)
'num_windows': tab.num_windows, 'num_windows': tab.num_windows,
'num_window_groups': tab.num_window_groups, 'num_window_groups': tab.num_window_groups,
'title': tab.title, 'title': tab.title,
'cwd': tab.cwd, 'cwd': tcwd,
'fmt': Formatter, 'fmt': Formatter,
'sup': SupSub(data), 'sup': SupSub(data),
'sub': SupSub(data, True), 'sub': SupSub(data, True),

View File

@ -230,10 +230,9 @@ class Tab: # {{{
ans += 1 ans += 1
return ans return ans
@property def get_cwd_of_active_window(self, oldest: bool = False) -> Optional[str]:
def get_cwd(self) -> str:
w = self.active_window w = self.active_window
return w.cwd_of_child if w and w.cwd_of_child else self.cwd return w.get_cwd_of_child(oldest) if w else None
def set_title(self, title: str) -> None: def set_title(self, title: str) -> None:
self.name = title or '' self.name = title or ''
@ -1083,14 +1082,13 @@ class TabManager: # {{{
title = (t.name or t.title or appname).strip() title = (t.name or t.title or appname).strip()
needs_attention = False needs_attention = False
has_activity_since_last_focus = False has_activity_since_last_focus = False
cwd = os.path.abspath(t.get_cwd)
for w in t: for w in t:
if w.needs_attention: if w.needs_attention:
needs_attention = True needs_attention = True
if w.has_activity_since_last_focus: if w.has_activity_since_last_focus:
has_activity_since_last_focus = True has_activity_since_last_focus = True
ans.append(TabBarData( ans.append(TabBarData(
title, t is at, needs_attention, cwd, title, t is at, needs_attention, weakref.ref(t),
len(t), t.num_window_groups, t.current_layout.name or '', len(t), t.num_window_groups, t.current_layout.name or '',
has_activity_since_last_focus, t.active_fg, t.active_bg, has_activity_since_last_focus, t.active_fg, t.active_bg,
t.inactive_fg, t.inactive_bg t.inactive_fg, t.inactive_bg