2020-09-29 20:50:19 +00:00
|
|
|
[CmdletBinding(SupportsShouldProcess)]param(
|
2022-01-29 22:48:01 +00:00
|
|
|
[switch]$sudo,
|
|
|
|
[Parameter(Position = 0, ValueFromPipelineByPropertyName, ValueFromPipeline, ValueFromRemainingArguments = $true)]
|
|
|
|
[string[]]$Path,
|
|
|
|
[string[]]$PreferredEditors
|
2020-09-29 20:50:19 +00:00
|
|
|
)
|
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
begin {
|
2022-01-29 22:48:01 +00:00
|
|
|
$global:WaitForEditor = (($WaitForEditor, -1 -ne $null)[0], 5 -ne -1)[0]
|
|
|
|
$PathForEditor = @()
|
2020-09-29 20:50:19 +00:00
|
|
|
}
|
2021-04-07 20:04:46 +00:00
|
|
|
process {
|
2022-01-29 22:48:01 +00:00
|
|
|
foreach ( $local:P in $Path ) {
|
|
|
|
$PathForEditor += $P | Get-Path -Expand
|
|
|
|
}
|
2020-09-30 04:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
end {
|
2022-01-29 22:48:01 +00:00
|
|
|
$local:editors = $PreferredEditors + @(
|
|
|
|
$env:EDITOR, 'nvim', 'vim', 'vi', 'codium', 'code', 'notepad'
|
|
|
|
) | Where-Object { $_ }
|
|
|
|
$local:editor = $null
|
|
|
|
foreach ( $local:testEditor in $editors ) {
|
|
|
|
if ( $(try { Get-Command -ListImported -Type Application $testEditor -ErrorAction SilentlyContinue }catch {}) ) {
|
|
|
|
$editor = "`"$testEditor`""
|
|
|
|
break;
|
|
|
|
}
|
2021-04-07 20:04:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 22:48:01 +00:00
|
|
|
if ( $editor -match 'vim"$' ) {
|
|
|
|
$editor += ' -p'
|
|
|
|
}
|
2021-04-07 20:04:46 +00:00
|
|
|
|
2022-01-29 22:48:01 +00:00
|
|
|
if ( $EditorProcessName ) {
|
|
|
|
if ( -not (Get-Process -Name $EditorProcessName -ErrorAction SilentlyContinue) ) {
|
|
|
|
Invoke-ExpressionEx -sudo:$sudo "& $editor"
|
|
|
|
while ( -not (Get-Process -Name $EditorProcessName -ErrorAction SilentlyContinue) ) {
|
|
|
|
Start-Sleep -Seconds 1
|
|
|
|
}
|
|
|
|
Write-Host -ForegroundColor DarkCyan "Please wait, initializing editor..."
|
|
|
|
Start-Sleep -Seconds $WaitForEditor
|
|
|
|
if ( Test-Path variable:EditorWaitStopWatch ) {
|
|
|
|
$EditorWaitStopWatch.Restart()
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 20:04:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 22:48:01 +00:00
|
|
|
Write-Verbose $($Path | Out-String)
|
2021-04-07 20:04:46 +00:00
|
|
|
|
2022-01-29 22:48:01 +00:00
|
|
|
$local:arguments = $PathForEditor | Foreach-Object { "'$_'" }
|
|
|
|
if ( $Path ) { $arguments = "$arguments" }
|
2021-04-07 20:04:46 +00:00
|
|
|
|
2022-01-29 22:48:01 +00:00
|
|
|
if ( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
|
|
|
|
Invoke-ExpressionEx -sudo:$sudo "& $editor $arguments"
|
|
|
|
}
|
2021-06-30 03:16:28 +00:00
|
|
|
}
|