1"""
2Lazy-loading per-terminal grapheme override tables.
3
4This minimizes memory for the 99.9% of use-cases of a single 'term_program'.
5"""
6from __future__ import annotations
7
8# std imports
9import importlib
10from functools import lru_cache
11
12# local
13from ._registry import _REGISTRY
14
15
16@lru_cache(maxsize=32)
17def get(term_canonical: str) -> dict[str, int]:
18 """
19 Return grapheme override dict for a terminal, or an empty dict.
20
21 The per-terminal module is imported on first access and cached in ``sys.modules``; subsequent
22 calls for the same terminal return immediately via lru_cache.
23 """
24 hash_key = _REGISTRY.get(term_canonical)
25 if hash_key is None:
26 return {}
27
28 try:
29 mod = importlib.import_module(f'._known_{hash_key}', __package__)
30 result: dict[str, int] = getattr(mod, 'GRAPHEMES')
31 return result
32 except ImportError:
33 # This can occur during a program re-install when the registry and files are out of sync
34 # (filesystem vs. in-memory copy differ), this can happen with a "pip upgrade" of a running
35 # service, or if the upgrade tool itself uses wcwidth. Not being able to provide terminal
36 # overrides in this situation is not important, continue without them.
37 return {}