-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5646 from epage/powershell
feat(clap_complete): Add support for Powershell
- Loading branch information
Showing
7 changed files
with
450 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/// Completion support for Powershell | ||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
pub struct Powershell; | ||
|
||
impl crate::dynamic::shells::ShellCompleter for Powershell { | ||
fn file_name(&self, name: &str) -> String { | ||
format!("{name}.ps1") | ||
} | ||
|
||
fn write_registration( | ||
&self, | ||
_name: &str, | ||
bin: &str, | ||
completer: &str, | ||
buf: &mut dyn std::io::Write, | ||
) -> Result<(), std::io::Error> { | ||
let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin)); | ||
let completer = | ||
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer)); | ||
|
||
writeln!( | ||
buf, | ||
r#" | ||
Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{ | ||
param($wordToComplete, $commandAst, $cursorPosition) | ||
$results = Invoke-Expression "&{completer} complete --shell powershell -- $($commandAst.ToString())"; | ||
$results | ForEach-Object {{ | ||
$split = $_.Split("`t"); | ||
$cmd = $split[0]; | ||
if ($split.Length -eq 2) {{ | ||
$help = $split[1]; | ||
}} | ||
else {{ | ||
$help = $split[0]; | ||
}} | ||
[System.Management.Automation.CompletionResult]::new($cmd, $cmd, 'ParameterValue', $help) | ||
}} | ||
}}; | ||
"# | ||
) | ||
} | ||
|
||
fn write_complete( | ||
&self, | ||
cmd: &mut clap::Command, | ||
args: Vec<std::ffi::OsString>, | ||
current_dir: Option<&std::path::Path>, | ||
buf: &mut dyn std::io::Write, | ||
) -> Result<(), std::io::Error> { | ||
let index = args.len() - 1; | ||
let completions = crate::dynamic::complete(cmd, args, index, current_dir)?; | ||
|
||
for candidate in completions { | ||
write!(buf, "{}", candidate.get_content().to_string_lossy())?; | ||
if let Some(help) = candidate.get_help() { | ||
write!( | ||
buf, | ||
"\t{}", | ||
help.to_string().lines().next().unwrap_or_default() | ||
)?; | ||
} | ||
writeln!(buf)?; | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.