zoxide/templates/cmd.txt

212 lines
6.6 KiB
Plaintext

{%- import "utils/batch.txt" as batch -%}
{%- macro cd(directory, tabs) -%} if /i not "%$p%CD%$p%"=="{{ directory }}" (^
{%- call batch::indent(tabs) %} %__builtin_cd% {{ directory|safe }}^
{%- call batch::indent(tabs) %} ^&^& set "OLDPWD=%$p%CD%$p%"^
{%- if echo -%}
{%- call batch::indent(tabs) %} ^&^& {%~ call pwd(tabs + 1) -%}^
{%- endif -%}
{%- if hook == InitHook::Pwd -%}
{%- call batch::indent(tabs) %} ^&^& for /f "delims=" %$p%a in ('"%__builtin_pwd%"') do @if /i not "%$p%~dpa"=="%$p%~fa" (^
{%- call batch::indent(tabs) %} zoxide add -- "%$p%~fa"^
{%- call batch::indent(tabs) %} ) else (^
{%- call batch::indent(tabs) %} zoxide add -- "%$p%~fa\"^
{%- call batch::indent(tabs) %} )^
{%- endif -%}
{%- call batch::indent(tabs) %})
{%- endmacro cd -%}
{%- macro pwd(tabs) -%} (^
{%- if resolve_symlinks -%}
{%- call batch::indent(tabs) %} (^
{%- call batch::indent(tabs) %} for /f "skip=9 tokens=1,2,*" %$p%j in ('"%$fsutil% reparsepoint query ."') do @(^
{%- call batch::indent(tabs) %} if "%$p%~j"=="Print" if "%$p%~k"=="Name:" if not "%$p%~l"=="" (echo(%$p%~l)^
{%- call batch::indent(tabs) %} )^
{%- call batch::indent(tabs) %} ) ^|^| %__builtin_pwd%^
{%- else -%}
{%- call batch::indent(tabs) %} %__builtin_pwd%^
{%- endif -%}
{%- call batch::indent(tabs) %})
{%- endmacro pwd -%}
{%- let section = "@rem ==========================================================================\n@rem" -%}
{%- let not_configured = "@rem -- not configured --" -%}
@(set ^"^") >nul 2>nul && (echo off) || (
echo zoxide: unable to init with Command Extensions disabled ^(see `help cmd` for details^)
) >&2 && exit /b 1 "Command Extensions are disabled" 2>nul
setlocal DisableDelayedExpansion EnableExtensions
if "%=^%="=="%=%=" (
set ^"$p=%%<nul"
) else (
set ^"$p=^%<nul"
)
set ^"$true=(call )"
set ^"$false=(call)"
set ^"$doskey="%SystemRoot%\System32\doskey.exe""
{%- if resolve_symlinks %}
set ^"$fsutil="%SystemRoot%\System32\fsutil.exe""
{%- endif %}
{{ section }}
@rem Utility functions for zoxide.
@rem
set __builtin_cd=chdir /d
set __builtin_pwd=chdir
set __zoxide_cd=cd
set __zoxide_pwd=pwd
@rem cd + custom logic based on the value of _ZO_ECHO.
%$doskey% %__zoxide_cd% = (^
{% call batch::for_caret("%$p%", "i") ~%} @(^
if "%$p%~i"=="" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) -%}^
) else (^
(echo(%__zoxide_cd%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="~" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) -%}^
) else (^
(echo(%__zoxide_cd%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="-" (^
if defined OLDPWD (^
{% call cd("%$p%OLDPWD%$p%", 4) -%}^
) else (^
(echo(%__zoxide_cd%: OLDPWD is not defined) ^>^&2 ^& %$false%^
)^
) else (^
(^
{% call cd("%$p%~fi", 4) -%}^
)^
)^
)^
) ^&^& %$true%
%$doskey% %__zoxide_cd%~ = (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 2) -%}^
) else (^
(echo(%__zoxide_cd%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
)
%$doskey% %__zoxide_cd%- = (^
if defined OLDPWD (^
{% call cd("%$p%OLDPWD%$p%", 2) -%}^
) else (^
(echo(%__zoxide_cd%: OLDPWD is not defined) ^>^&2 ^& %$false%^
)^
)
%$doskey% %__zoxide_cd%\ = {%~ call cd("\\", 0) %}
%$doskey% %__zoxide_cd%/ = {%~ call cd("/", 0) %}
%$doskey% %__zoxide_cd%. = (%__builtin_cd%.)
%$doskey% %__zoxide_cd%.. = {%~ call cd("..", 0) %}
@rem pwd based on the value of _ZO_RESOLVE_SYMLINKS.
%$doskey% %__zoxide_pwd% = {%~ call pwd(0) %}
{{ section }}
@rem Commands for zoxide. Disable these using --no-cmd.
@rem
{%- match cmd %}
{%- when Some with (cmd) %}
set __zoxide_command={{cmd}}
@rem Jump to a directory using only keywords.
%$doskey% %__zoxide_command% = (^
{% call batch::for_caret("%$p%", "i") ~%} @(^
if "%$p%~i"=="" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) -%}^
) else (^
(echo(%__zoxide_command%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="~" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) -%}^
) else (^
(echo(%__zoxide_command%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="-" (^
if defined OLDPWD (^
{% call cd("%$p%OLDPWD%$p%", 4) -%}^
) else (^
(echo(%__zoxide_command%: OLDPWD is not defined) ^>^&2 ^& %$false%^
)^
) else for /f "tokens=1,* delims=d" %$p%a in ("-%$p%~ai") do @(^
if not "%$p%b"=="" (^
{% call cd("%$p%~fi", 4) -%}^
) else if /i not "%$p%CD%$p%"=="%$p%__CD__%$p%" (^
for /f "delims=" %$p%q in ('"zoxide query --exclude "%$p%CD%$p%" -- %$p%~i"') do @(^
{% call cd("%$p%~fq", 5) -%}^
)^
) else (^
for /f "delims=" %$p%q in ('"zoxide query --exclude "%$p%CD%$p%\" -- %$p%~i"') do @(^
{% call cd("%$p%~fq", 5) -%}^
)^
)^
)^
)^
) ^&^& %$true%
%$doskey% %__zoxide_command%~ = (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 2) -%}^
) else (^
(echo(%__zoxide_command%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
)
%$doskey% %__zoxide_command%- = (^
if defined OLDPWD (^
{% call cd("%$p%OLDPWD%$p%", 2) -%}^
) else (^
(echo(%__zoxide_command%: OLDPWD is not defined) ^>^&2 ^& %$false%^
)^
)
%$doskey% %__zoxide_command%\ = {%~ call cd("\\", 0) %}
%$doskey% %__zoxide_command%/ = {%~ call cd("/", 0) %}
%$doskey% %__zoxide_command%. = (%__builtin_cd%.)
%$doskey% %__zoxide_command%.. = {%~ call cd("..", 0) %}
@rem Jump to a directory using interactive search.
%$doskey% %__zoxide_command%i = (^
{% call batch::for_caret("%$p%", "i") ~%} @(^
for /f "delims=" %$p%p in ('"zoxide query --interactive -- %$p%~i"') do @(^
{% call cd("%$p%~fp", 3) -%}^
)^
)^
) ^&^& %$true%
{%- when None %}
{{ not_configured }}
{%- endmatch %}
endlocal & set OLDPWD=
{{ section }}
@rem To initialize zoxide, add this to your configuration or AutoRun command:
@rem
@rem zoxide init cmd | cmd /d /k >nul