Skip to content

Commit

Permalink
feat(complete)!: Open to new shells for dynamic completions
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 19, 2023
1 parent 830dd74 commit 00e9217
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 266 deletions.
4 changes: 2 additions & 2 deletions clap_complete/examples/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ fn command() -> clap::Command {
.value_parser(["json", "yaml", "toml"]),
)
.args_conflicts_with_subcommands(true);
clap_complete::dynamic::bash::CompleteCommand::augment_subcommands(cmd)
clap_complete::dynamic::shells::CompleteCommand::augment_subcommands(cmd)
}

fn main() {
let cmd = command();
let matches = cmd.get_matches();
if let Ok(completions) =
clap_complete::dynamic::bash::CompleteCommand::from_arg_matches(&matches)
clap_complete::dynamic::shells::CompleteCommand::from_arg_matches(&matches)
{
completions.complete(&mut command());
} else {
Expand Down
262 changes: 0 additions & 262 deletions clap_complete/src/dynamic/bash.rs

This file was deleted.

24 changes: 23 additions & 1 deletion clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@ use std::ffi::OsString;

use clap_lex::OsStrExt as _;

/// Shell-specific completions
pub trait Completer {
/// The recommended file name for the registration code
fn file_name(&self, name: &str) -> String;
/// Register for completions
fn write_registration(
&self,
name: &str,
bin: &str,
completer: &str,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error>;
/// Complete the command
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>;
}

/// Complete the command specified
pub fn complete(
cmd: &mut clap::Command,
Expand Down Expand Up @@ -58,7 +80,7 @@ pub fn complete(

Err(std::io::Error::new(
std::io::ErrorKind::Other,
"No completion generated",
"no completion generated",
))
}

Expand Down
1 change: 0 additions & 1 deletion clap_complete/src/dynamic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

mod completer;

pub mod bash;
pub mod shells;

pub use completer::*;
Loading

0 comments on commit 00e9217

Please sign in to comment.