functions/Set-CmdFavEditor.ps1
function Set-CmdFavEditor { <# .SYNOPSIS Configures the external editor for CmdFav. .DESCRIPTION Allows the user to select one of the supported editors (VSCode, Notepad++, Notepad, Nano) or manually specify the editor path, wait mode, and parameter. .PARAMETER Editor The editor to configure (code, notepad++, notepad, nano). .PARAMETER Path The path to the editor executable (manual mode). .PARAMETER WaitMode How CmdFav should wait for the editor (Process or ReadKey, manual mode). .PARAMETER Parameter The parameter string for the editor, use [PATH] as placeholder (manual mode). .EXAMPLE Set-CmdFavEditor -Editor code .EXAMPLE Set-CmdFavEditor -Path "C:\Tools\myeditor.exe" -WaitMode Process -Parameter "[PATH]" #> [CmdletBinding(DefaultParameterSetName='Predefined')] param ( [Parameter(Mandatory, ParameterSetName='Predefined')] [ValidateSet('code','notepad++','notepad','nano','clipboard')] [string]$Editor, [Parameter(Mandatory, ParameterSetName='Manual')] [string]$Path, [Parameter(Mandatory, ParameterSetName='Manual')] [ValidateSet('Process','ReadKey')] [string]$WaitMode, [Parameter(Mandatory, ParameterSetName='Manual')] [string]$Parameter ) if ($PSCmdlet.ParameterSetName -eq 'Predefined') { switch ($Editor) { 'code' { $path = (Get-Command code -ErrorAction Stop).Source $wait = 'ReadKey' $param = '[PATH]' } 'notepad++' { $possiblePaths = @( "$env:ProgramFiles\\Notepad++\\notepad++.exe", "$env:ProgramFiles(x86)\\Notepad++\\notepad++.exe", "$env:LOCALAPPDATA\\Programs\\Notepad++\\notepad++.exe" ) $path = $possiblePaths | Where-Object { Test-Path $_ } | Select-Object -First 1 if (-not $path) { Stop-PSFFunction -Level Warning -Message "Notepad++ executable not found in standard locations. Please install Notepad++ or use manual mode." return } $wait = 'ReadKey' $param = '[PATH]' } 'notepad' { $path = (Get-Command notepad -ErrorAction Stop).Source $wait = 'Process' $param = '[PATH]' } 'nano' { $path = (Get-Command nano -ErrorAction Stop).Source $wait = 'Process' $param = '[PATH]' } # 'clipboard' { # $path = 'clipboard' # $wait = 'ReadKey' # $param = 'n/a' # } } } else { $path = $Path $wait = $WaitMode $param = $Parameter } Set-PSFConfig -Module 'CmdFav' -Name 'ExternalEditor.Path' -Value $path -Validation string -Description "Path to the preferred external editor" -PassThru|Register-PSFConfig -Scope UserDefault Set-PSFConfig -Module 'CmdFav' -Name 'ExternalEditor.WaitMode' -Value $wait -Validation string -Description "How CmdFav should wait for the editor to finish (Process or ReadKey)" -PassThru|Register-PSFConfig -Scope UserDefault Set-PSFConfig -Module 'CmdFav' -Name 'ExternalEditor.Parameter' -Value $param -Validation string -Description "Parameter for the editor ([PATH] as placeholder for file)" -PassThru | Register-PSFConfig -Scope UserDefault Write-PSFMessage "External editor configuration updated: $path ($wait, $param)" } |