o2sh / onefetch

Command-line Git information tool
https://onefetch.dev
MIT License
10.35k stars 277 forks source link

PowerShell snippet to run onefetch on directory change can break tools that wrap `Set-Location` (`cd`) #1467

Open spenserblack opened 1 day ago

spenserblack commented 1 day ago

zoixde yazi can not use ,Unable to jump

Originally posted by @Wu-Felix in https://github.com/o2sh/onefetch/issues/1452#issuecomment-2470462841

spenserblack commented 1 day ago

I think I know what the problem is for Yazi (and likely zoxide, too). Yazi has this recommended wrapper for PowerShell to CD into the last directory used:

function y {
    $tmp = [System.IO.Path]::GetTempFileName()
    yazi $args --cwd-file="$tmp"
    $cwd = Get-Content -Path $tmp
    if (-not [String]::IsNullOrEmpty($cwd) -and $cwd -ne $PWD.Path) {
        Set-Location -LiteralPath $cwd
    }
    Remove-Item -Path $tmp
}

I don't know much PowerShell, but I'm guessing that @Wu-Felix's issue is caused because the custom Set-Location takes -Path, but the Yazi wrapper uses -LiteralPath. So the y wrapper fails to CD ("jump").

Perhaps this can be easily fixed by accepting and passing any parameter from the custom Set-Location to the builtin Set-Location for better compatibility? Here's a simple example of what I mean:

function Set-Location-Proxy {
    # do something
    Set-Location @args
}

cc @kiapanahi

I'd happily accept a PR from either of you 🙂