mirror of
https://github.com/Llewellynvdm/zoxide.git
synced 2024-11-10 23:30:57 +00:00
201 lines
5.3 KiB
Plaintext
201 lines
5.3 KiB
Plaintext
{%- let SECTION = "# =============================================================================\n#" -%}
|
|
{%- let NOT_CONFIGURED = "# -- not configured --" -%}
|
|
|
|
"""Initialize zoxide on Xonsh."""
|
|
|
|
import os
|
|
import os.path
|
|
import subprocess
|
|
import sys
|
|
{%- if cmd.is_some() %}
|
|
from builtins import aliases # type: ignore # pylint: disable=no-name-in-module
|
|
{%- endif %}
|
|
{%- if hook != Hook::None %}
|
|
from builtins import events # type: ignore # pylint: disable=no-name-in-module
|
|
{%- endif %}
|
|
from subprocess import CalledProcessError
|
|
from typing import AnyStr, List, Optional
|
|
|
|
import xonsh.dirstack # type: ignore
|
|
|
|
{{ SECTION }}
|
|
# Utility functions for zoxide.
|
|
#
|
|
|
|
|
|
def __zoxide_pwd() -> str:
|
|
"""pwd based on the value of _ZO_RESOLVE_SYMLINKS."""
|
|
{%- if resolve_symlinks %}
|
|
pwd = os.getcwd()
|
|
{%- else %}
|
|
pwd = os.getenv("PWD")
|
|
if pwd is None:
|
|
raise Exception("$PWD not found in env")
|
|
{%- endif %}
|
|
return pwd
|
|
|
|
|
|
def __zoxide_cd(path: Optional[AnyStr] = None):
|
|
"""cd + custom logic based on the value of _ZO_ECHO."""
|
|
if path is None:
|
|
args = []
|
|
elif isinstance(path, bytes):
|
|
args = [path.decode("utf-8")]
|
|
elif isinstance(path, str):
|
|
args = [path]
|
|
_, exc, _ = xonsh.dirstack.cd(args)
|
|
if exc is not None:
|
|
raise Exception(exc)
|
|
{%- if echo %}
|
|
print(__zoxide_pwd())
|
|
{%- endif %}
|
|
|
|
|
|
class ZoxideSilentException(Exception):
|
|
"""Exit without complaining."""
|
|
|
|
|
|
def __zoxide_errhandler(func):
|
|
"""Print exception and exit with error code 1."""
|
|
|
|
def wrapper(args: List[str]):
|
|
try:
|
|
func(args)
|
|
return 0
|
|
except ZoxideSilentException:
|
|
return 1
|
|
except Exception as exc: # pylint: disable=broad-except
|
|
print(f"zoxide: {exc}", file=sys.stderr)
|
|
return 1
|
|
|
|
return wrapper
|
|
|
|
|
|
{{ SECTION }}
|
|
# Hook configuration for zoxide.
|
|
#
|
|
|
|
# Initialize hook to add new entries to the database.
|
|
{%- match hook %}
|
|
{%- when Hook::None %}
|
|
{{ NOT_CONFIGURED }}
|
|
|
|
{%- when Hook::Prompt %}
|
|
@events.on_post_prompt # type: ignore # pylint:disable=undefined-variable
|
|
|
|
{%- when Hook::Pwd %}
|
|
@events.on_chdir # type: ignore # pylint:disable=undefined-variable
|
|
|
|
{%- endmatch %}
|
|
def __zoxide_hook(**_kwargs):
|
|
"""Hook to add new entries to the database."""
|
|
pwd = __zoxide_pwd()
|
|
subprocess.run(["zoxide", "add", pwd], check=False)
|
|
|
|
|
|
{{ SECTION }}
|
|
# When using zoxide with --no-aliases, alias these internal functions as
|
|
# desired.
|
|
#
|
|
|
|
|
|
@__zoxide_errhandler
|
|
def __zoxide_z(args: List[str]):
|
|
"""Jump to a directory using only keywords."""
|
|
if args == []:
|
|
__zoxide_cd()
|
|
elif args == ["-"]:
|
|
__zoxide_cd("-")
|
|
elif len(args) == 1 and os.path.isdir(args[0]):
|
|
__zoxide_cd(args[0])
|
|
else:
|
|
try:
|
|
__zoxide_cmd = subprocess.run(
|
|
["zoxide", "query", "--"] + args, check=True, stdout=subprocess.PIPE
|
|
)
|
|
except CalledProcessError as exc:
|
|
raise ZoxideSilentException() from exc
|
|
|
|
__zoxide_result = __zoxide_cmd.stdout[:-1]
|
|
__zoxide_cd(__zoxide_result)
|
|
|
|
|
|
def __zoxide_zi(args: List[str]):
|
|
"""Jump to a directory using interactive search."""
|
|
try:
|
|
__zoxide_cmd = subprocess.run(
|
|
["zoxide", "query", "-i", "--"] + args, check=True, stdout=subprocess.PIPE
|
|
)
|
|
except CalledProcessError as exc:
|
|
raise ZoxideSilentException() from exc
|
|
|
|
__zoxide_result = __zoxide_cmd.stdout[:-1]
|
|
__zoxide_cd(__zoxide_result)
|
|
|
|
|
|
def __zoxide_za(args: List[str]):
|
|
"""Add a new entry to the database."""
|
|
try:
|
|
subprocess.run(["zoxide", "add"] + args, check=True)
|
|
except CalledProcessError as exc:
|
|
raise ZoxideSilentException from exc
|
|
|
|
|
|
def __zoxide_zq(args: List[str]):
|
|
"""Query an entry from the database using only keywords."""
|
|
try:
|
|
subprocess.run(["zoxide", "query"] + args, check=True)
|
|
except CalledProcessError as exc:
|
|
raise ZoxideSilentException from exc
|
|
|
|
|
|
def __zoxide_zqi(args: List[str]):
|
|
"""Query an entry from the database using interactive selection."""
|
|
try:
|
|
subprocess.run(["zoxide", "query", "-i"] + args, check=True)
|
|
except CalledProcessError as exc:
|
|
raise ZoxideSilentException from exc
|
|
|
|
|
|
def __zoxide_zr(args: List[str]):
|
|
"""Remove an entry from the database using the exact path."""
|
|
try:
|
|
subprocess.run(["zoxide", "remove"] + args, check=True)
|
|
except CalledProcessError as exc:
|
|
raise ZoxideSilentException from exc
|
|
|
|
|
|
def __zoxide_zri(args: List[str]):
|
|
"""Remove an entry from the database using interactive selection."""
|
|
try:
|
|
subprocess.run(["zoxide", "remove", "-i"] + args, check=True)
|
|
except CalledProcessError as exc:
|
|
raise ZoxideSilentException from exc
|
|
|
|
|
|
{{ SECTION }}
|
|
# Convenient aliases for zoxide. Disable these using --no-aliases.
|
|
#
|
|
|
|
{%- match cmd %}
|
|
{%- when Some with (cmd) %}
|
|
|
|
aliases["{{cmd}}"] = __zoxide_z
|
|
aliases["{{cmd}}i"] = __zoxide_zi
|
|
aliases["{{cmd}}a"] = __zoxide_za
|
|
aliases["{{cmd}}q"] = __zoxide_zq
|
|
aliases["{{cmd}}qi"] = __zoxide_zqi
|
|
aliases["{{cmd}}r"] = __zoxide_zr
|
|
aliases["{{cmd}}ri"] = __zoxide_zri
|
|
|
|
{%- when None %}
|
|
{{ NOT_CONFIGURED }}
|
|
|
|
{%- endmatch %}
|
|
|
|
{{ SECTION }}
|
|
# To initialize zoxide with xonsh, add the following line to your xonsh
|
|
# configuration file (usually ~/.xonshrc):
|
|
#
|
|
# execx($(zoxide init xonsh), 'exec', __xonsh__.ctx, filename='zoxide')
|