diff options
Diffstat (limited to 'mingling_core/tmpls/comps')
| -rw-r--r-- | mingling_core/tmpls/comps/pwsh.ps1 | 112 | ||||
| -rw-r--r-- | mingling_core/tmpls/comps/pwsl.ps1 | 43 |
2 files changed, 112 insertions, 43 deletions
diff --git a/mingling_core/tmpls/comps/pwsh.ps1 b/mingling_core/tmpls/comps/pwsh.ps1 new file mode 100644 index 0000000..c9d9b97 --- /dev/null +++ b/mingling_core/tmpls/comps/pwsh.ps1 @@ -0,0 +1,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 + } +} diff --git a/mingling_core/tmpls/comps/pwsl.ps1 b/mingling_core/tmpls/comps/pwsl.ps1 deleted file mode 100644 index 6d7d91d..0000000 --- a/mingling_core/tmpls/comps/pwsl.ps1 +++ /dev/null @@ -1,43 +0,0 @@ -Register-ArgumentCompleter -CommandName <<<bin_name>>> -ScriptBlock { - param($wordToComplete, $commandAst, $cursorPosition) - - $line = $commandAst.ToString() - $commandName = if ($commandAst.CommandElements.Count -gt 0) { - $commandAst.CommandElements[0].Value - } else { "" } - - $words = @() - $currentIndex = 0 - $parser = [System.Management.Automation.PSParser] - $tokens = $parser::Tokenize($line, [ref]$null) - - foreach ($token in $tokens) { - if ($token.Type -in 'CommandArgument', 'CommandParameter') { - $words += $token.Content - } - } - - $args = @( - "-f", ($line -replace '-', '^') - "-C", $cursorPosition.ToString() - "-w", ($wordToComplete -replace '-', '^') - "-p", (if ($words.Count -gt 1) { $words[-2] } else { "" }) -replace '-', '^' - "-c", $commandName - "-i", ($words.Count - 1).ToString() - "-a", ($words | ForEach-Object { $_ -replace '-', '^' }) - "-F", "powershell" - ) - - $suggestions = <<<bin_name>>> __comp $args 2>$null - - if ($suggestions) { - $suggestions | ForEach-Object { - if ($_ -eq "_file_") { - $completionType = 'ProviderItem' - } else { - $completionType = 'ParameterValue' - } - [System.Management.Automation.CompletionResult]::new($_, $_, $completionType, $_) - } - } -} |
