diff --git a/module/PowerShellRun/Private/FunctionRegistry.ps1 b/module/PowerShellRun/Private/FunctionRegistry.ps1 index 64d71d2..c7d255a 100644 --- a/module/PowerShellRun/Private/FunctionRegistry.ps1 +++ b/module/PowerShellRun/Private/FunctionRegistry.ps1 @@ -31,10 +31,14 @@ class FunctionRegistry : EntryRegistry { $this.callback = { $result = $args[0].Result - $functionName = $args[0].ArgumentList + $functionName, $argumentList = $args[0].ArgumentList if ($result.KeyCombination -eq $script:globalStore.firstActionKey) { - & $functionName + if ($null -eq $argumentList) { + & $functionName + } else { + & $functionName @argumentList + } } elseif ($result.KeyCombination -eq $script:globalStore.secondActionKey) { $function = Get-Command $functionName $function.Definition @@ -46,7 +50,7 @@ class FunctionRegistry : EntryRegistry { $function.ScriptBlock.Ast.Body.ParamBlock.Parameters } - $parameters = $script:globalStore.GetParameterList($astParameters) + $parameters = $script:globalStore.GetParameterList($astParameters, $argumentList) if ($null -ne $parameters) { & $functionName @parameters } @@ -87,7 +91,7 @@ class FunctionRegistry : EntryRegistry { if ($functionAtStart -eq $function.ScriptBlock) { continue } - $entry = $this.CreateFunctionEntry($function) + $entry = $this.CreateFunctionEntry($function, $null) $this.entries.Add($entry) $this.isEntryUpdated = $true } @@ -95,7 +99,7 @@ class FunctionRegistry : EntryRegistry { $this.functionsAtRegisterStart = $null } - [void] AddFunction($functionName, $icon, $name, $description, $preview, [EntryGroup]$entryGroup) { + [void] AddFunction($functionName, $argumentList, $icon, $name, $description, $preview, [EntryGroup]$entryGroup) { if (-not $this.isEnabled) { Write-Warning -Message '"Function" category is disabled.' return @@ -107,7 +111,7 @@ class FunctionRegistry : EntryRegistry { return } - $entry = $this.CreateFunctionEntry($function) + $entry = $this.CreateFunctionEntry($function, $argumentList) if ($icon) { $entry.Icon = $icon } if ($name) { $entry.Name = $name } if ($description) { $entry.Description = $description } @@ -121,7 +125,7 @@ class FunctionRegistry : EntryRegistry { } } - [PowerShellRun.SelectorEntry] CreateFunctionEntry($function) { + [PowerShellRun.SelectorEntry] CreateFunctionEntry($function, $argumentList) { $help = Get-Help $function.Name $customAttributes = $this.GetFunctionCustomAttributes($help) @@ -142,7 +146,7 @@ class FunctionRegistry : EntryRegistry { $entry.UserData = @{ ScriptBlock = $this.callback - ArgumentList = $function.Name + ArgumentList = $function.Name, $argumentList } return $entry } diff --git a/module/PowerShellRun/Private/GlobalStore.ps1 b/module/PowerShellRun/Private/GlobalStore.ps1 index 630a1f5..19bf95a 100644 --- a/module/PowerShellRun/Private/GlobalStore.ps1 +++ b/module/PowerShellRun/Private/GlobalStore.ps1 @@ -330,11 +330,23 @@ class GlobalStore { } } - [Object] GetParameterList($astParameters) { + [Object] GetParameterList($astParameters, $argumentList) { if (-not $astParameters) { return @{} } + $initialPromptContexts = @() + for ($i = 0; $i -lt $astParameters.Count; ++$i) { + $astParameter = $astParameters[$i] + $promptContext = [PowerShellRun.PromptContext]::new() + if (($null -ne $argumentList) -and ($null -ne $argumentList[$i].ToString)) { + $promptContext.Input = $argumentList[$i].ToString() + } elseif ($null -ne $astParameter.DefaultValue.Value.ToString) { + $promptContext.Input = $astParameter.DefaultValue.Value.ToString() + } + $initialPromptContexts += $promptContext + } + $option = $this.GetPSRunSelectorOption() $option.QuitWithBackspaceOnEmptyQuery = $true @@ -344,6 +356,9 @@ class GlobalStore { $parameterName = $astParameters[$i].Name.VariablePath.UserPath.Replace('$', '') $option.Prompt = $parameterName $promptContext = $promptContexts[$parameterName] + if ($null -eq $promptContext) { + $promptContext = $initialPromptContexts[$i] + } $promptResult = Invoke-PSRunPrompt -Option $option -Context $promptContext if ([PowerShellRun.ExitStatus]::Type -eq [PowerShellRun.ExitType]::QuitWithBackspaceOnEmptyQuery) { diff --git a/module/PowerShellRun/Private/ScriptRegistry.ps1 b/module/PowerShellRun/Private/ScriptRegistry.ps1 index 76b0147..5c130e2 100644 --- a/module/PowerShellRun/Private/ScriptRegistry.ps1 +++ b/module/PowerShellRun/Private/ScriptRegistry.ps1 @@ -37,12 +37,16 @@ class ScriptRegistry : EntryRegistry { $scriptBlock, $argumentList = $args[0].ArgumentList if ($result.KeyCombination -eq $script:globalStore.firstActionKey) { - & $scriptBlock @argumentList + if ($null -eq $argumentList) { + & $scriptBlock + } else { + & $scriptBlock @argumentList + } } elseif ($result.KeyCombination -eq $script:globalStore.secondActionKey) { $scriptBlock.ToString() } elseif ($result.KeyCombination -eq $script:globalStore.thirdActionKey) { $astParameters = $scriptBlock.Ast.ParamBlock.Parameters - $parameters = $script:globalStore.GetParameterList($astParameters) + $parameters = $script:globalStore.GetParameterList($astParameters, $argumentList) if ($null -ne $parameters) { & $scriptBlock @parameters } @@ -63,7 +67,11 @@ class ScriptRegistry : EntryRegistry { $filePath, $argumentList = $args[0].ArgumentList if ($result.KeyCombination -eq $script:globalStore.firstActionKey) { - & $filePath @argumentList + if ($null -eq $argumentList) { + & $filePath + } else { + & $filePath @argumentList + } } elseif ($result.KeyCombination -eq $script:globalStore.secondActionKey) { & $script:globalStore.defaultEditorScript $filePath } elseif ($result.KeyCombination -eq $script:globalStore.thirdActionKey) { diff --git a/module/PowerShellRun/Public/Add-PSRunFunction.ps1 b/module/PowerShellRun/Public/Add-PSRunFunction.ps1 index cd2e9ed..ff5fd9e 100644 --- a/module/PowerShellRun/Public/Add-PSRunFunction.ps1 +++ b/module/PowerShellRun/Public/Add-PSRunFunction.ps1 @@ -9,6 +9,9 @@ The function must be global and defined before calling this function. .PARAMETER FunctionName The function name you would like to add as an entry. +.PARAMETER ArgumentList +The arguments that are passed to the function. + .PARAMETER Icon The icon string. @@ -44,6 +47,9 @@ function Add-PSRunFunction { [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [String]$FunctionName, + [Parameter(ValueFromPipelineByPropertyName = $true)] + [Object[]]$ArgumentList, + [Parameter(ValueFromPipelineByPropertyName = $true)] [String]$Icon, @@ -62,6 +68,6 @@ function Add-PSRunFunction { process { $registry = $script:globalStore.GetRegistry('FunctionRegistry') - $registry.AddFunction($FunctionName, $Icon, $Name, $Description, $Preview, $EntryGroup) + $registry.AddFunction($FunctionName, $ArgumentList, $Icon, $Name, $Description, $Preview, $EntryGroup) } }