aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/tmpls/comps
diff options
context:
space:
mode:
authorWeicao-CatilGrass <1992414357@qq.com>2026-04-12 20:37:01 +0800
committerWeicao-CatilGrass <1992414357@qq.com>2026-04-12 20:37:01 +0800
commit40578ba8e008e69d8fa0615a822b286617f37adb (patch)
tree545fa64e5999d9ab0e17d5f13c4e771d9f92d934 /mingling_core/tmpls/comps
parent7a51b02771110251cf120cd4d85be8082c59ef1a (diff)
Add Windows-specific flag detection and improve PowerShell completion
Diffstat (limited to 'mingling_core/tmpls/comps')
-rw-r--r--mingling_core/tmpls/comps/pwsh.ps186
1 files changed, 57 insertions, 29 deletions
diff --git a/mingling_core/tmpls/comps/pwsh.ps1 b/mingling_core/tmpls/comps/pwsh.ps1
index 6d7d91d..8a52a4f 100644
--- a/mingling_core/tmpls/comps/pwsh.ps1
+++ b/mingling_core/tmpls/comps/pwsh.ps1
@@ -1,43 +1,71 @@
-Register-ArgumentCompleter -CommandName <<<bin_name>>> -ScriptBlock {
+# PowerShell completion script for <<<bin_name>>>
+Register-ArgumentCompleter -Native -CommandName '<<<bin_name>>>' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
- $line = $commandAst.ToString()
- $commandName = if ($commandAst.CommandElements.Count -gt 0) {
- $commandAst.CommandElements[0].Value
- } else { "" }
+ $words = $commandAst.ToString().Split(' ')
+ $currentIndex = $words.IndexOf($wordToComplete)
+ if ($currentIndex -eq -1) { $currentIndex = $words.Count }
- $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
- }
- }
+ $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", ($line -replace '-', '^')
- "-C", $cursorPosition.ToString()
- "-w", ($wordToComplete -replace '-', '^')
- "-p", (if ($words.Count -gt 1) { $words[-2] } else { "" }) -replace '-', '^'
+ "-f", $buffer.Replace('-', '^')
+ "-C", $cursorPosition
+ "-w", $currentWord.Replace('-', '^')
+ "-p", $previousWord.Replace('-', '^')
"-c", $commandName
- "-i", ($words.Count - 1).ToString()
- "-a", ($words | ForEach-Object { $_ -replace '-', '^' })
- "-F", "powershell"
+ "-i", $wordIndex
+ "-a", ($words | ForEach-Object { $_.Replace('-', '^') }) -join ' '
+ "-F", "pwsh"
)
- $suggestions = <<<bin_name>>> __comp $args 2>$null
+ $suggestions = & <<<bin_name>>> __comp $args 2>$null
- if ($suggestions) {
- $suggestions | ForEach-Object {
- if ($_ -eq "_file_") {
- $completionType = 'ProviderItem'
+ 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 {
- $completionType = 'ParameterValue'
+ @()
+ }
+
+ $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', $_)
}
- [System.Management.Automation.CompletionResult]::new($_, $_, $completionType, $_)
}
}
}