updated panel display
This commit is contained in:
+116
-19
@@ -16,6 +16,7 @@ from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Optional, Callable, Set
|
||||
from pathlib import Path
|
||||
import json
|
||||
import re
|
||||
|
||||
from rich.box import SIMPLE
|
||||
from rich.console import Group
|
||||
@@ -113,14 +114,97 @@ _RESULT_TABLE_ROW_STYLE_LOOP: List[tuple[str, str]] = [
|
||||
("#ffffff", "#000000"),
|
||||
]
|
||||
|
||||
_RESULT_TABLE_BW_ROW_STYLE_LOOP: List[tuple[str, str]] = [
|
||||
("#000000", "#ffffff"),
|
||||
("#ffffff", "#000000"),
|
||||
]
|
||||
|
||||
RESULT_TABLE_HEADER_STYLE = "bold #000000 on #ffffff"
|
||||
RESULT_TABLE_BORDER_STYLE = "#000000 on #ffffff"
|
||||
RESULT_TABLE_PLAIN_HEADER_STYLE = "bold"
|
||||
|
||||
_RESULT_TABLE_APPEARANCE_ALIASES: Dict[str, str] = {
|
||||
"": "rainbow",
|
||||
"rainbow": "rainbow",
|
||||
"default": "rainbow",
|
||||
"plain": "plain",
|
||||
"none": "plain",
|
||||
"bw": "bw-striped",
|
||||
"b-w": "bw-striped",
|
||||
"b-w-striped": "bw-striped",
|
||||
"bw-striped": "bw-striped",
|
||||
"b-w-stripes": "bw-striped",
|
||||
"bw-stripes": "bw-striped",
|
||||
"black-white": "bw-striped",
|
||||
"black-white-striped": "bw-striped",
|
||||
"black-white-stripes": "bw-striped",
|
||||
"black-and-white": "bw-striped",
|
||||
"black-and-white-striped": "bw-striped",
|
||||
"black-and-white-stripes": "bw-striped",
|
||||
}
|
||||
|
||||
|
||||
def get_result_table_row_style(row_index: int) -> str:
|
||||
text_color, bg_color = _RESULT_TABLE_ROW_STYLE_LOOP[
|
||||
row_index % len(_RESULT_TABLE_ROW_STYLE_LOOP)
|
||||
]
|
||||
def normalize_result_table_appearance_mode(value: Any) -> str:
|
||||
text = str(value or "").strip().lower()
|
||||
if not text:
|
||||
return "rainbow"
|
||||
|
||||
collapsed = re.sub(r"[^a-z0-9]+", "-", text).strip("-")
|
||||
return _RESULT_TABLE_APPEARANCE_ALIASES.get(collapsed, "rainbow")
|
||||
|
||||
|
||||
def get_result_table_appearance_mode(config: Optional[Dict[str, Any]] = None) -> str:
|
||||
cfg = config
|
||||
if cfg is None:
|
||||
try:
|
||||
from SYS.config import load_config
|
||||
|
||||
cfg = load_config() or {}
|
||||
except Exception:
|
||||
cfg = {}
|
||||
|
||||
raw_value = None
|
||||
if isinstance(cfg, dict):
|
||||
raw_value = cfg.get("table_appearance")
|
||||
|
||||
return normalize_result_table_appearance_mode(raw_value)
|
||||
|
||||
|
||||
def get_result_table_header_style(config: Optional[Dict[str, Any]] = None) -> str:
|
||||
mode = get_result_table_appearance_mode(config)
|
||||
if mode == "plain":
|
||||
return RESULT_TABLE_PLAIN_HEADER_STYLE
|
||||
return RESULT_TABLE_HEADER_STYLE
|
||||
|
||||
|
||||
def get_result_table_border_style(config: Optional[Dict[str, Any]] = None) -> str:
|
||||
mode = get_result_table_appearance_mode(config)
|
||||
if mode == "plain":
|
||||
return ""
|
||||
return RESULT_TABLE_BORDER_STYLE
|
||||
|
||||
|
||||
def get_result_table_panel_style(config: Optional[Dict[str, Any]] = None) -> str:
|
||||
mode = get_result_table_appearance_mode(config)
|
||||
if mode == "plain":
|
||||
return ""
|
||||
return "on #ffffff"
|
||||
|
||||
|
||||
def get_result_table_row_style(
|
||||
row_index: int,
|
||||
appearance_mode: Optional[str] = None,
|
||||
config: Optional[Dict[str, Any]] = None,
|
||||
) -> str:
|
||||
mode = appearance_mode or get_result_table_appearance_mode(config)
|
||||
if mode == "plain":
|
||||
return ""
|
||||
|
||||
style_loop = (
|
||||
_RESULT_TABLE_BW_ROW_STYLE_LOOP
|
||||
if mode == "bw-striped" else _RESULT_TABLE_ROW_STYLE_LOOP
|
||||
)
|
||||
text_color, bg_color = style_loop[row_index % len(style_loop)]
|
||||
return f"{text_color} on {bg_color}"
|
||||
|
||||
|
||||
@@ -1435,16 +1519,21 @@ class Table:
|
||||
|
||||
def to_rich(self):
|
||||
"""Return a Rich renderable representing this table."""
|
||||
appearance_mode = get_result_table_appearance_mode()
|
||||
header_style = get_result_table_header_style({"table_appearance": appearance_mode})
|
||||
border_style = get_result_table_border_style({"table_appearance": appearance_mode})
|
||||
panel_style = get_result_table_panel_style({"table_appearance": appearance_mode})
|
||||
|
||||
if not self.rows:
|
||||
empty = Text("No results")
|
||||
return (
|
||||
Panel(
|
||||
empty,
|
||||
title=Text(str(self.title), style=RESULT_TABLE_HEADER_STYLE),
|
||||
border_style=RESULT_TABLE_BORDER_STYLE,
|
||||
title=Text(str(self.title), style=header_style),
|
||||
border_style=border_style,
|
||||
padding=(0, 0),
|
||||
expand=False,
|
||||
style="on #ffffff",
|
||||
style=panel_style,
|
||||
)
|
||||
if self.title
|
||||
else empty
|
||||
@@ -1460,8 +1549,8 @@ class Table:
|
||||
|
||||
table = RichTable(
|
||||
show_header=True,
|
||||
header_style=RESULT_TABLE_HEADER_STYLE,
|
||||
border_style=RESULT_TABLE_BORDER_STYLE,
|
||||
header_style=header_style,
|
||||
border_style=border_style,
|
||||
box=None,
|
||||
expand=False,
|
||||
show_lines=False,
|
||||
@@ -1497,7 +1586,13 @@ class Table:
|
||||
for name in col_names:
|
||||
val = row.get_column(name) or ""
|
||||
cells.append(self._apply_value_case(_sanitize_cell_text(val)))
|
||||
table.add_row(*cells, style=get_result_table_row_style(row_idx - 1))
|
||||
table.add_row(
|
||||
*cells,
|
||||
style=get_result_table_row_style(
|
||||
row_idx - 1,
|
||||
appearance_mode=appearance_mode,
|
||||
),
|
||||
)
|
||||
|
||||
if self.title or self.header_lines:
|
||||
header_bits = [Text(line) for line in (self.header_lines or [])]
|
||||
@@ -1505,11 +1600,11 @@ class Table:
|
||||
return (
|
||||
Panel(
|
||||
renderable,
|
||||
title=Text(str(self.title), style=RESULT_TABLE_HEADER_STYLE),
|
||||
border_style=RESULT_TABLE_BORDER_STYLE,
|
||||
title=Text(str(self.title), style=header_style),
|
||||
border_style=border_style,
|
||||
padding=(0, 0),
|
||||
expand=False,
|
||||
style="on #ffffff",
|
||||
style=panel_style,
|
||||
)
|
||||
if self.title
|
||||
else renderable
|
||||
@@ -2385,11 +2480,13 @@ class ItemDetailView(Table):
|
||||
elements = []
|
||||
|
||||
if has_details:
|
||||
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(
|
||||
details_table,
|
||||
title=Text(detail_title, style=RESULT_TABLE_HEADER_STYLE),
|
||||
border_style=RESULT_TABLE_BORDER_STYLE,
|
||||
title=Text(detail_title, style=header_style),
|
||||
border_style=border_style,
|
||||
padding=(1, 2)
|
||||
))
|
||||
|
||||
@@ -2397,11 +2494,11 @@ class ItemDetailView(Table):
|
||||
# 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):
|
||||
results_renderable.border_style = RESULT_TABLE_BORDER_STYLE
|
||||
results_renderable.border_style = get_result_table_border_style()
|
||||
if results_renderable.title:
|
||||
results_renderable.title = Text(
|
||||
str(results_renderable.title),
|
||||
style=RESULT_TABLE_HEADER_STYLE,
|
||||
style=get_result_table_header_style(),
|
||||
)
|
||||
# Add a bit of padding inside if it contains a table
|
||||
elements.append(results_renderable)
|
||||
@@ -2416,8 +2513,8 @@ class ItemDetailView(Table):
|
||||
elements.append(
|
||||
Panel(
|
||||
results_group,
|
||||
title=Text(str(display_title), style=RESULT_TABLE_HEADER_STYLE),
|
||||
border_style=RESULT_TABLE_BORDER_STYLE,
|
||||
title=Text(str(display_title), style=get_result_table_header_style()),
|
||||
border_style=get_result_table_border_style(),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user