2021-04-08 18:35:42 +00:00
|
|
|
{%- let section = "# =============================================================================\n#" -%}
|
|
|
|
{%- let not_configured = "# -- not configured --" -%}
|
2020-10-18 09:22:13 +00:00
|
|
|
|
2021-08-15 15:07:19 +00:00
|
|
|
# pylint: disable=missing-module-docstring
|
2020-11-10 19:11:26 +00:00
|
|
|
|
2021-07-22 17:47:35 +00:00
|
|
|
import builtins # pylint: disable=unused-import
|
2020-10-18 09:22:13 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
2020-10-20 17:59:28 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2021-10-28 06:53:14 +00:00
|
|
|
import typing
|
2020-11-10 19:11:26 +00:00
|
|
|
|
2021-03-01 06:10:10 +00:00
|
|
|
import xonsh.dirstack # type: ignore # pylint: disable=import-error
|
2021-03-30 13:14:29 +00:00
|
|
|
import xonsh.environ # type: ignore # pylint: disable=import-error
|
2020-10-18 09:22:13 +00:00
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2020-10-18 09:22:13 +00:00
|
|
|
# Utility functions for zoxide.
|
|
|
|
#
|
|
|
|
|
2020-11-10 19:11:26 +00:00
|
|
|
|
2021-03-30 13:14:29 +00:00
|
|
|
def __zoxide_bin() -> str:
|
|
|
|
"""Finds and returns the location of the zoxide binary."""
|
2021-10-28 06:53:14 +00:00
|
|
|
zoxide = typing.cast(str, xonsh.environ.locate_binary("zoxide"))
|
2021-03-30 13:14:29 +00:00
|
|
|
if zoxide is None:
|
|
|
|
zoxide = "zoxide"
|
|
|
|
return zoxide
|
|
|
|
|
|
|
|
|
2021-10-28 06:53:14 +00:00
|
|
|
def __zoxide_env() -> typing.Dict[str, str]:
|
2021-10-05 10:26:04 +00:00
|
|
|
"""Returns the current environment."""
|
|
|
|
return builtins.__xonsh__.env.detype() # type: ignore # pylint:disable=no-member
|
|
|
|
|
|
|
|
|
2020-10-18 09:22:13 +00:00
|
|
|
def __zoxide_pwd() -> str:
|
2020-11-10 19:11:26 +00:00
|
|
|
"""pwd based on the value of _ZO_RESOLVE_SYMLINKS."""
|
2020-10-18 09:22:13 +00:00
|
|
|
{%- if resolve_symlinks %}
|
2020-11-10 19:11:26 +00:00
|
|
|
pwd = os.getcwd()
|
2020-10-18 09:22:13 +00:00
|
|
|
{%- else %}
|
2021-10-05 10:26:04 +00:00
|
|
|
pwd = __zoxide_env().get("PWD")
|
2020-11-10 19:11:26 +00:00
|
|
|
if pwd is None:
|
2023-11-19 13:22:15 +00:00
|
|
|
raise RuntimeError("$PWD not found")
|
2020-10-18 09:22:13 +00:00
|
|
|
{%- endif %}
|
2020-11-10 19:11:26 +00:00
|
|
|
return pwd
|
|
|
|
|
|
|
|
|
2021-10-28 06:53:14 +00:00
|
|
|
def __zoxide_cd(path: typing.Optional[typing.AnyStr] = None) -> None:
|
2020-11-10 19:11:26 +00:00
|
|
|
"""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:
|
2023-11-19 13:22:15 +00:00
|
|
|
raise RuntimeError(exc)
|
2020-11-10 19:11:26 +00:00
|
|
|
{%- if echo %}
|
|
|
|
print(__zoxide_pwd())
|
|
|
|
{%- endif %}
|
|
|
|
|
|
|
|
|
|
|
|
class ZoxideSilentException(Exception):
|
|
|
|
"""Exit without complaining."""
|
|
|
|
|
|
|
|
|
2021-10-28 06:53:14 +00:00
|
|
|
def __zoxide_errhandler(
|
|
|
|
func: typing.Callable[[typing.List[str]], None]
|
|
|
|
) -> typing.Callable[[typing.List[str]], int]:
|
2020-11-10 19:11:26 +00:00
|
|
|
"""Print exception and exit with error code 1."""
|
|
|
|
|
2021-10-28 06:53:14 +00:00
|
|
|
def wrapper(args: typing.List[str]) -> int:
|
2020-11-10 19:11:26 +00:00
|
|
|
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
|
2020-10-18 09:22:13 +00:00
|
|
|
|
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2020-10-18 09:22:13 +00:00
|
|
|
# Hook configuration for zoxide.
|
|
|
|
#
|
|
|
|
|
2021-09-06 21:11:17 +00:00
|
|
|
{% if hook == InitHook::None -%}
|
|
|
|
{{ not_configured }}
|
|
|
|
|
|
|
|
{%- else -%}
|
2020-10-18 09:22:13 +00:00
|
|
|
# Initialize hook to add new entries to the database.
|
2021-09-06 21:11:17 +00:00
|
|
|
if "__zoxide_hook" not in globals():
|
|
|
|
{% if hook == InitHook::Prompt %}
|
2021-07-22 17:47:35 +00:00
|
|
|
@builtins.events.on_post_prompt # type: ignore # pylint:disable=no-member
|
2021-09-06 21:11:17 +00:00
|
|
|
{%- else if hook == InitHook::Pwd %}
|
2021-07-22 17:47:35 +00:00
|
|
|
@builtins.events.on_chdir # type: ignore # pylint:disable=no-member
|
2021-09-06 21:11:17 +00:00
|
|
|
{%- endif %}
|
2021-10-28 06:53:14 +00:00
|
|
|
def __zoxide_hook(**_kwargs: typing.Any) -> None:
|
2021-03-06 12:55:13 +00:00
|
|
|
"""Hook to add new entries to the database."""
|
|
|
|
pwd = __zoxide_pwd()
|
2021-03-30 13:14:29 +00:00
|
|
|
zoxide = __zoxide_bin()
|
2021-10-05 10:26:04 +00:00
|
|
|
subprocess.run(
|
|
|
|
[zoxide, "add", "--", pwd],
|
|
|
|
check=False,
|
|
|
|
env=__zoxide_env(),
|
|
|
|
)
|
2021-12-20 15:59:11 +00:00
|
|
|
{% endif %}
|
2021-09-06 21:11:17 +00:00
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2022-04-22 07:41:11 +00:00
|
|
|
# When using zoxide with --no-cmd, alias these internal functions as desired.
|
2020-10-18 09:22:13 +00:00
|
|
|
#
|
|
|
|
|
2020-10-20 17:59:28 +00:00
|
|
|
|
2020-11-10 19:11:26 +00:00
|
|
|
@__zoxide_errhandler
|
2021-10-28 06:53:14 +00:00
|
|
|
def __zoxide_z(args: typing.List[str]) -> None:
|
2020-11-10 19:11:26 +00:00
|
|
|
"""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:
|
2020-10-20 17:59:28 +00:00
|
|
|
try:
|
2021-03-30 13:14:29 +00:00
|
|
|
zoxide = __zoxide_bin()
|
2021-09-04 10:31:50 +00:00
|
|
|
cmd = subprocess.run(
|
2021-04-04 14:36:44 +00:00
|
|
|
[zoxide, "query", "--exclude", __zoxide_pwd(), "--"] + args,
|
|
|
|
check=True,
|
2021-10-05 10:26:04 +00:00
|
|
|
env=__zoxide_env(),
|
2021-04-04 14:36:44 +00:00
|
|
|
stdout=subprocess.PIPE,
|
2020-11-10 19:11:26 +00:00
|
|
|
)
|
2021-07-22 17:47:35 +00:00
|
|
|
except subprocess.CalledProcessError as exc:
|
2020-11-10 19:11:26 +00:00
|
|
|
raise ZoxideSilentException() from exc
|
2020-10-20 17:59:28 +00:00
|
|
|
|
2021-09-04 10:31:50 +00:00
|
|
|
result = cmd.stdout[:-1]
|
|
|
|
__zoxide_cd(result)
|
2020-10-18 09:22:13 +00:00
|
|
|
|
2020-10-20 17:59:28 +00:00
|
|
|
|
2021-10-05 10:26:04 +00:00
|
|
|
@__zoxide_errhandler
|
2021-10-28 06:53:14 +00:00
|
|
|
def __zoxide_zi(args: typing.List[str]) -> None:
|
2020-11-10 19:11:26 +00:00
|
|
|
"""Jump to a directory using interactive search."""
|
2020-10-20 17:59:28 +00:00
|
|
|
try:
|
2021-03-30 13:14:29 +00:00
|
|
|
zoxide = __zoxide_bin()
|
2021-09-04 10:31:50 +00:00
|
|
|
cmd = subprocess.run(
|
2021-10-05 10:26:04 +00:00
|
|
|
[zoxide, "query", "-i", "--"] + args,
|
|
|
|
check=True,
|
|
|
|
env=__zoxide_env(),
|
|
|
|
stdout=subprocess.PIPE,
|
2020-11-10 19:11:26 +00:00
|
|
|
)
|
2021-07-22 17:47:35 +00:00
|
|
|
except subprocess.CalledProcessError as exc:
|
2020-11-10 19:11:26 +00:00
|
|
|
raise ZoxideSilentException() from exc
|
2020-10-20 17:59:28 +00:00
|
|
|
|
2021-09-04 10:31:50 +00:00
|
|
|
result = cmd.stdout[:-1]
|
|
|
|
__zoxide_cd(result)
|
2020-10-20 17:59:28 +00:00
|
|
|
|
2020-10-18 09:22:13 +00:00
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2022-04-10 22:11:51 +00:00
|
|
|
# Commands for zoxide. Disable these using --no-cmd.
|
2020-10-18 09:22:13 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
{%- match cmd %}
|
|
|
|
{%- when Some with (cmd) %}
|
|
|
|
|
2021-07-22 17:47:35 +00:00
|
|
|
builtins.aliases["{{cmd}}"] = __zoxide_z # type: ignore # pylint:disable=no-member
|
|
|
|
builtins.aliases["{{cmd}}i"] = __zoxide_zi # type: ignore # pylint:disable=no-member
|
2020-10-18 09:22:13 +00:00
|
|
|
|
|
|
|
{%- when None %}
|
2021-04-08 18:35:42 +00:00
|
|
|
|
|
|
|
{{ not_configured }}
|
2020-10-18 09:22:13 +00:00
|
|
|
|
|
|
|
{%- endmatch %}
|
|
|
|
|
2021-04-08 18:35:42 +00:00
|
|
|
{{ section }}
|
2021-04-27 12:07:38 +00:00
|
|
|
# To initialize zoxide, add this to your configuration (usually ~/.xonshrc):
|
2020-10-18 09:22:13 +00:00
|
|
|
#
|
|
|
|
# execx($(zoxide init xonsh), 'exec', __xonsh__.ctx, filename='zoxide')
|