#!/usr/bin/env python # License: GPL v3 Copyright: 2018, Kovid Goyal import os import re from collections.abc import Callable, Generator, Sequence from contextlib import contextmanager from functools import lru_cache, partial, wraps from string import Formatter as StringFormatter from typing import ( Any, Literal, NamedTuple, ) from .borders import Border, BorderColor from .constants import config_dir, is_macos from .fast_data_types import ( BOTTOM_EDGE, DECAWM, LEFT_EDGE, LNM, RIGHT_EDGE, TOP_EDGE, Color, Region, Screen, cell_size_for_window, get_boss, get_options, pt_to_px, set_tab_bar_render_data, split_into_graphemes, update_tab_bar_edge_colors, viewport_for_window, wcswidth, ) from .progress import ProgressState from .rgb import alpha_blend, color_as_sgr, color_from_int, to_color from .types import WindowGeometry, run_once from .typing_compat import EdgeLiteral, PowerlineStyle from .utils import color_as_int, log_error, sgr_sanitizer_pat if is_macos: from .fast_data_types import cocoa_is_secure_input_enabled # type: ignore class TabBarData(NamedTuple): title: str is_active: bool = False needs_attention: bool = False tab_id: int = -1 os_window_id: int = 0 num_windows: int = 0 num_window_groups: int = 0 layout_name: str = '' has_activity_since_last_focus: bool = False active_fg: int | None = None active_bg: int | None = None inactive_fg: int | None = None inactive_bg: int | None = None num_of_windows_with_progress: int = 0 total_progress: int = 0 last_focused_window_with_progress_id: int = 0 session_name: str = '' active_session_name: str = '' class DrawData(NamedTuple): leading_spaces: int sep: str trailing_spaces: int bell_on_tab: str alpha: Sequence[float] active_fg: Color active_bg: Color inactive_fg: Color inactive_bg: Color default_bg: Color title_template: str active_title_template: str | None tab_activity_symbol: str powerline_style: PowerlineStyle tab_bar_edge: EdgeLiteral max_tab_title_length: int max_tab_title_lines: int wrap_width: int os_window_id: int def tab_fg(self, tab: TabBarData) -> int: if tab.is_active: if tab.active_fg is not None: return tab.active_fg return int(self.active_fg) if tab.inactive_fg is not None: return tab.inactive_fg return int(self.inactive_fg) def tab_bg(self, tab: TabBarData) -> int: if tab.is_active: if tab.active_bg is not None: return tab.active_bg return int(self.active_bg) if tab.inactive_bg is not None: return tab.inactive_bg return int(self.inactive_bg) def as_rgb(x: int) -> int: return (x << 8) | 2 VERTICAL_EDGES = frozenset({LEFT_EDGE, RIGHT_EDGE}) def is_vertical_edge(edge: int) -> bool: return edge in VERTICAL_EDGES edge_name_map: dict[int, EdgeLiteral] = { LEFT_EDGE: 'left', TOP_EDGE: 'top', RIGHT_EDGE: 'right', BOTTOM_EDGE: 'bottom', } def normalized_tab_bar_align(align: str) -> Literal['start', 'end', 'center']: match align: case 'left' | 'start': return 'start' case 'right' | 'end': return 'end' return 'center' @lru_cache def report_template_failure(template: str, e: str) -> None: log_error(f'Invalid tab title template: "{template}" with error: {e}') @lru_cache def compile_template(template: str) -> Any: try: return compile('f"""' + template + '"""', '