Skip to content

Commit dc62fb8

Browse files
committed
WIP
1 parent cd0e1cd commit dc62fb8

File tree

5 files changed

+29
-60
lines changed

5 files changed

+29
-60
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ indent_style = space
88
insert_final_newline = true
99
trim_trailing_whitespace = true
1010

11-
[*.(md,txt}]
11+
[*.(md,rst,txt}]
1212
indent_size = unset
1313
trim_trailing_whitespace = false
1414

man/man1/zoxide-init.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Add this to the \fBend\fR of your config file (usually \fB~/.bashrc\fR):
1313
\fBeval "$(zoxide init bash)"\fR
1414
.fi
1515
.TP
16-
.B cmd
16+
.B cmd.exe
1717
Add this to the \fBend\fR of your config file or AutoRun command:
1818
.sp
1919
.nf

src/shell.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ mod tests {
110110
.success();
111111

112112
if opts.hook != InitHook::None {
113-
assert.stderr("zoxide: hooks are not supported on cmd shell.\r\n");
113+
assert.stderr("zoxide: hooks are not supported on cmd.exe shell.\r\n");
114114
} else {
115115
assert.stderr("");
116116
}
@@ -129,7 +129,7 @@ mod tests {
129129
.success();
130130

131131
if opts.hook != InitHook::None {
132-
assert.stderr("zoxide: hooks are not supported on cmd shell.\r\n");
132+
assert.stderr("zoxide: hooks are not supported on cmd.exe shell.\r\n");
133133
} else {
134134
assert.stderr("");
135135
}

src/util.rs

+15-48
Original file line numberDiff line numberDiff line change
@@ -291,77 +291,44 @@ pub fn resolve_path(path: impl AsRef<Path>) -> Result<PathBuf> {
291291
}
292292
}
293293

294-
fn get_drive_prefix_path(drive_letter: u8) -> PathBuf {
295-
format!(r"{}:\", patch_drive_letter(drive_letter)).into()
294+
fn get_drive_path(drive_letter: u8) -> PathBuf {
295+
format!(r"{}:\", drive_letter as char).into()
296296
}
297297

298-
fn get_drive_relative_path(drive_letter: u8) -> Result<PathBuf> {
298+
fn get_drive_relative(drive_letter: u8) -> Result<PathBuf> {
299299
let path = current_dir()?;
300300
if Some(drive_letter) == get_drive_letter(&path) {
301-
return Ok(patch_drive_prefix(path));
301+
return Ok(path);
302302
}
303303

304-
if let Some(path) = env::var_os(format!("={}:", patch_drive_letter(drive_letter))) {
305-
return Ok(patch_drive_prefix(path.into()));
304+
if let Some(path) = env::var_os(format!("={}:", drive_letter as char)) {
305+
return Ok(path.into());
306306
}
307307

308-
let path = get_drive_prefix_path(drive_letter);
308+
let path = get_drive_path(drive_letter);
309309
Ok(path)
310310
}
311311

312-
#[inline(always)]
313-
fn patch_drive_letter(drive_letter: u8) -> char {
314-
drive_letter.to_ascii_uppercase() as char
315-
}
316-
317-
// https://github.com/rust-lang/rust-analyzer/pull/14689
318-
fn patch_drive_prefix(path: PathBuf) -> PathBuf {
319-
let mut components = path.components();
320-
321-
match components.next() {
322-
Some(Component::Prefix(prefix)) => {
323-
let prefix = match prefix.kind() {
324-
Prefix::Disk(drive_letter) => {
325-
format!(r"{}:", patch_drive_letter(drive_letter))
326-
}
327-
Prefix::VerbatimDisk(drive_letter) => {
328-
format!(r"\\?\{}:", patch_drive_letter(drive_letter))
329-
}
330-
_ => return path,
331-
};
332-
333-
let mut path = PathBuf::default();
334-
path.push(prefix);
335-
path.extend(components);
336-
path
337-
}
338-
_ => path,
339-
}
340-
}
341-
342312
match components.peek() {
343313
Some(Component::Prefix(prefix)) => match prefix.kind() {
344314
Prefix::Disk(drive_letter) => {
345-
components.next();
315+
let disk = components.next().unwrap();
346316
if components.peek() == Some(&Component::RootDir) {
347-
components.next();
348-
base_path = get_drive_prefix_path(drive_letter);
317+
let root = components.next().unwrap();
318+
stack.push(disk);
319+
stack.push(root);
349320
} else {
350-
base_path = get_drive_relative_path(drive_letter)?;
321+
base_path = get_drive_relative(drive_letter)?;
322+
stack.extend(base_path.components());
351323
}
352-
353-
stack.extend(base_path.components());
354324
}
355325
Prefix::VerbatimDisk(drive_letter) => {
356326
components.next();
357327
if components.peek() == Some(&Component::RootDir) {
358328
components.next();
359-
base_path = get_drive_prefix_path(drive_letter);
360-
} else {
361-
// Verbatim prefix without a root component? Likely not a legal path
362-
bail!("illegal path: {}", path.display());
363329
}
364330

331+
base_path = get_drive_path(drive_letter);
365332
stack.extend(base_path.components());
366333
}
367334
_ => bail!("invalid path: {}", path.display()),
@@ -373,7 +340,7 @@ pub fn resolve_path(path: impl AsRef<Path>) -> Result<PathBuf> {
373340
let drive_letter = get_drive_letter(&current_dir).with_context(|| {
374341
format!("could not get drive letter: {}", current_dir.display())
375342
})?;
376-
base_path = get_drive_prefix_path(drive_letter);
343+
base_path = get_drive_path(drive_letter);
377344
stack.extend(base_path.components());
378345
}
379346
_ => {

templates/cmd.txt

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@ rem Code generated by zoxide. DO NOT EDIT.
55

66
@if "%=^%=" == "%=%=" (
77
set \p=%%<nul
8-
) else setlocal DisableDelayedExpansion EnableExtensions & (
8+
) else (
99
set \p=^%<nul
1010
)
1111

12+
set __program=zoxide
13+
1214
set __builtin_cd=chdir /d
1315
set __builtin_pwd=(chdir)
1416

1517
{{ section }}
1618
rem Hook configuration for zoxide.
1719
rem
1820

19-
{% if hook == InitHook::None -%}
20-
{{ not_configured }}
21+
{% if hook != InitHook::None -%}
22+
>&2 echo %__program%: hooks are not supported on cmd.exe shell.
2123

2224
{%- else -%}
23-
>&2 echo zoxide: hooks are not supported on cmd shell.
25+
{{ not_configured }}
2426

2527
{%- endif %}
2628

@@ -29,10 +31,10 @@ rem Utility functions for zoxide.
2931
rem
3032

3133
rem pwd based on the value of _ZO_RESOLVE_SYMLINKS.
32-
set __zoxide_pwd={%- if resolve_symlinks -%} (for /f "skip=9 tokens=1,2,*" %\p%j in ('"%SystemRoot%\system32\fsutil.exe" reparsepoint query .') do @if "%\p%~j" == "Print" if "%\p%~k" == "Name:" if not "%\p%~l" == "" (echo(%%~l)) ^^^^^^^|^^^^^^^| {%~ endif -%} %__builtin_pwd%
34+
set __zoxide_pwd= {%- if resolve_symlinks -%} (for /f "skip=9 tokens=1,2,*" %\p%j in ('^^^^^^^^""%SystemRoot%\system32\fsutil.exe" reparsepoint query .^"') do @if "%\p%~j" == "Print" if "%\p%~k" == "Name:" if not "%\p%~l" == "" (echo(%%~l)) ^^^^^^^|^^^^^^^| {%~ endif -%} %__builtin_pwd%
3335

3436
rem cd + custom logic based on the value of _ZO_ECHO.
35-
set __zoxide_cd=if /i "%\p%CD%\p%" neq "%\p%~fc" (%__builtin_cd% "%\p%~fc" ^^^&^^^& set "OLDPWD=%\p%CD%\p%" ^^^&^^^& (for /f "delims=" %\p%l in ('"%__builtin_pwd%"') do @if /i "%\p%~dpl" neq "%\p%~fl" (zoxide add -- "%\p%~fl") else (zoxide add -- "%\p%~fl\")) {%- if echo ~%} ^^^&^^^& %__zoxide_pwd% {%- endif ~%} ^^^&^^^& if defined CDCMD (call %\p%CDCMD%\p%))
37+
set __zoxide_cd=if /i "%\p%CD%\p%" neq "%\p%~fc" (%__builtin_cd% "%\p%~fc" ^^^&^^^& set "OLDPWD=%\p%CD%\p%" ^^^&^^^& (for /f "delims=" %\p%l in ('"%__builtin_pwd%"') do @if /i "%\p%~fl" neq "%\p%~dpl" (%__program% add -- "%\p%~fl") else (%__program% add -- "%\p%~dpl\")) {%- if echo ~%} ^^^&^^^& %__zoxide_pwd% {%- endif ~%} ^^^&^^^& if defined CDCMD (call %\p%CDCMD%\p%))
3638

3739
{{ section }}
3840
rem Commands for zoxide. Disable these using --no-cmd.
@@ -44,10 +46,10 @@ rem
4446
set __zoxide_command={{ cmd }}
4547

4648
rem Jump to a directory using only keywords.
47-
"%SystemRoot%\system32\doskey.exe" %__zoxide_command% = (for %\p%^^^^ in ("") do @for /f "delims=" %\p%i in (^^""$*%\p%~^^"^") do @if "%\p%~i" == "" (if defined HOME (for /f "delims=" %\p%c in (^^""%\p%HOME%\p%"^") do @%__zoxide_cd%) else ^>^&2 (echo(zoxide: HOME not set) ^& call) else if "%\p%~i" == "~" (if defined HOME (for /f "delims=" %\p%c in (^^""%\p%HOME%\p%"^") do @%__zoxide_cd%) else ^>^&2 (echo(zoxide: HOME not set) ^& call) else if "%\p%~i" == "-" (if defined OLDPWD (for /f "delims=" %\p%c in (^^""%\p%OLDPWD%\p%"^") do @%__zoxide_cd%) else ^>^&2 (echo(zoxide: OLDPWD not set) ^& call) else for /f "delims=" %\p%~ in (^^"%\p%~i^") do @for /f "tokens=1,* delims=d" %\p%a in ("-%\p%~a~") do @if not "%\p%b" == "" (for /f "delims=" %\p%c in (^^""%\p%~f~"^") do @%__zoxide_cd%) else if /i "%\p%CD%\p%" neq "%\p%__CD__%\p%" (for /f "delims=" %\p%q in ('zoxide query --exclude "%\p%CD%\p%" -- %\p%~i') do @(for /f "delims=" %\p%c in (^^""%\p%~fq"^") do @%__zoxide_cd%)) else (for /f "delims=" %\p%q in ('zoxide query --exclude "%\p%__CD__%\p%\" -- %\p%~i') do @(for /f "delims=" %\p%c in (^^""%\p%~fq"^") do @%__zoxide_cd%))) ^&^& (call )
49+
"%SystemRoot%\system32\doskey.exe" %__zoxide_command% = @(for %\p%^^^^ in ("") do @for /f "delims=" %\p%i in (^^""$*%\p%~^^"^") do @if "%\p%~i" == "" (if "%\p%HOMEDRIVE%\p%%\p%HOMEPATH%\p%" == "" (for /f "delims=" %\p%c in (^^""%\p%USERPROFILE%\p%"^") do @%__zoxide_cd%) else for /f "delims=" %\p%c in (^^""%\p%HOMEDRIVE%\p%%\p%HOMEPATH%\p%"^") do @%__zoxide_cd%) else if "%\p%~i" == "~" (if "%\p%HOMEDRIVE%\p%%\p%HOMEPATH%\p%" == "" (for /f "delims=" %\p%c in (^^""%\p%USERPROFILE%\p%"^") do @%__zoxide_cd%) else for /f "delims=" %\p%c in (^^""%\p%HOMEDRIVE%\p%%\p%HOMEPATH%\p%"^") do @%__zoxide_cd%) else if "%\p%~i" == "-" (if defined OLDPWD (for /f "delims=" %\p%c in (^^""%\p%OLDPWD%\p%"^") do @%__zoxide_cd%) else ^>^&2 (echo(%__program%: OLDPWD not set) ^& (call)) else for /f "delims=" %\p%~ in (^^"%\p%~i^") do @for /f "tokens=1,* delims=d" %\p%a in ("-%\p%~a~") do @if not "%\p%b" == "" (for /f "delims=" %\p%c in (^^""%\p%~f~"^") do @%__zoxide_cd%) else if /i "%\p%CD%\p%" neq "%\p%__CD__%\p%" (for /f "delims=" %\p%q in ('^^"%__program% query --exclude "%\p%CD%\p%" -- %\p%~i^"') do @(for /f "delims=" %\p%c in (^^""%\p%~fq"^") do @%__zoxide_cd%)) else (for /f "delims=" %\p%q in ('^^"%__program% query --exclude "%\p%__CD__%\p%\" -- %\p%~i^"') do @(for /f "delims=" %\p%c in (^^""%\p%~fq"^") do @%__zoxide_cd%))) ^&^& call ;
4850

4951
rem Jump to a directory using interactive search.
50-
"%SystemRoot%\system32\doskey.exe" %__zoxide_command%i = (for %\p%^^^^ in ("") do @for /f "delims=" %\p%i in (^^""$*%\p%~^^"^") do @for /f "delims=" %\p%p in ('zoxide query --interactive -- %\p%~i') do @for /f "delims=" %\p%c in (^^""%\p%~fp"^") do @%__zoxide_cd%) ^&^& (call )
52+
"%SystemRoot%\system32\doskey.exe" %__zoxide_command%i = @(for %\p%^^^^ in ("") do @for /f "delims=" %\p%i in (^^""$*%\p%~^^"^") do @for /f "delims=" %\p%p in ('^^"%__program% query --interactive -- %\p%~i^"') do @for /f "delims=" %\p%c in (^^""%\p%~fp"^") do @%__zoxide_cd%) ^&^& call ;
5153

5254
{%- when None %}
5355

0 commit comments

Comments
 (0)