From 51b11850f3db3ecacd43dc948ec225266e961d84 Mon Sep 17 00:00:00 2001 From: mataha Date: Mon, 12 Jun 2023 01:19:24 +0200 Subject: [PATCH] Quit if Command Extensions are disabled --- README.md | 5 +---- src/shell.rs | 16 +++++++++++++++- templates/cmd.txt | 10 ++++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e04d7ed7..88355245 100644 --- a/README.md +++ b/README.md @@ -186,11 +186,8 @@ zoxide can be installed in 4 easy steps: > Add this to the **end** of your config file or AutoRun command: > > ```batchfile - > zoxide init cmd | cmd /d >nul + > zoxide init cmd | cmd /d /k >nul > ``` - > - > **Note** - > zoxide only supports `cmd.exe` with Command Extensions v2 and above. diff --git a/src/shell.rs b/src/shell.rs index 79fe86ec..d333885d 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -104,13 +104,27 @@ mod tests { let source = Cmd(&opts).render().unwrap(); Command::new("cmd.exe") - .args(["/a", "/d", "/e:on", "/q", "/v:off", "/k", "@doskey", "/macros:all"]) + .args(["/a", "/d", "/e:on", "/q", "/v:off", "/k", "@doskey /macros:cmd.exe"]) .write_stdin(source) .assert() .success() .stderr(""); } + #[apply(opts)] + #[cfg(windows)] + fn cmd_dos(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { + let opts = Opts { cmd, hook, echo, resolve_symlinks }; + let source = Cmd(&opts).render().unwrap(); + + Command::new("cmd.exe") + .args(["/a", "/d", "/e:off", "/q", "/v:off", "/k", "@doskey /macros:cmd.exe"]) + .write_stdin(source) + .assert() + .failure() + .stderr("zoxide: unable to init with Command Extensions disabled (see `help cmd` for details)"); + } + #[apply(opts)] #[cfg(unix)] fn elvish_elvish(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) { diff --git a/templates/cmd.txt b/templates/cmd.txt index d25132d4..90653d0a 100644 --- a/templates/cmd.txt +++ b/templates/cmd.txt @@ -32,7 +32,11 @@ {%- let section = "@rem ==========================================================================\n@rem" -%} {%- let not_configured = "@rem -- not configured --" -%} -@echo off & setlocal DisableDelayedExpansion EnableExtensions +@(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 /i "%~f0"=="%~dpnx0" ( set ^"$p=%%nul -@rem -@rem Note: zoxide only supports `cmd.exe` with Command Extensions v2 and above. +@rem zoxide init cmd | cmd /d /k >nul