Skip to content

Commit 1ea9062

Browse files
committed
use rich for formatting exceptions
1 parent 4d861ca commit 1ea9062

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dependencies = [
3838
"tomlkit~=0.13",
3939
"google-generativeai~=0.8",
4040
"pygments~=2.19",
41+
"rich>=13.9.4",
4142
]
4243

4344
[dependency-groups]

src/shelloracle/cli/__init__.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from __future__ import annotations
22

3+
import asyncio
34
import logging
5+
import os
46
import sys
57
from typing import TYPE_CHECKING
68

79
import click
810

9-
from shelloracle import shelloracle
1011
from shelloracle.cli.application import Application
1112
from shelloracle.cli.config import config
1213
from shelloracle.config import Configuration
14+
from shelloracle.shelloracle import shelloracle
1315
from shelloracle.tty_log_handler import TtyLogHandler
1416

1517
if TYPE_CHECKING:
@@ -55,11 +57,21 @@ def cli(ctx: click.Context):
5557
logger.warning("Configuration not found. Run `shor config init` to initialize.")
5658
sys.exit(1)
5759

58-
shelloracle.cli(app)
60+
asyncio.run(shelloracle(app))
5961

6062

6163
cli.add_command(config)
6264

6365

6466
def main():
65-
cli()
67+
try:
68+
cli()
69+
except Exception: # noqa: BLE001
70+
import sys
71+
72+
from rich.console import Console
73+
74+
console = Console(stderr=True)
75+
shor_debug = os.getenv("SHOR_DEBUG") in {"1", "true"}
76+
console.print_exception(suppress=[click, asyncio], show_locals=shor_debug)
77+
sys.exit(1)

src/shelloracle/shelloracle.py

-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import asyncio
43
import logging
54
import os
65
import sys
@@ -87,16 +86,3 @@ async def shelloracle(app: Application) -> None:
8786
sp.text = shell_command
8887
logger.info("generated shell command: %s", shell_command)
8988
sys.stdout.write(shell_command)
90-
91-
92-
def cli(app: Application) -> None:
93-
"""Run the ShellOracle command line interface
94-
95-
:returns: None
96-
"""
97-
try:
98-
asyncio.run(shelloracle(app))
99-
except (KeyboardInterrupt, asyncio.exceptions.CancelledError):
100-
return
101-
except Exception:
102-
logger.exception("An error occurred")

uv.lock

+38-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)