Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spenserblack opened this issue Nov 12, 2024 · 4 comments

Comments

@spenserblack
Copy link
Collaborator

spenserblack commented Nov 12, 2024

zoixde yazi can not use ,Unable to jump

Originally posted by @Wu-Felix in #1452 (comment)

@spenserblack
Copy link
Collaborator Author

spenserblack commented Nov 12, 2024

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 🙂

@spenserblack spenserblack changed the title PowerShell snippet can break tools that wrap Set-Location (cd) PowerShell snippet to run onefetch on directory change can break tools that wrap Set-Location (cd) Nov 12, 2024
@FallenDeity
Copy link
Contributor

@spenserblack I was working with zoxide encountered this exact issue this is a script I modified from the original one in the docs

$global:lastRepository = $null

function Check-DirectoryForNewRepository {
    $currentRepository = git rev-parse --show-toplevel 2>$null
    if ($currentRepository -and ($currentRepository -ne $global:lastRepository)) {
        [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
        onefetch | Write-Host
    }
    $global:lastRepository = $currentRepository
}

function Set-Location {
    param (
        [string]$Path,
        [string]$LiteralPath
    )

    if ($LiteralPath) {
        Microsoft.PowerShell.Management\Set-Location -LiteralPath $LiteralPath
    }
    else {
        Microsoft.PowerShell.Management\Set-Location -Path $Path
    }

    Check-DirectoryForNewRepository
}

Check-DirectoryForNewRepository

Added an extra parameter to handle the LiteralPath, also when using zoxide while onefetch was triggering there was no console output unless I explicitly pipe it

Output

Image

@spenserblack
Copy link
Collaborator Author

@FallenDeity Looks good! I have some feedback about the code block, but if you'd like to make a PR I can save my feedback for that.

@spenserblack
Copy link
Collaborator Author

Closed by #1542

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants