Skip to content

Commit 77f2c90

Browse files
Add an option to generate powershell completion script
1 parent 8edc419 commit 77f2c90

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/Options/Applicative/BashCompletion.hs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ bashCompletionParser pinfo pprefs = complParser
6262
, failure <$>
6363
(zshCompletionScript <$>
6464
strOption (long "zsh-completion-script" `mappend` internal))
65+
, failure <$>
66+
(pwshCompletionScript <$>
67+
strOption (long "pwsh-completion-script" `mappend` internal))
6568
]
6669

6770
bashCompletionQuery :: ParserInfo a -> ParserPrefs -> Richness -> [String] -> Int -> String -> IO [String]
@@ -254,3 +257,38 @@ zshCompletionScript prog progn = return
254257
, " fi"
255258
, "done"
256259
]
260+
261+
pwshCompletionScript :: String -> String -> IO [String]
262+
pwshCompletionScript prog progn = return
263+
[ "using namespace System.Management.Automation"
264+
, "using namespace System.Management.Automation.Language"
265+
, "Register-ArgumentCompleter -Native -CommandName '" ++ progn ++ "' -ScriptBlock {"
266+
, " param($wordToComplete, $commandAst)"
267+
, " [string[]]$localCommand = @('\"--bash-completion-enriched\"')"
268+
, " $hay = [System.Collections.Generic.List[string]]::new()"
269+
, " foreach ($item in $commandAst.CommandElements) {"
270+
, " $localCommand += '\"--bash-completion-word\"'"
271+
, " $localCommand += \"\"\"$item\"\"\""
272+
, " $hay.Add($item.ToString())"
273+
, " }"
274+
, ""
275+
, ""
276+
, " $localCommand += '\"--bash-completion-index\"'"
277+
, " if ($wordToComplete.Equals(\"\")) {"
278+
, " $localCommand += '\"0\"'"
279+
, " }"
280+
, " else {"
281+
, " $localCommand += '\"' + $hay.IndexOf($wordToComplete) + '\"'"
282+
, " }"
283+
, " $inp = & '" ++ prog ++ "' @localCommand"
284+
, " [CompletionResult[]]$out = @()"
285+
, " foreach ($item in $inp) {"
286+
, " $spl = $item.Split(\"`t\")"
287+
, " $show = $spl[0]"
288+
, " $tooltip = if ($spl.Length -eq 1) { $spl[0] } else { $spl[1] }"
289+
, " $crt = if ($show.StartsWith('-')) { [CompletionResultType]::ParameterName } else { [CompletionResultType]::ParameterValue }"
290+
, " $out += [CompletionResult]::new($show, $show.Trim('-'), $crt, $tooltip)"
291+
, " }"
292+
, " $out"
293+
, "}"
294+
]

0 commit comments

Comments
 (0)