1use super::Generator;
2
3use crate::acore::Error;
4use crate::SHELL_PSH;
5
6#[derive(Debug, Clone, Copy, Default)]
7pub struct PowerShell;
8
9impl Generator for PowerShell {
10 type Err = Error;
11
12 fn is_avail(&self, name: &str) -> bool {
13 name == SHELL_PSH
14 }
15
16 fn generate(&self, name: &str, bin: &str) -> Result<String, Self::Err> {
17 let template = r#"Register-ArgumentCompleter -CommandName PROGRAM -ScriptBlock {
18 param($wordToComplete, $commandAst, $cursorPosition)
19
20 $words = $commandAst.CommandElements;
21 $currentWord = $wordToComplete;
22 $prevWord = if ($words.Count -gt 1 -and $words[-2]) { $words[-2] } else { 'PROGRAM' };
23
24 try {
25 $completions = & PROGRAM --_shell SHELL --_curr "`"$currentWord`"" --_prev "`"$prevWord`"" $words;
26
27 if ($LASTEXITCODE -eq 0) {
28 return $completions | ForEach-Object {
29 $split = $_.Split("`t");
30 $name = $split[0].Trim();
31 $desc = if ($split.Length -gt 1) { $split[1] } else { $name }
32
33 [System.Management.Automation.CompletionResult]::new($name, $name, 'ParameterValue', $desc)
34 }
35 }
36 }
37 catch { }
38
39 return @()
40};
41"#;
42
43 Ok(template
44 .replace("NAME", name)
45 .replace("PROGRAM", bin)
46 .replace("SHELL", SHELL_PSH))
47 }
48}