- Optimizing: `__zoxide_z` function for better preformance.

- Modifying: `cd +` to look for latest made directory and switch to It, in case of no found next Dirs.
- Adding: `cd ++` a Forced vesion of `cd +` that only look for latest created Dir and Switch Location.
- Fixing: `cd +` history lookup order, and `cd ++` not matching.
This commit is contained in:
benzaria 2025-05-13 21:38:47 +01:00
parent e65184005a
commit e97a17e608
No known key found for this signature in database
GPG Key ID: 7CCBB3419E1E1475
1 changed files with 11 additions and 5 deletions

View File

@ -40,11 +40,14 @@ function global:__zoxide_cd($dir, $literal) {
try {
Set-Location -Path $dir -Passthru -ErrorAction Stop
} catch {
if ($dir -eq '+') {
# Get the last 5 commands from history
if ($dir -match '^\+{1,2}$') { # '^[-\+]{1,2}$'
# Get the last commands from history
if ($dir -eq '++') { $history = Get-History }
else { $history = Get-History -Count 5 }
# Check for 'mkdir' command and extract the directory created
foreach ($cmd in Get-History -Count 5) {
if ($cmd.CommandLine -match 'mkdir\s+([^\s]+)') {
for ($i = $history.Count - 1; $i -ge 0; $i--) {
if ($history[$i].CommandLine -match 'mkdir\s+([^\s]+)') {
if (Test-Path -Path $matches[1] -PathType Container) {
Set-Location -Path $matches[1]
}
@ -120,12 +123,15 @@ function global:__zoxide_z {
}
if ($args.Length -eq 1) {
$arg = $args[0]
if ($arg -eq '-' -or $arg -eq '+' -or (Test-Path -PathType Container -Path $arg)) {
if ($arg -match '^[-\+]{1,2}$') {
return __zoxide_cd $arg $false
}
if (Test-Path -PathType Container -LiteralPath $arg) {
return __zoxide_cd $arg $true
}
if (Test-Path -PathType Container -Path $arg) {
return __zoxide_cd $arg $false
}
}
$current = __zoxide_pwd