Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mataha committed Jun 10, 2024
1 parent de8fb59 commit 94ba939
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.(md,txt}]
[*.(md,rst,txt}]
indent_size = unset
trim_trailing_whitespace = false

Expand Down
2 changes: 1 addition & 1 deletion man/man1/zoxide-init.1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Add this to the \fBend\fR of your config file (usually \fB~/.bashrc\fR):
\fBeval "$(zoxide init bash)"\fR
.fi
.TP
.B cmd
.B cmd.exe
Add this to the \fBend\fR of your config file or AutoRun command:
.sp
.nf
Expand Down
4 changes: 2 additions & 2 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ mod tests {
.success();

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

if opts.hook != InitHook::None {
assert.stderr("zoxide: hooks are not supported on cmd shell.\r\n");
assert.stderr("zoxide: hooks are not supported on cmd.exe shell.\r\n");
} else {
assert.stderr("");
}
Expand Down
63 changes: 15 additions & 48 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,77 +291,44 @@ pub fn resolve_path(path: impl AsRef<Path>) -> Result<PathBuf> {
}
}

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

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

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

let path = get_drive_prefix_path(drive_letter);
let path = get_drive_path(drive_letter);
Ok(path)
}

#[inline(always)]
fn patch_drive_letter(drive_letter: u8) -> char {
drive_letter.to_ascii_uppercase() as char
}

// https://github.com/rust-lang/rust-analyzer/pull/14689
fn patch_drive_prefix(path: PathBuf) -> PathBuf {
let mut components = path.components();

match components.next() {
Some(Component::Prefix(prefix)) => {
let prefix = match prefix.kind() {
Prefix::Disk(drive_letter) => {
format!(r"{}:", patch_drive_letter(drive_letter))
}
Prefix::VerbatimDisk(drive_letter) => {
format!(r"\\?\{}:", patch_drive_letter(drive_letter))
}
_ => return path,
};

let mut path = PathBuf::default();
path.push(prefix);
path.extend(components);
path
}
_ => path,
}
}

match components.peek() {
Some(Component::Prefix(prefix)) => match prefix.kind() {
Prefix::Disk(drive_letter) => {
components.next();
let disk = components.next().unwrap();
if components.peek() == Some(&Component::RootDir) {
components.next();
base_path = get_drive_prefix_path(drive_letter);
let root = components.next().unwrap();
stack.push(disk);
stack.push(root);
} else {
base_path = get_drive_relative_path(drive_letter)?;
base_path = get_drive_relative(drive_letter)?;
stack.extend(base_path.components());
}

stack.extend(base_path.components());
}
Prefix::VerbatimDisk(drive_letter) => {
components.next();
if components.peek() == Some(&Component::RootDir) {
components.next();
base_path = get_drive_prefix_path(drive_letter);
} else {
// Verbatim prefix without a root component? Likely not a legal path
bail!("illegal path: {}", path.display());
}

base_path = get_drive_path(drive_letter);
stack.extend(base_path.components());
}
_ => bail!("invalid path: {}", path.display()),
Expand All @@ -373,7 +340,7 @@ pub fn resolve_path(path: impl AsRef<Path>) -> Result<PathBuf> {
let drive_letter = get_drive_letter(&current_dir).with_context(|| {
format!("could not get drive letter: {}", current_dir.display())
})?;
base_path = get_drive_prefix_path(drive_letter);
base_path = get_drive_path(drive_letter);
stack.extend(base_path.components());
}
_ => {
Expand Down
18 changes: 10 additions & 8 deletions templates/cmd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ rem Code generated by zoxide. DO NOT EDIT.

@if "%=^%=" == "%=%=" (
set \p=%%<nul
) else setlocal DisableDelayedExpansion EnableExtensions & (
) else (
set \p=^%<nul
)

set __program=zoxide

set __builtin_cd=chdir /d
set __builtin_pwd=(chdir)

{{ section }}
rem Hook configuration for zoxide.
rem

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

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

{%- endif %}

Expand All @@ -29,10 +31,10 @@ rem Utility functions for zoxide.
rem

rem pwd based on the value of _ZO_RESOLVE_SYMLINKS.
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%
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%

rem cd + custom logic based on the value of _ZO_ECHO.
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%))
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%))

{{ section }}
rem Commands for zoxide. Disable these using --no-cmd.
Expand All @@ -44,10 +46,10 @@ rem
set __zoxide_command={{ cmd }}

rem Jump to a directory using only keywords.
"%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 )
"%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 ;

rem Jump to a directory using interactive search.
"%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 )
"%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 ;

{%- when None %}

Expand Down

0 comments on commit 94ba939

Please sign in to comment.