From f0e9751f7e349b69e58415504c60ed407421ca37 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Tue, 23 May 2023 08:47:11 +0800 Subject: [PATCH] chore: improve powershell completion script (#1168) --- completion/ps/task.ps1 | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/completion/ps/task.ps1 b/completion/ps/task.ps1 index eba7631c..2a389e8a 100644 --- a/completion/ps/task.ps1 +++ b/completion/ps/task.ps1 @@ -1,8 +1,28 @@ -$scriptBlock = { - param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters ) - $reg = "\* ($commandName.+?):" - $listOutput = $(task --list-all) - $listOutput | Select-String $reg -AllMatches | ForEach-Object { $_.Matches.Groups[1].Value } -} +using namespace System.Management.Automation -Register-ArgumentCompleter -CommandName task -ScriptBlock $scriptBlock +Register-ArgumentCompleter -CommandName task -ScriptBlock { + param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) + + if ($commandName.StartsWith('-')) { + $completions = @( + [CompletionResult]::new('--list-all ', '--list-all ', [CompletionResultType]::ParameterName, 'list all tasks'), + [CompletionResult]::new('--color ', '--color', [CompletionResultType]::ParameterName, '--color'), + [CompletionResult]::new('--concurrency=', '--concurrency=', [CompletionResultType]::ParameterName, 'concurrency'), + [CompletionResult]::new('--interval=', '--interval=', [CompletionResultType]::ParameterName, 'interval'), + [CompletionResult]::new('--output=interleaved ', '--output=interleaved', [CompletionResultType]::ParameterName, '--output='), + [CompletionResult]::new('--output=group ', '--output=group', [CompletionResultType]::ParameterName, '--output='), + [CompletionResult]::new('--output=prefixed ', '--output=prefixed', [CompletionResultType]::ParameterName, '--output='), + [CompletionResult]::new('--dry ', '--dry', [CompletionResultType]::ParameterName, '--dry'), + [CompletionResult]::new('--force ', '--force', [CompletionResultType]::ParameterName, '--force'), + [CompletionResult]::new('--parallel ', '--parallel', [CompletionResultType]::ParameterName, '--parallel'), + [CompletionResult]::new('--silent ', '--silent', [CompletionResultType]::ParameterName, '--silent'), + [CompletionResult]::new('--status ', '--status', [CompletionResultType]::ParameterName, '--status'), + [CompletionResult]::new('--verbose ', '--verbose', [CompletionResultType]::ParameterName, '--verbose'), + [CompletionResult]::new('--watch ', '--watch', [CompletionResultType]::ParameterName, '--watch') + ) + + return $completions.Where{ $_.CompletionText.StartsWith($commandName) } + } + + return $(task --list-all --silent) | Where-Object { $_.StartsWith($commandName) } | ForEach-Object { return $_ + " " } +}