Skip to content

Instantly share code, notes, and snippets.

@ShrykeWindgrace
Created April 22, 2022 08:16
Show Gist options
  • Save ShrykeWindgrace/c7004b069d6abf97c7f07c1e0dbd29e3 to your computer and use it in GitHub Desktop.
Save ShrykeWindgrace/c7004b069d6abf97c7f07c1e0dbd29e3 to your computer and use it in GitHub Desktop.
Powershell tab-completion script for stack
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
}
@ShrykeWindgrace
Copy link
Author

ShrykeWindgrace commented Apr 22, 2022

This script works best in pwsh >= 6 (aka powershell core available on all major platforms), and with somewhat limited functionality in powershell (aka windows powershell, windows-only, duh).

Just replace the /path/to/stack with your own value, e.g. via

PS> (Get-Command stack).Source

or

$ which stack

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment