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

feat: Invoke-FuzzyEdit - UNC Path Support #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions PSFzf.Base.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ $script:AltCCommand = [ScriptBlock]{
function Get-FileSystemCmd {
param($dir, [switch]$dirOnly = $false)

$dirPPath = (Resolve-Path $dir).ProviderPath
$IsUnc = ([uri]$dirPPath).IsUnc
# For UNC paths, cmd requires a pushd to create the virtual directory.
# Initial Push-Location in case $pwd is a UNC path to avoid warning message.
# Second "pushd" is inside the cmd.exe shell.
$poshPrefixUnc = 'try { Push-Location $HOME;'
$poshPostfixUnc = '} catch { } finally { Pop-Location }'
$DefaultFileSystemCmdUncPrefix = 'pushd "{0}" `&`&'

# Note that there is no way to know how to list only directories using
# FZF_DEFAULT_COMMAND, so we never use it in that case.
if ($dirOnly -or [string]::IsNullOrWhiteSpace($env:FZF_DEFAULT_COMMAND)) {
Expand All @@ -53,10 +62,16 @@ function Get-FileSystemCmd {
if ($dirOnly) {
$cmd = $script:DefaultFileSystemCmdDirOnly
}
$script:ShellCmd -f ($cmd -f $dir)
$cmdUnc = "$DefaultFileSystemCmdUncPrefix $cmd"
if ($IsUnc) { "$poshPrefixUnc $($script:ShellCmd -f ($cmdUnc -f $dirPPath)) $poshPostfixUnc" }
else { $script:ShellCmd -f ($cmd -f $dir) }
}
} else {
$script:ShellCmd -f ($env:FZF_DEFAULT_COMMAND -f $dir)
if ($IsUnc) {
$fzfDefUncCmd = "$DefaultFileSystemCmdUncPrefix $env:FZF_DEFAULT_COMMAND"
"$poshPrefixUnc $($script:ShellCmd -f ( $fzfDefUncCmd -f $dirPPath)) $poshPostfixUnc"
}
else { $script:ShellCmd -f ($env:FZF_DEFAULT_COMMAND -f $dir) }
}
}

Expand Down
16 changes: 10 additions & 6 deletions PSFzf.Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,34 @@ function Get-EditorLaunch() {
if ($editor -eq 'code') {
if ($FileList -is [array] -and $FileList.length -gt 1) {
for ($i = 0; $i -lt $FileList.Count; $i++) {
$FileList[$i] = '"{0}"' -f $(Resolve-Path $FileList[$i].Trim('"'))
$FileList[$i] = '"{0}"' -f $((Resolve-Path $FileList[$i].Trim('"')).ProviderPath)
}
"$editor$editorOptions {0}" -f ($FileList -join ' ')
}
else {
"$editor$editorOptions --goto ""{0}:{1}""" -f $(Resolve-Path $FileList.Trim('"')), $LineNum
"$editor$editorOptions --goto ""{0}:{1}""" -f $((Resolve-Path $FileList.Trim('"')).ProviderPath), $LineNum
}
}
elseif ($editor -match '[gn]?vi[m]?') {
if ($FileList -is [array] -and $FileList.length -gt 1) {
for ($i = 0; $i -lt $FileList.Count; $i++) {
$FileList[$i] = '"{0}"' -f $(Resolve-Path $FileList[$i].Trim('"'))
$FileList[$i] = '"{0}"' -f $((Resolve-Path $FileList[$i].Trim('"')).ProviderPath)
}
"$editor$editorOptions {0}" -f ($FileList -join ' ')
}
else {
"$editor$editorOptions ""{0}"" +{1}" -f $(Resolve-Path $FileList.Trim('"')), $LineNum
"$editor$editorOptions ""{0}"" +{1}" -f $((Resolve-Path $FileList.Trim('"')).ProviderPath), $LineNum
}
}
elseif ($editor -eq 'nano') {
if ($FileList -is [array] -and $FileList.length -gt 1) {
for ($i = 0; $i -lt $FileList.Count; $i++) {
$FileList[$i] = '"{0}"' -f $(Resolve-Path $FileList[$i].Trim('"'))
$FileList[$i] = '"{0}"' -f $((Resolve-Path $FileList[$i].Trim('"')).ProviderPath)
}
"$editor$editorOptions {0}" -f ($FileList -join ' ')
}
else {
"$editor$editorOptions +{1} {0}" -f $(Resolve-Path $FileList.Trim('"')), $LineNum
"$editor$editorOptions +{1} {0}" -f $((Resolve-Path $FileList.Trim('"')).ProviderPath), $LineNum
}
}
}
Expand Down Expand Up @@ -124,6 +124,10 @@ function Invoke-FuzzyEdit() {
$cmd = Get-EditorLaunch -FileList $files
Write-Host "Executing '$cmd'..."
($Editor, $Arguments) = $cmd.Split(' ')
# Avoids code.cmd "error" message by not calling cmd.exe from a UNC $PWD
if( ([uri]$PWD.ProviderPath).IsUnc ) {
cd $HOME
}
Start-Process $Editor -ArgumentList $Arguments -Wait:$Wait -NoNewWindow
}
catch {
Expand Down