36 lines
813 B
PowerShell
36 lines
813 B
PowerShell
[CmdletBinding(SupportsShouldProcess)]param(
|
|
[switch]$sudo,
|
|
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
|
[string[]]$Path
|
|
)
|
|
|
|
$local:editors = $env:EDITOR,'nvim','code'
|
|
$local:editor = $null
|
|
foreach( $local:testEditor in $editors ) {
|
|
if( Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue ) {
|
|
$editor = $testEditor
|
|
break;
|
|
}
|
|
}
|
|
|
|
if( $editor -match 'vim?$' ) {
|
|
$editor += ' -p'
|
|
}
|
|
|
|
if( $sudo ) {
|
|
$editor = "/usr/bin/env sudo $editor"
|
|
}
|
|
|
|
if( $editor -match 'code(\.exe)?$' ) {
|
|
if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) {
|
|
& $editor
|
|
}
|
|
}
|
|
|
|
$local:arguments = $Path -join "' '"
|
|
if( $Path ) { $arguments = "'$arguments'" }
|
|
|
|
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
|
|
Invoke-Expression "$editor $arguments"
|
|
}
|