Skip to content

Commit

Permalink
feat: hide home dir
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Feb 13, 2024
1 parent 2ef1d79 commit 3dabd41
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/ape/managers/project/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ape.managers.project.types import ApeProject, BrownieProject
from ape.utils import get_relative_path
from ape.utils.basemodel import _assert_not_ipython_check
from ape.utils.os import clean_path


class ProjectManager(BaseManager):
Expand Down Expand Up @@ -55,11 +56,11 @@ def __init__(
self.path = self.path.parent

def __str__(self) -> str:
return f'Project("{self.path}")'
return f'Project("{clean_path(self.path)}")'

def __repr__(self):
try:
path = f" {self.path}"
path = f" {clean_path(self.path)}"
except Exception:
# Disallow exceptions in __repr__
path = ""
Expand Down
4 changes: 4 additions & 0 deletions src/ape/utils/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ def __exit__(self, *exc):
for path in self.exclude:
if path not in sys.path:
sys.path.append(path)


def clean_path(path: Path) -> str:
return f"{path.relative_to(Path.home())}"
9 changes: 9 additions & 0 deletions src/ape_console/plugin.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import shlex
from pathlib import Path

import click
from click.testing import CliRunner
from eth_utils import is_hex
from IPython import get_ipython
from IPython.core.magic import Magics, line_magic, magics_class
from rich import print as rich_print

import ape
from ape._cli import cli
from ape.exceptions import Abort, ApeException, handle_ape_exception
from ape.logging import logger
from ape.types import AddressType
from ape.utils import cached_property
from ape.utils.os import clean_path


@magics_class
Expand Down Expand Up @@ -82,3 +85,9 @@ def custom_exception_handler(self, etype, value, tb, tb_offset=None):
def load_ipython_extension(ipython):
ipython.register_magics(ApeConsoleMagics)
ipython.set_custom_exc((ApeException,), custom_exception_handler)

# This prevents displaying a user's home directory
# ever when using `ape console`.
ipython.display_formatter.formatters["text/plain"].for_type(
Path, lambda x, *args, **kwargs: rich_print(clean_path(x))
)

0 comments on commit 3dabd41

Please sign in to comment.