Find zoxide in Xonsh shells (#168)

This commit is contained in:
Ajeet D'Souza 2021-03-30 18:44:29 +05:30 committed by GitHub
parent c4aebf6041
commit e2506631e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,12 +17,21 @@ from subprocess import CalledProcessError
from typing import AnyStr, List, Optional from typing import AnyStr, List, Optional
import xonsh.dirstack # type: ignore # pylint: disable=import-error import xonsh.dirstack # type: ignore # pylint: disable=import-error
import xonsh.environ # type: ignore # pylint: disable=import-error
{{ SECTION }} {{ SECTION }}
# Utility functions for zoxide. # Utility functions for zoxide.
# #
def __zoxide_bin() -> str:
"""Finds and returns the location of the zoxide binary."""
zoxide = xonsh.environ.locate_binary("zoxide")
if zoxide is None:
zoxide = "zoxide"
return zoxide
def __zoxide_pwd() -> str: def __zoxide_pwd() -> str:
"""pwd based on the value of _ZO_RESOLVE_SYMLINKS.""" """pwd based on the value of _ZO_RESOLVE_SYMLINKS."""
{%- if resolve_symlinks %} {%- if resolve_symlinks %}
@ -90,7 +99,8 @@ if globals().get("__zoxide_hooked") is not True:
def __zoxide_hook(**_kwargs): def __zoxide_hook(**_kwargs):
"""Hook to add new entries to the database.""" """Hook to add new entries to the database."""
pwd = __zoxide_pwd() pwd = __zoxide_pwd()
subprocess.run(["zoxide", "add", pwd], check=False) zoxide = __zoxide_bin()
subprocess.run([zoxide, "add", pwd], check=False)
{{ SECTION }} {{ SECTION }}
@ -110,8 +120,9 @@ def __zoxide_z(args: List[str]):
__zoxide_cd(args[0]) __zoxide_cd(args[0])
else: else:
try: try:
zoxide = __zoxide_bin()
__zoxide_cmd = subprocess.run( __zoxide_cmd = subprocess.run(
["zoxide", "query", "--"] + args, check=True, stdout=subprocess.PIPE [zoxide, "query", "--"] + args, check=True, stdout=subprocess.PIPE
) )
except CalledProcessError as exc: except CalledProcessError as exc:
raise ZoxideSilentException() from exc raise ZoxideSilentException() from exc
@ -123,8 +134,9 @@ def __zoxide_z(args: List[str]):
def __zoxide_zi(args: List[str]): def __zoxide_zi(args: List[str]):
"""Jump to a directory using interactive search.""" """Jump to a directory using interactive search."""
try: try:
zoxide = __zoxide_bin()
__zoxide_cmd = subprocess.run( __zoxide_cmd = subprocess.run(
["zoxide", "query", "-i", "--"] + args, check=True, stdout=subprocess.PIPE [zoxide, "query", "-i", "--"] + args, check=True, stdout=subprocess.PIPE
) )
except CalledProcessError as exc: except CalledProcessError as exc:
raise ZoxideSilentException() from exc raise ZoxideSilentException() from exc