aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/tmpls/comps/pwsh.ps1
blob: c9d9b97d2b53743b0a91744f8c715eb3a0eab01d (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# PowerShell completion script for <<<bin_name>>>
Register-ArgumentCompleter -Native -CommandName '<<<bin_name>>>' -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)

    $line = $commandAst.ToString()

    $elements = @()
    if ($commandAst.CommandElements.Count -gt 0) {
        $elements = $commandAst.CommandElements | ForEach-Object { $_.Value }
    }

    $commandName = if ($elements.Count -gt 0) { $elements[0] } else { "" }

    $currentWord = $wordToComplete
    $previousWord = ""
    $wordIndex = 0

    $found = $false
    for ($i = 0; $i -lt $elements.Count; $i++) {
        if ($elements[$i] -eq $currentWord) {
            $wordIndex = $i
            if ($i -gt 0) {
                $previousWord = $elements[$i - 1]
            }
            $found = $true
            break
        }
    }

    if (-not $found) {
        $wordIndex = $elements.Count
        if ($elements.Count -gt 0) {
            $previousWord = $elements[-1]
        }
    }

    $args = @(
        "-f", ($line -replace '-', '^')
        "-C", $cursorPosition.ToString()
        "-w", ($currentWord -replace '-', '^')
        "-p", ($previousWord -replace '-', '^')
        "-c", ($commandName -replace '-', '^')
        "-i", $wordIndex.ToString()
        "-F", "Powershell"
    )

    foreach ($element in $elements) {
        $args += "-a"
        $args += ($element -replace '-', '^')
    }

    $output = & <<<bin_name>>> __comp $args 2>&1
    $output = $output -replace "`r`n", "`n" -replace "`r", "`n"

    if (-not $output) {
        return @()
    }

    $lines = $output -split "`n"

    if ($lines.Count -eq 0) {
        return @()
    }

    $firstLine = $lines[0].Trim()

    if ($firstLine -eq "_file_") {
        if ($lines.Count -gt 1) {
            $fileSuggestions = $lines[1..($lines.Count-1)]
        } else {
            $fileSuggestions = @()
        }

        $completionResults = @()
        $fileSuggestions | ForEach-Object {
            $path = $_
            $isDirectory = $path.EndsWith([System.IO.Path]::DirectorySeparatorChar) -or $path.EndsWith('/')
            $completionType = if ($isDirectory) { 'ProviderContainer' } else { 'ProviderItem' }
            $completionResults += [System.Management.Automation.CompletionResult]::new($path, $path, $completionType, $path)
        }

        return $completionResults
    } else {
        $completionResults = @()

        foreach ($line in $lines) {
            $trimmedLine = $line.Trim()

            if ($trimmedLine -match '^([^$]+)\$\((.+)\)$') {
                $text = $matches[1]
                $description = $matches[2]
                $completionResults += [System.Management.Automation.CompletionResult]::new(
                    $text,
                    $text,
                    'ParameterValue',
                    $description
                )
            } else {
                $text = $trimmedLine
                $resultType = if ($text.StartsWith('-')) { 'ParameterName' } else { 'ParameterValue' }
                $completionResults += [System.Management.Automation.CompletionResult]::new(
                    $text,
                    $text,
                    $resultType,
                    $text
                )
            }
        }

        return $completionResults
    }
}