aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/tmpls/comps/pwsh.ps1
blob: 8a52a4f3ce937927a74b95a7c678d2bc161129d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# PowerShell completion script for <<<bin_name>>>
Register-ArgumentCompleter -Native -CommandName '<<<bin_name>>>' -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)

    $words = $commandAst.ToString().Split(' ')
    $currentIndex = $words.IndexOf($wordToComplete)
    if ($currentIndex -eq -1) { $currentIndex = $words.Count }

    $buffer = $commandAst.ToString()
    $currentWord = $wordToComplete
    $previousWord = if ($currentIndex -gt 1) { $words[$currentIndex - 2] } else { "" }
    $commandName = if ($words.Count -gt 0) { $words[0] } else { "" }
    $wordIndex = $currentIndex

    $args = @(
        "-f", $buffer.Replace('-', '^')
        "-C", $cursorPosition
        "-w", $currentWord.Replace('-', '^')
        "-p", $previousWord.Replace('-', '^')
        "-c", $commandName
        "-i", $wordIndex
        "-a", ($words | ForEach-Object { $_.Replace('-', '^') }) -join ' '
        "-F", "pwsh"
    )

    $suggestions = & <<<bin_name>>> __comp $args 2>$null

    if ($LASTEXITCODE -eq 0 -and $suggestions) {
        $completions = $suggestions -split "`n"

        if ($completions[0].Trim() -eq "_file_") {
            $completions = if ($completions.Count -gt 1) {
                $completions[1..($completions.Count-1)]
            } else {
                @()
            }

            $completions | ForEach-Object {
                $path = $_.Replace('^', '-')
                $isDirectory = $path.EndsWith([System.IO.Path]::DirectorySeparatorChar) -or $path.EndsWith('/')
                $completionType = if ($isDirectory) { 'ProviderContainer' } else { 'ProviderItem' }
                [System.Management.Automation.CompletionResult]::new($path, $path, $completionType, $path)
            }
        }
        else {
            $parsedCompletions = @()
            foreach ($item in $completions) {
                if ($item -match '^([^$]+)\$\((.+)\)$') {
                    $parsedCompletions += "$($matches[1]):$($matches[2])"
                }
                else {
                    $parsedCompletions += $item
                }
            }

            $simpleCompletions = @()
            foreach ($item in $parsedCompletions) {
                if ($item -match '^([^:]+):(.+)$') {
                    $simpleCompletions += $matches[1]
                }
                else {
                    $simpleCompletions += $item
                }
            }

            return $simpleCompletions | ForEach-Object {
                [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
            }
        }
    }
}