Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: click styling-> rich #6178

Merged
merged 5 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions pipenv/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
from collections import namedtuple
from traceback import format_tb

from pipenv.patched.pip._vendor.rich.console import Console
from pipenv.patched.pip._vendor.rich.text import Text
from pipenv.vendor import click
from pipenv.vendor.click.exceptions import ClickException, FileError, UsageError


def unstyle(text: str) -> str:
"""Remove all styles from the given text."""
styled_text = Text.from_markup(text)
stripped_text = styled_text.strip_styles()
return stripped_text.to_plain_text()


KnownException = namedtuple(
"KnownException",
["exception_name", "match_string", "show_from_string", "prefix"],
Expand Down Expand Up @@ -62,13 +72,14 @@ def __init__(self, message=None, **kwargs):
def show(self, file=None):
if file is None:
file = sys.stderr
console = Console(file=file)
if self.extra:
if isinstance(self.extra, str):
self.extra = [self.extra]
for extra in self.extra:
extra = f"[pipenv.exceptions.{self.__class__.__name__}]: {extra}"
click.echo(extra, file=file)
click.echo(f"{self.message}", file=file)
console.print(extra)
console.print(f"{self.message}")
oz123 marked this conversation as resolved.
Show resolved Hide resolved


class PipenvCmdError(PipenvException):
Expand Down Expand Up @@ -275,11 +286,9 @@ def __init__(self, message=None, **kwargs):
self.message = message
extra = kwargs.pop("extra", None)
if extra is not None and isinstance(extra, str):
extra = click.unstyle(f"{extra}")
extra = unstyle(f"{extra}")
if "KeyboardInterrupt" in extra:
extra = click.style(
"Virtualenv creation interrupted by user", fg="red", bold=True
)
extra = "[red][/bold]Virtualenv creation interrupted by user[red][/bold]"
self.extra = extra = [extra]
VirtualenvException.__init__(self, message, extra=extra)

Expand Down
1 change: 1 addition & 0 deletions pipenv/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from pipenv.patched.pip._vendor.rich.console import Console
from pipenv.patched.pip._vendor.rich.prompt import Confirm # noqa

logging.basicConfig(level=logging.INFO)

Expand Down
13 changes: 5 additions & 8 deletions pipenv/utils/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,10 @@ def system_which(command, path=None):
env = {"PATH": path} if path else None
c = subprocess_run(f"{_which} {command}", shell=True, env=env)
if c.returncode == 127:
click.echo(
"{}: the {} system utility is required for Pipenv to find Python installations properly."
"\n Please install it.".format(
click.style("Warning", fg="red", bold=True),
click.style(_which, fg="yellow"),
),
err=True,
err.print(
f"[bold][red]Warning[/red][/bold]: the [yellow]{_which}[/yellow]"
"system utility is required for Pipenv to find Python installations properly."
"\nPlease install it."
)
if c.returncode == 0:
result = next(iter(c.stdout.splitlines()), None)
Expand All @@ -448,7 +445,7 @@ def shorten_path(location, bold=False):
short = short.split(os.sep)
short[-1] = original.split(os.sep)[-1]
if bold:
short[-1] = str(click.style(short[-1], bold=True))
short[-1] = f"[bold]{short[-1]}[/bold]"
return os.sep.join(short)


Expand Down
Loading
Loading