2020-09-29 20:50:19 +00:00
|
|
|
[CmdletBinding(SupportsShouldProcess)]param(
|
|
|
|
[switch]$sudo,
|
|
|
|
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
|
|
|
|
[string[]]$Path
|
|
|
|
)
|
|
|
|
|
2020-09-30 04:14:51 +00:00
|
|
|
$local:editors = $env:EDITOR,'nvim','code'
|
|
|
|
$local:editor = $null
|
|
|
|
foreach( $local:testEditor in $editors ) {
|
2020-09-30 20:43:07 +00:00
|
|
|
if( $(try{Get-Command -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) {
|
2020-09-30 04:14:51 +00:00
|
|
|
$editor = $testEditor
|
|
|
|
break;
|
|
|
|
}
|
2020-09-29 20:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( $editor -match 'vim?$' ) {
|
|
|
|
$editor += ' -p'
|
|
|
|
}
|
|
|
|
|
2020-09-30 04:14:51 +00:00
|
|
|
if( $editor -match 'code(\.exe)?$' ) {
|
|
|
|
if( -not (Get-Process -Name 'code' -ErrorAction SilentlyContinue) ) {
|
2020-09-30 20:43:07 +00:00
|
|
|
Invoke-ExpressionEx -sudo:$sudo $editor
|
2020-09-30 04:14:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 02:31:31 +00:00
|
|
|
$local:arguments = $($Path | ForEach-Object {
|
|
|
|
if ($Path -match '\*|\?|~') {
|
|
|
|
Get-ChildItem -Path $_ | Select-Object -ExpandProperty FullName
|
|
|
|
} else { $_ }
|
|
|
|
} ) -join "' '"
|
2020-09-29 20:50:19 +00:00
|
|
|
if( $Path ) { $arguments = "'$arguments'" }
|
|
|
|
|
|
|
|
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
|
2020-09-30 20:43:07 +00:00
|
|
|
Invoke-ExpressionEx -sudo:$sudo $editor "$arguments"
|
2020-09-29 20:50:19 +00:00
|
|
|
}
|