Skip to content

Commit ab8f5e9

Browse files
AnselmooCopilot
andcommitted
fix: 🐛 render warnings and errors as Rich Panels in fit command
Replace plain typer.echo/style with rich.panel.Panel for all warnings and errors in the fit command: - _rich_showwarning: intercepts Python UserWarning, strips the legacy ASCII-art ## WARNING ##### borders from report/metrics.py, and renders a yellow-bordered Panel via Rich - Config/fitting exceptions: replaced typer.echo(typer.style(...)) with red-bordered Panel for consistent Rich UX - Restores warnings.showwarning on all exit paths No frozen modules (report/, core/postprocessing.py) were modified. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 35a3194 commit ab8f5e9

1 file changed

Lines changed: 52 additions & 6 deletions

File tree

spectrafit/cli/commands/fit.py

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,58 @@ def fit(
7070
7171
$ spectrafit fit my_xps.toml --outfile xps_results --verbose 2
7272
"""
73+
import re
74+
import warnings
75+
7376
from rich.console import Console
77+
from rich.panel import Panel
7478
from rich.status import Status
79+
from rich.text import Text
7580

7681
console = Console()
82+
_original_showwarning = warnings.showwarning
83+
84+
def _rich_showwarning(
85+
message: warnings.WarningMessage | Warning | str,
86+
category: type[Warning],
87+
filename: str,
88+
lineno: int,
89+
file: object = None,
90+
line: str | None = None,
91+
) -> None:
92+
# Strip the legacy ASCII-art borders produced by report/metrics.py warn_meassage()
93+
raw = str(message)
94+
cleaned = re.sub(r"^[#\s]+$", "", raw, flags=re.MULTILINE)
95+
cleaned = re.sub(r"^##\s*WARNING\s*#+", "", cleaned, flags=re.MULTILINE)
96+
cleaned = cleaned.strip()
97+
console.print(
98+
Panel(
99+
Text(cleaned, style="yellow"),
100+
title=f"[bold yellow]⚠ {category.__name__}[/bold yellow]",
101+
title_align="left",
102+
border_style="yellow",
103+
expand=False,
104+
padding=(0, 1),
105+
)
106+
)
107+
108+
warnings.showwarning = _rich_showwarning
77109

78110
while True:
79111
__status__.start()
80112

81113
try:
82114
cfg = UnifiedFittingConfig.from_file(config)
83115
except Exception as exc:
84-
typer.echo(
85-
typer.style(f"Configuration error: {exc}", fg=typer.colors.RED),
86-
err=True,
116+
console.print(
117+
Panel(
118+
Text(str(exc), style="red"),
119+
title="[bold red]✗ Configuration error[/bold red]",
120+
title_align="left",
121+
border_style="red",
122+
expand=False,
123+
padding=(0, 1),
124+
)
87125
)
88126
raise typer.Exit(code=1) from exc
89127

@@ -99,9 +137,15 @@ def fit(
99137
args=cfg, output=output
100138
)
101139
except Exception as exc:
102-
typer.echo(
103-
typer.style(f"Fitting error: {exc}", fg=typer.colors.RED),
104-
err=True,
140+
console.print(
141+
Panel(
142+
Text(str(exc), style="red"),
143+
title="[bold red]✗ Fitting error[/bold red]",
144+
title_align="left",
145+
border_style="red",
146+
expand=False,
147+
padding=(0, 1),
148+
)
105149
)
106150
raise typer.Exit(code=1) from exc
107151

@@ -115,10 +159,12 @@ def fit(
115159
__status__.end()
116160

117161
if noplot:
162+
warnings.showwarning = _original_showwarning
118163
return
119164

120165
from spectrafit.cli._types import reset_keyboard_protocol
121166

122167
reset_keyboard_protocol()
123168
if not typer.confirm("Would you like to fit again?", default=False):
169+
warnings.showwarning = _original_showwarning
124170
return

0 commit comments

Comments
 (0)