From 171edecf4710fed5d72f87ba57d0af7a7cbf335d Mon Sep 17 00:00:00 2001 From: mattcargile Date: Sat, 26 Nov 2022 14:37:52 -0600 Subject: [PATCH] feat: `Invoke-FuzzyEdit` - UNC Path Support Add logic if `pwd` is a UNC path. Starting a `cmd.exe` process from a UNC path displays an unecessary warning message so ensuring the `pwd` is set to `$HOME`. `Get-FileSystemCmd` - Add logic for `cmd.exe` UNC handling requiring pushd into the UNC path for searching. `Get-EditorLaunch` - Use `ProviderPath` property for Resolve-Path instead of default `ToString()` output. `(resolve-path '\\localhost\c$\users\').ToString()` returns `Microsoft.PowerShell.Core\FileSystem::\\localhost\c$\users\` which isn't supported by native terminal or visual text editors. --- PSFzf.Base.ps1 | 19 +++++++++++++++++-- PSFzf.Functions.ps1 | 16 ++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/PSFzf.Base.ps1 b/PSFzf.Base.ps1 index 39b2ec0..6f5eb6b 100644 --- a/PSFzf.Base.ps1 +++ b/PSFzf.Base.ps1 @@ -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)) { @@ -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) } } } diff --git a/PSFzf.Functions.ps1 b/PSFzf.Functions.ps1 index 3382b88..0f1a51d 100644 --- a/PSFzf.Functions.ps1 +++ b/PSFzf.Functions.ps1 @@ -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 } } } @@ -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 {