updating and refactoring codebase for improved performance and maintainability

This commit is contained in:
2026-05-03 17:29:32 -07:00
parent b7d3dc5f2d
commit 77cab1bd27
17 changed files with 590 additions and 294 deletions
+71 -44
View File
@@ -18,20 +18,47 @@ from pathlib import Path
import json
import re
from rich.box import SIMPLE
from rich.console import Group
from rich.panel import Panel
from rich.prompt import Prompt
from rich.table import Table as RichTable
from rich.text import Text
# rich imports are deferred to avoid ~100ms startup cost.
# All rich types are only needed inside method bodies, so we lazily import on first use.
_rich_mod: Any = None
# Optional Textual imports - graceful fallback if not available
try:
from textual.widgets import Tree
TEXTUAL_AVAILABLE = True
except ImportError:
TEXTUAL_AVAILABLE = False
def _rich():
global _rich_mod
if _rich_mod is None:
import types as _types
_m = _types.SimpleNamespace()
from rich.box import SIMPLE as _SIMPLE
from rich.console import Group as _Group
from rich.panel import Panel as _Panel
from rich.prompt import Prompt as _Prompt
from rich.table import Table as _RichTable
from rich.text import Text as _Text
_m.SIMPLE = _SIMPLE
_m.Group = _Group
_m.Panel = _Panel
_m.Prompt = _Prompt
_m.RichTable = _RichTable
_m.Text = _Text
_rich_mod = _m
return _rich_mod
# Optional Textual imports - lazily loaded to avoid pulling in ~300ms of textual
# at import time when the TUI is not being used.
import importlib.util as _importlib_util
TEXTUAL_AVAILABLE: bool = _importlib_util.find_spec("textual") is not None
# Tree is populated lazily on first call to build_metadata_tree().
_textual_Tree: Any = None
def _get_textual_Tree() -> Any:
global _textual_Tree
if _textual_Tree is None:
from textual.widgets import Tree as _Tree
_textual_Tree = _Tree
return _textual_Tree
# Import ResultModel from the API for typing; avoid runtime redefinition issues
@@ -1591,11 +1618,11 @@ class Table:
panel_style = get_result_table_panel_style({"table_appearance": appearance_mode})
if not self.rows:
empty = Text("No results")
empty = _rich().Text("No results")
return (
Panel(
_rich().Panel(
empty,
title=Text(str(self.title), style=header_style),
title=_rich().Text(str(self.title), style=header_style),
border_style=border_style,
padding=(0, 0),
expand=False,
@@ -1613,7 +1640,7 @@ class Table:
seen.add(col.name)
col_names.append(col.name)
table = RichTable(
table = _rich().RichTable(
show_header=True,
header_style=header_style,
border_style=border_style,
@@ -1661,12 +1688,12 @@ class Table:
)
if self.title or self.header_lines:
header_bits = [Text(line) for line in (self.header_lines or [])]
renderable = Group(*header_bits, table) if header_bits else table
header_bits = [_rich().Text(line) for line in (self.header_lines or [])]
renderable = _rich().Group(*header_bits, table) if header_bits else table
return (
Panel(
_rich().Panel(
renderable,
title=Text(str(self.title), style=header_style),
title=_rich().Text(str(self.title), style=header_style),
border_style=border_style,
padding=(0, 0),
expand=False,
@@ -1777,7 +1804,7 @@ class Table:
from SYS.rich_display import stdout_console
stdout_console().print(self)
stdout_console().print(Panel(Text("Selection is disabled for this table.")))
stdout_console().print(_rich().Panel(_rich().Text("Selection is disabled for this table.")))
return None
# Display the table
@@ -1789,11 +1816,11 @@ class Table:
while True:
try:
if accept_args:
choice = Prompt.ask(
choice = _rich().Prompt.ask(
f"{prompt} (e.g., '5' or '2 -storage hydrus' or 'q' to quit)"
).strip()
else:
choice = Prompt.ask(
choice = _rich().Prompt.ask(
f"{prompt} (e.g., '5' or '3-5' or '1,3,5' or 'q' to quit)"
).strip()
@@ -1806,8 +1833,8 @@ class Table:
if result is not None:
return result
stdout_console().print(
Panel(
Text(
_rich().Panel(
_rich().Text(
"Invalid format. Use: selection (5 or 3-5 or 1,3,5) optionally followed by flags (e.g., '5 -storage hydrus')."
)
)
@@ -1818,8 +1845,8 @@ class Table:
if selected_indices is not None:
return selected_indices
stdout_console().print(
Panel(
Text(
_rich().Panel(
_rich().Text(
"Invalid format. Use: single (5), range (3-5), list (1,3,5), combined (1-3,7,9-11), or 'q' to quit."
)
)
@@ -1827,16 +1854,16 @@ class Table:
except (ValueError, EOFError):
if accept_args:
stdout_console().print(
Panel(
Text(
_rich().Panel(
_rich().Text(
"Invalid format. Use: selection (5 or 3-5 or 1,3,5) optionally followed by flags (e.g., '5 -storage hydrus')."
)
)
)
else:
stdout_console().print(
Panel(
Text(
_rich().Panel(
_rich().Text(
"Invalid format. Use: single (5), range (3-5), list (1,3,5), combined (1-3,7,9-11), or 'q' to quit."
)
)
@@ -2468,12 +2495,12 @@ class ItemDetailView(Table):
from rich.text import Text
# 1. Create Detail Grid (matching rich_display.py style)
details_table = RichTable.grid(expand=True, padding=(0, 2))
details_table = _rich().RichTable.grid(expand=True, padding=(0, 2))
details_table.add_column("Key", style="cyan", justify="right", width=15)
details_table.add_column("Value", style="white")
def _render_tag_text(tag_value: Any) -> Text:
tag_text = Text()
tag_text = _rich().Text()
tag_text.append("#", style="dim")
raw = str(tag_value or "")
@@ -2497,17 +2524,17 @@ class ItemDetailView(Table):
renderables.append(_render_tag_text(tag))
if freeform_tags:
freeform_grid = RichTable.grid(expand=True, padding=(0, 2))
freeform_grid = _rich().RichTable.grid(expand=True, padding=(0, 2))
for _ in range(3):
freeform_grid.add_column(ratio=1)
for row_values in _chunk_detail_tags(freeform_tags, 3):
cells = [_render_tag_text(tag) for tag in row_values]
while len(cells) < 3:
cells.append(Text(""))
cells.append(_rich().Text(""))
freeform_grid.add_row(*cells)
renderables.append(freeform_grid)
return Group(*renderables)
return _rich().Group(*renderables)
def _has_renderable_value(value: Any) -> bool:
if value is None:
@@ -2596,9 +2623,9 @@ class ItemDetailView(Table):
header_style = get_result_table_header_style()
border_style = get_result_table_border_style()
detail_title = str(self.detail_title or "Item Details").strip() or "Item Details"
elements.append(Panel(
elements.append(_rich().Panel(
details_table,
title=Text(detail_title, style=header_style),
title=_rich().Text(detail_title, style=header_style),
border_style=border_style,
padding=(1, 2)
))
@@ -2606,10 +2633,10 @@ class ItemDetailView(Table):
if results_renderable:
# If it's a Panel already (from super().to_rich() with title), use it directly
# but force the border style to the result-table standard for consistency
if isinstance(results_renderable, Panel):
if isinstance(results_renderable, _rich().Panel):
results_renderable.border_style = get_result_table_border_style()
if results_renderable.title:
results_renderable.title = Text(
results_renderable.title = _rich().Text(
str(results_renderable.title),
style=get_result_table_header_style(),
)
@@ -2622,13 +2649,13 @@ class ItemDetailView(Table):
display_title = original_title
# Add a bit of padding
results_group = Group(Text(""), results_renderable, Text(""))
results_group = _rich().Group(_rich().Text(""), results_renderable, _rich().Text(""))
elements.append(
Panel(
_rich().Panel(
results_group,
title=Text(str(display_title), style=get_result_table_header_style()),
title=_rich().Text(str(display_title), style=get_result_table_header_style()),
border_style=get_result_table_border_style(),
)
)
return Group(*elements)
return _rich().Group(*elements)