Created
April 22, 2022 08:16
-
-
Save ShrykeWindgrace/c7004b069d6abf97c7f07c1e0dbd29e3 to your computer and use it in GitHub Desktop.
Powershell tab-completion script for stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Management.Automation | |
using namespace System.Management.Automation.Language | |
Register-ArgumentCompleter -Native -CommandName 'stack' -ScriptBlock { | |
param($wordToComplete, $commandAst) | |
[string[]]$localCommand = @('"--bash-completion-enriched"') | |
$hay = [System.Collections.Generic.List[string]]::new() | |
foreach ($item in $commandAst.CommandElements) { | |
$localCommand += '"--bash-completion-word"' | |
$localCommand += """$item""" | |
$hay.Add($item.ToString()) | |
} | |
$localCommand += '"--bash-completion-index"' | |
if ($wordToComplete.Equals("")) { | |
$localCommand += '"' + $commandAst.CommandElements.Count +'"' | |
} | |
else { | |
$localCommand += '"' + $hay.IndexOf($wordToComplete) + '"' | |
} | |
$inp = & '/path/to/stack' @localCommand | |
[CompletionResult[]]$out = @() | |
[string]$suffix = if ($inp.Count -eq 1) {' '} else {""} | |
foreach ($item in $inp) { | |
$spl = $item.Split("`t") | |
$show = $spl[0] | |
$tooltip = if ($spl.Length -eq 1) { $spl[0] } else { $spl[1] } | |
$crt = if ($show.StartsWith('-')) { [CompletionResultType]::ParameterName } else { [CompletionResultType]::ParameterValue } | |
$out += [CompletionResult]::new($show + $suffix, $show, $crt, $tooltip) | |
} | |
$out | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script works best in
pwsh >= 6
(akapowershell core
available on all major platforms), and with somewhat limited functionality inpowershell
(akawindows powershell
, windows-only, duh).Just replace the
/path/to/stack
with your own value, e.g. viaor
Another important note: this script will work for all haskell executables based on
optparse-applicative
! Simply replace the'stack'
with the name of your executable. On windows you might want to omit the extension for best behavior.Also, to get tooltips, you would want to set
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
in your powershell session or in your$PROFILE
.