zoxide/templates/cmd.txt

149 lines
4.5 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::None -%}
{%- call batch::indent(tabs) %} ^&^& (for /f "delims=" %$p%a in ('"%__builtin_pwd%"') do @(zoxide add -- "%$p%~fa\."))^
{%- 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 --" -%}
@echo off & setlocal DisableDelayedExpansion EnableExtensions
@if "%~f0"=="%~dpnx0" (
set ^"$p=%%<nul"
) else (
set ^"$p=^%<nul"
)
set ^"$true=(call )"
set ^"$false=(call)"
set root=%SystemRoot%\System32
set ^"$doskey=%root%\doskey.exe"
{%- if resolve_symlinks %}
set ^"$fsutil=%root%\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%
@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_z_prefix={{cmd}}
@rem Jump to a directory using only keywords.
"%$doskey%" %__zoxide_z_prefix% = (^
{% call batch::for_caret("%$p%", "i") ~%} @(^
if "%$p%~i"=="" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) %}^
) else (^
(echo(%__zoxide_z_prefix%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="~" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) %}^
) else (^
(echo(%__zoxide_z_prefix%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="-" (^
if defined OLDPWD (^
{% call cd("%$p%OLDPWD%$p%", 4) %}^
) else (^
(echo(%__zoxide_z_prefix%: OLDPWD is not defined) ^>^&2 ^& %$false%^
)^
) else (^
for /f "delims=" %$p%p in ('"zoxide query --exclude "%$p%CD%$p%\." -- %$p%~i"') do @(^
{% call cd("%$p%~fp", 4) %}^
)^
)^
)^
) ^&^& %$true%
@rem Jump to a directory using interactive search.
"%$doskey%" %__zoxide_z_prefix%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
{{ section }}
@rem To initialize zoxide, add this to your configuration or AutoRun command:
@rem
@rem zoxide init cmd | cmd /d >nul