From 58ef8a8f42a68c7a81118ef9120705730ce3f458 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 11 Apr 2026 16:50:57 +0800 Subject: Add shell completion script generation feature --- mingling_core/tmpls/comps/pwsl.ps1 | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 mingling_core/tmpls/comps/pwsl.ps1 (limited to 'mingling_core/tmpls/comps/pwsl.ps1') diff --git a/mingling_core/tmpls/comps/pwsl.ps1 b/mingling_core/tmpls/comps/pwsl.ps1 new file mode 100644 index 0000000..6d7d91d --- /dev/null +++ b/mingling_core/tmpls/comps/pwsl.ps1 @@ -0,0 +1,43 @@ +Register-ArgumentCompleter -CommandName <<>> -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 = <<>> __comp $args 2>$null + + if ($suggestions) { + $suggestions | ForEach-Object { + if ($_ -eq "_file_") { + $completionType = 'ProviderItem' + } else { + $completionType = 'ParameterValue' + } + [System.Management.Automation.CompletionResult]::new($_, $_, $completionType, $_) + } + } +} -- cgit