2020-09-29 20:50:19 +00:00
|
|
|
[CmdletBinding(SupportsShouldProcess)]param(
|
|
|
|
[switch]$sudo,
|
2021-04-23 17:41:59 +00:00
|
|
|
[Parameter(Position = 0, ValueFromPipelineByPropertyName, ValueFromPipeline, ValueFromRemainingArguments = $true)]
|
2020-09-29 20:50:19 +00:00
|
|
|
[string[]]$Path
|
|
|
|
)
|
|
|
|
|
2021-04-07 20:04:46 +00:00
|
|
|
begin {
|
2021-08-10 15:50:43 +00:00
|
|
|
$global:WaitForEditor = (($WaitForEditor, -1 -ne $null)[0], 5 -ne -1)[0]
|
2021-04-07 20:04:46 +00:00
|
|
|
$PathForEditor = @()
|
2020-09-29 20:50:19 +00:00
|
|
|
}
|
2021-04-07 20:04:46 +00:00
|
|
|
process {
|
|
|
|
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 {
|
2021-08-10 15:50:43 +00:00
|
|
|
$local:editors = $env:EDITOR,'nvim','codium','code'
|
2021-04-07 20:04:46 +00:00
|
|
|
$local:editor = $null
|
|
|
|
foreach( $local:testEditor in $editors ) {
|
2021-04-27 16:04:12 +00:00
|
|
|
if( $(try{Get-Command -ListImported -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) {
|
2021-06-30 03:16:28 +00:00
|
|
|
$editor = "`"$testEditor`""
|
2021-04-07 20:04:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 03:16:28 +00:00
|
|
|
if( $editor -match 'vim?"$' ) {
|
2021-04-07 20:04:46 +00:00
|
|
|
$editor += ' -p'
|
|
|
|
}
|
|
|
|
|
2021-08-10 15:50:43 +00:00
|
|
|
if( $EditorProcessName ) {
|
|
|
|
if( -not (Get-Process -Name $EditorProcessName -ErrorAction SilentlyContinue) ) {
|
2021-07-14 21:05:48 +00:00
|
|
|
Invoke-ExpressionEx -sudo:$sudo "& $editor"
|
2021-08-10 15:50:43 +00:00
|
|
|
while( -not (Get-Process -Name $EditorProcessName -ErrorAction SilentlyContinue) ) {
|
2021-07-14 21:05:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Verbose $($Path | Out-String)
|
|
|
|
|
|
|
|
$local:arguments = $PathForEditor | Foreach-Object { "'$_'" }
|
|
|
|
if( $Path ) { $arguments = "$arguments" }
|
|
|
|
|
|
|
|
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
|
2021-06-30 03:16:28 +00:00
|
|
|
Invoke-ExpressionEx -sudo:$sudo "& $editor $arguments"
|
2021-04-07 20:04:46 +00:00
|
|
|
}
|
2021-06-30 03:16:28 +00:00
|
|
|
}
|