Skip to content

Commit

Permalink
refactor: click styling-> rich
Browse files Browse the repository at this point in the history
Replace in shell.py and virtualenv.py.
Also, replace in some exceptions.

Signed-off-by: Oz Tiram <[email protected]>
  • Loading branch information
oz123 committed Jun 16, 2024
1 parent 7493d18 commit e32a1af
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 125 deletions.
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}")


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
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

0 comments on commit e32a1af

Please sign in to comment.