Make percent characters work in all contexts

This commit is contained in:
mataha 2023-06-06 23:01:19 +02:00
parent 1a403b4322
commit 06c19d8d31
4 changed files with 138 additions and 127 deletions

View File

@ -213,6 +213,17 @@ zoxide can be installed in 4 easy steps:
</details> </details>
<details>
<summary>`cmd.exe`</summary>
> Add this to the **end** of your config file or AutoRun command:
>
> ```batchfile
> zoxide init cmd | cmd /d >nul
> ```
</details>
<details> <details>
<summary>Elvish</summary> <summary>Elvish</summary>

View File

@ -105,11 +105,10 @@ mod tests {
let source = Cmd(&opts).render().unwrap(); let source = Cmd(&opts).render().unwrap();
Command::new("cmd.exe") Command::new("cmd.exe")
.args(["/a", "/d", "/e:on", "/q", "/v:off", "/k", "@doskey", "/macros:cmd.exe"]) .args(["/a", "/d", "/e:on", "/q", "/v:off", "/k", "@doskey", "/macros:all"])
.write_stdin(source) .write_stdin(source)
.assert() .assert()
.success() .success()
.stdout("")
.stderr(""); .stderr("");
} }

View File

@ -1,84 +1,91 @@
{%- 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 section = "@rem ==========================================================================\n@rem" -%}
{%- let not_configured = "@rem -- not configured --" -%} {%- let not_configured = "@rem -- not configured --" -%}
@setlocal EnableDelayedExpansion EnableExtensions & set "CMD=!CMDCMDLINE!" 2>nul
@if /i not "!CMD!"=="!CMD:/=!" (goto :EOF) else @if not defined DEBUG (echo off) @echo off & setlocal EnableDelayedExpansion EnableExtensions
@reg query "HKCU\Software\Microsoft\Command Processor" /v "AutoRun" /z >nul 2>&1
@if !ERRORLEVEL! equ 0 (endlocal & set "CMD_ENV=%~0") else (echo(cmd& goto :EOF) @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 }} {{ section }}
@rem Utility functions for zoxide. @rem Utility functions for zoxide.
@rem @rem
@rem Full credits to jeb for this - https://stackoverflow.com/a/76213522/6724141 set __builtin_cd=chdir /d
set i_AS_$*=%%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") set __builtin_pwd=chdir
set __ZOXIDE_CD=chdir /d set __zoxide_cd=cd
set __ZOXIDE_PWD=chdir set __zoxide_pwd=pwd
@rem cd + custom logic based on the value of _ZO_ECHO. @rem cd + custom logic based on the value of _ZO_ECHO.
doskey cd = ( ^ "%$doskey%" %__zoxide_cd% = (^
{#- Full credits to jeb (see https://stackoverflow.com/a/76213522/6724141) #} {% call batch::for_caret("%$p%", "i") ~%} @(^
for %%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") do @( ^ if "%$p%~i"=="" (^
if "%%~i"=="" ( ^ if defined USERPROFILE (^
if defined USERPROFILE ( ^ {% call cd("%$p%USERPROFILE%$p%", 4) %}^
if /i not "%%CD%%"=="%%USERPROFILE%%" ( ^ ) else (^
%__ZOXIDE_CD% "%%USERPROFILE%%" ^&^& ^ (echo(%__zoxide_cd%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
} )^
{%- if echo %} ) else if "%$p%~i"=="~" (^
%__ZOXIDE_PWD% ^&^& ^ if defined USERPROFILE (^
{%- endif %} {% call cd("%$p%USERPROFILE%$p%", 4) %}^
{% if hook != InitHook::None -%} ) else (^
if defined __ZOXIDE_HOOK (%%__ZOXIDE_HOOK%%) ^&^& ^ (echo(%__zoxide_cd%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
{%- endif %} )^
set "OLDCD=%%CD%%" ^ ) else if "%$p%~i"=="-" (^
) ^ if defined OLDPWD (^
) ^ {% call cd("%$p%OLDPWD%$p%", 4) %}^
) else if "%%~i"=="-" ( ^ ) else (^
if defined OLDCD ( ^ (echo(%__zoxide_cd%: OLDPWD is not defined) ^>^&2 ^& %$false%^
if /i not "%%CD%%"=="%%OLDCD%%" ( ^ )^
%__ZOXIDE_CD% "%%OLDCD%%" ^&^& ^ ) else (^
{%- if echo %} (^
%__ZOXIDE_PWD% ^&^& ^ {% call cd("%$p%~fi", 4) %}^
{%- endif %} )^
{% if hook != InitHook::None -%} )^
if defined __ZOXIDE_HOOK (%%__ZOXIDE_HOOK%%) ^&^& ^ )^
{%- endif %} ) ^&^& %$true%
set "OLDCD=%%CD%%" ^
) ^
) ^
) else ( ^
( ^
if /i not "%%CD%%"=="%%~fi" ( ^
%__ZOXIDE_CD% %%~fi ^&^& ^
{%- if echo %}
%__ZOXIDE_PWD% ^&^& ^
{%- endif %}
{% if hook != InitHook::None -%}
if defined __ZOXIDE_HOOK (%%__ZOXIDE_HOOK%%) ^&^& ^
{%- endif %}
set "OLDCD=%%CD%%" ^
) ^
) ^
) ^
) ^
)
doskey pwd = (%__ZOXIDE_PWD%) @rem pwd based on the value of _ZO_RESOLVE_SYMLINKS.
"%$doskey%" %__zoxide_pwd% = {%~ call pwd(0) %}
{{ section }}
@rem Hook configuration for zoxide.
@rem
if not defined __ZOXIDE_HOOKED (
set __ZOXIDE_HOOKED=1
{% if hook == InitHook::None -%}
@rem {{ not_configured }}
set __ZOXIDE_HOOK=
{%- else -%}
@rem Initialize hook to add new entries to the database.
set __ZOXIDE_HOOK=for /f "delims=" %%z in ('%__ZOXIDE_PWD%') do (zoxide add -- "%%~fz ")
{%- endif %}
)
{{ section }} {{ section }}
@rem Commands for zoxide. Disable these using --no-cmd. @rem Commands for zoxide. Disable these using --no-cmd.
@ -87,59 +94,45 @@ if not defined __ZOXIDE_HOOKED (
{%- match cmd %} {%- match cmd %}
{%- when Some with (cmd) %} {%- when Some with (cmd) %}
set __zoxide_z_prefix={{cmd}}
@rem Jump to a directory using only keywords. @rem Jump to a directory using only keywords.
doskey {{cmd}} = ( ^ "%$doskey%" %__zoxide_z_prefix% = (^
for %%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") do @( ^ {% call batch::for_caret("%$p%", "i") ~%} @(^
if "%%~i"=="" ( ^ if "%$p%~i"=="" (^
if defined HOME ( ^ if defined USERPROFILE (^
if /i not "%%CD%%"=="%%HOME%%" ( ^ {% call cd("%$p%USERPROFILE%$p%", 4) %}^
%__CD% "%%HOME%%" ^&^& ^ ) else (^
{%- if echo %} (echo(%__zoxide_z_prefix%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
%__ZOXIDE_PWD% ^&^& ^ )^
{%- endif %} ) else if "%$p%~i"=="~" (^
set "OLDCD=%%CD%%" ^&^& ^ if defined USERPROFILE (^
%__ZOXIDE_HOOK% ^ {% call cd("%$p%USERPROFILE%$p%", 4) %}^
) ^ ) else (^
) ^ (echo(%__zoxide_z_prefix%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
) else if "%%~i"=="-" ( ^ )^
if defined OLDCD ( ^ ) else if "%$p%~i"=="-" (^
if /i not "%%CD%%"=="%%OLDCD%%" ( ^ if defined OLDPWD (^
%__CD% "%%OLDCD%%" ^&^& ^ {% call cd("%$p%OLDPWD%$p%", 4) %}^
{%- if echo %} ) else (^
%__ZOXIDE_PWD% ^&^& ^ (echo(%__zoxide_z_prefix%: OLDPWD is not defined) ^>^&2 ^& %$false%^
{%- endif %} )^
set "OLDCD=%%CD%%" ^&^& ^ ) else (^
%__ZOXIDE_HOOK% ^ for /f "delims=" %$p%p in ('"zoxide query --exclude "%$p%CD%$p%\." -- %$p%~i"') do @(^
) ^ {% call cd("%$p%~fp", 4) %}^
) ^ )^
) else ( ^ )^
for /f "delims=" %%p in ('zoxide query --exclude "%%CD%%" -- "%%~i"') do @( ^ )^
if /i not "%%CD%%"=="%%~fp" ( ^ ) ^&^& %$true%
%__CD% %%~fp ^&^& ^
{%- if echo %}
%__ZOXIDE_PWD% ^&^& ^
{%- endif %}
set "OLDCD=%%CD%%" ^&^& ^
%__ZOXIDE_HOOK% ^
) ^
) ^
) ^
) ^
)
@rem Jump to a directory using interactive search. @rem Jump to a directory using interactive search.
doskey {{cmd}}i = ( ^ "%$doskey%" %__zoxide_z_prefix%i = (^
for %%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") do @( ^ {% call batch::for_caret("%$p%", "i") ~%} @(^
for /f "delims=" %%q in ('zoxide query --interactive -- "%%~i"') do @( ^ for /f "delims=" %$p%p in ('"zoxide query --interactive -- %$p%~i"') do @(^
%__CD% %%~fq ^&^& ^ {% call cd("%$p%~fp", 3) %}^
{%- if echo %} )^
%__ZOXIDE_PWD% ^&^& ^ )^
{%- endif %} ) ^&^& %$true%
set "OLDCD=%%CD%%" ^&^& ^
%__ZOXIDE_HOOK% ^
) ^
) ^
)
{%- when None %} {%- when None %}
@ -147,10 +140,9 @@ doskey {{cmd}}i = ( ^
{%- endmatch %} {%- endmatch %}
@endlocal
{{ section }} {{ section }}
@rem To initialize zoxide, add the contents of the following command to your @rem To initialize zoxide, add this to your configuration or AutoRun command:
@rem configuration:
@rem @rem
@rem zoxide init cmd @rem zoxide init cmd | cmd /d >nul
@rem
@rem If you don't have one: <TODO: explain how to run `AutoRun` scripts>

View File

@ -0,0 +1,9 @@
{%- macro indent(tabs) %}
{% for tab in 0..tabs %} {% endfor -%}
{%- endmacro indent -%}
{%- macro for_caret(percent, for_parameter) -%}
{#- `for /f` statement with disappearing carets suitable for use in aliases. -#}
{#- Credits for this go to jeb: https://stackoverflow.com/a/76213522/6724141 -#}
for {{ percent }}^^^^ in ("") do @for /f "delims=" {{ percent }}{{ for_parameter }} in (^^""$*{{ percent }}~^^"^") do
{%- endmacro for_caret -%}