Skip to content

Commit d630c7c

Browse files
committed
Add support for cmd.exe (experimental)
1 parent 03df9d4 commit d630c7c

File tree

8 files changed

+181
-3
lines changed

8 files changed

+181
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## Unreleased
1111

12+
- Support for `cmd.exe` (experimental).
13+
1214
### Added
1315

1416
- Fish/Zsh: aliases on `__zoxide_z` will now use completions.

contrib/completions/_zoxide

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contrib/completions/zoxide.bash

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contrib/completions/zoxide.ts

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/cmd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub enum InitHook {
132132
#[derive(ValueEnum, Clone, Debug)]
133133
pub enum InitShell {
134134
Bash,
135+
Cmd,
135136
Elvish,
136137
Fish,
137138
Nushell,

src/cmd/init.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use askama::Template;
66
use crate::cmd::{Init, InitShell, Run};
77
use crate::config;
88
use crate::error::BrokenPipeHandler;
9-
use crate::shell::{Bash, Elvish, Fish, Nushell, Opts, Posix, Powershell, Xonsh, Zsh};
9+
use crate::shell::{Bash, Cmd, Elvish, Fish, Nushell, Opts, Posix, Powershell, Xonsh, Zsh};
1010

1111
impl Run for Init {
1212
fn run(&self) -> Result<()> {
@@ -17,6 +17,7 @@ impl Run for Init {
1717

1818
let source = match self.shell {
1919
InitShell::Bash => Bash(opts).render(),
20+
InitShell::Cmd => Cmd(opts).render(),
2021
InitShell::Elvish => Elvish(opts).render(),
2122
InitShell::Fish => Fish(opts).render(),
2223
InitShell::Nushell => Nushell(opts).render(),

src/shell.rs

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ macro_rules! make_template {
2424
}
2525

2626
make_template!(Bash, "bash.txt");
27+
make_template!(Cmd, "cmd.txt");
2728
make_template!(Elvish, "elvish.txt");
2829
make_template!(Fish, "fish.txt");
2930
make_template!(Nushell, "nushell.txt");
@@ -93,6 +94,22 @@ mod tests {
9394
.stderr("");
9495
}
9596

97+
#[apply(opts)]
98+
fn cmd_cmd(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) {
99+
let opts = Opts { cmd, hook, echo, resolve_symlinks };
100+
let mut source = Cmd(&opts).render().unwrap();
101+
102+
// @TODO test this sometime, somehow
103+
let tempfile = tempfile::tempfile();
104+
105+
Command::new("cmd")
106+
.args(["/c", &source])
107+
.assert()
108+
.success()
109+
.stdout("")
110+
.stderr("");
111+
}
112+
96113
#[apply(opts)]
97114
fn elvish_elvish(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) {
98115
let opts = Opts { cmd, hook, echo, resolve_symlinks };

templates/cmd.txt

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
{%- let section = "@rem ==========================================================================\n@rem" -%}
2+
{%- let not_configured = "@rem -- not configured --" -%}
3+
@setlocal EnableDelayedExpansion EnableExtensions & set "CMD=!CMDCMDLINE!" 2>nul
4+
@if /i not "!CMD!"=="!CMD:/=!" (goto :EOF) else @if not defined DEBUG (echo off)
5+
@reg query "HKCU\Software\Microsoft\Command Processor" /v "AutoRun" /z >nul 2>&1
6+
@if !ERRORLEVEL! equ 0 (endlocal & set "CMD_ENV=%~0") else (echo(cmd& goto :EOF)
7+
8+
{{ section }}
9+
@rem Utility functions for zoxide.
10+
@rem
11+
12+
@rem Full credits to jeb for this - https://stackoverflow.com/a/76213522/6724141
13+
set i_AS_$*=%%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^")
14+
15+
set __ZOXIDE_CD=chdir /d
16+
set __ZOXIDE_PWD=chdir
17+
18+
@rem cd + custom logic based on the value of _ZO_ECHO.
19+
doskey cd = ( ^
20+
{#- Full credits to jeb (see https://stackoverflow.com/a/76213522/6724141) #}
21+
for %%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") do @( ^
22+
if "%%~i"=="" ( ^
23+
if defined USERPROFILE ( ^
24+
if /i not "%%CD%%"=="%%USERPROFILE%%" ( ^
25+
%__ZOXIDE_CD% "%%USERPROFILE%%" ^&^& ^
26+
}
27+
{%- if echo %}
28+
%__ZOXIDE_PWD% ^&^& ^
29+
{%- endif %}
30+
{% if hook != InitHook::None -%}
31+
if defined __ZOXIDE_HOOK (%%__ZOXIDE_HOOK%%) ^&^& ^
32+
{%- endif %}
33+
set "OLDCD=%%CD%%" ^
34+
) ^
35+
) ^
36+
) else if "%%~i"=="-" ( ^
37+
if defined OLDCD ( ^
38+
if /i not "%%CD%%"=="%%OLDCD%%" ( ^
39+
%__ZOXIDE_CD% "%%OLDCD%%" ^&^& ^
40+
{%- if echo %}
41+
%__ZOXIDE_PWD% ^&^& ^
42+
{%- endif %}
43+
{% if hook != InitHook::None -%}
44+
if defined __ZOXIDE_HOOK (%%__ZOXIDE_HOOK%%) ^&^& ^
45+
{%- endif %}
46+
set "OLDCD=%%CD%%" ^
47+
) ^
48+
) ^
49+
) else ( ^
50+
( ^
51+
if /i not "%%CD%%"=="%%~fi" ( ^
52+
%__ZOXIDE_CD% %%~fi ^&^& ^
53+
{%- if echo %}
54+
%__ZOXIDE_PWD% ^&^& ^
55+
{%- endif %}
56+
{% if hook != InitHook::None -%}
57+
if defined __ZOXIDE_HOOK (%%__ZOXIDE_HOOK%%) ^&^& ^
58+
{%- endif %}
59+
set "OLDCD=%%CD%%" ^
60+
) ^
61+
) ^
62+
) ^
63+
) ^
64+
)
65+
66+
doskey pwd = (%__ZOXIDE_PWD%)
67+
68+
{{ section }}
69+
@rem Hook configuration for zoxide.
70+
@rem
71+
72+
if not defined __ZOXIDE_HOOKED (
73+
set __ZOXIDE_HOOKED=1
74+
{% if hook == InitHook::None -%}
75+
@rem {{ not_configured }}
76+
set __ZOXIDE_HOOK=
77+
{%- else -%}
78+
@rem Initialize hook to add new entries to the database.
79+
set __ZOXIDE_HOOK=for /f "delims=" %%z in ('%__ZOXIDE_PWD%') do (zoxide add -- "%%~fz ")
80+
{%- endif %}
81+
)
82+
83+
{{ section }}
84+
@rem Commands for zoxide. Disable these using --no-cmd.
85+
@rem
86+
87+
{%- match cmd %}
88+
{%- when Some with (cmd) %}
89+
90+
@rem Jump to a directory using only keywords.
91+
doskey {{cmd}} = ( ^
92+
for %%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") do @( ^
93+
if "%%~i"=="" ( ^
94+
if defined HOME ( ^
95+
if /i not "%%CD%%"=="%%HOME%%" ( ^
96+
%__CD% "%%HOME%%" ^&^& ^
97+
{%- if echo %}
98+
%__ZOXIDE_PWD% ^&^& ^
99+
{%- endif %}
100+
set "OLDCD=%%CD%%" ^&^& ^
101+
%__ZOXIDE_HOOK% ^
102+
) ^
103+
) ^
104+
) else if "%%~i"=="-" ( ^
105+
if defined OLDCD ( ^
106+
if /i not "%%CD%%"=="%%OLDCD%%" ( ^
107+
%__CD% "%%OLDCD%%" ^&^& ^
108+
{%- if echo %}
109+
%__ZOXIDE_PWD% ^&^& ^
110+
{%- endif %}
111+
set "OLDCD=%%CD%%" ^&^& ^
112+
%__ZOXIDE_HOOK% ^
113+
) ^
114+
) ^
115+
) else ( ^
116+
for /f "delims=" %%p in ('zoxide query --exclude "%%CD%%" -- "%%~i"') do @( ^
117+
if /i not "%%CD%%"=="%%~fp" ( ^
118+
%__CD% %%~fp ^&^& ^
119+
{%- if echo %}
120+
%__ZOXIDE_PWD% ^&^& ^
121+
{%- endif %}
122+
set "OLDCD=%%CD%%" ^&^& ^
123+
%__ZOXIDE_HOOK% ^
124+
) ^
125+
) ^
126+
) ^
127+
) ^
128+
)
129+
130+
@rem Jump to a directory using interactive search.
131+
doskey {{cmd}}i = ( ^
132+
for %%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") do @( ^
133+
for /f "delims=" %%q in ('zoxide query --interactive -- "%%~i"') do @( ^
134+
%__CD% %%~fq ^&^& ^
135+
{%- if echo %}
136+
%__ZOXIDE_PWD% ^&^& ^
137+
{%- endif %}
138+
set "OLDCD=%%CD%%" ^&^& ^
139+
%__ZOXIDE_HOOK% ^
140+
) ^
141+
) ^
142+
)
143+
144+
{%- when None %}
145+
146+
{{ not_configured }}
147+
148+
{%- endmatch %}
149+
150+
{{ section }}
151+
@rem To initialize zoxide, add the contents of the following command to your
152+
@rem configuration:
153+
@rem
154+
@rem zoxide init cmd
155+
@rem
156+
@rem If you don't have one: <TODO: explain how to run `AutoRun` scripts>

0 commit comments

Comments
 (0)