Skip to content

Commit

Permalink
feat(console): add subcommand to gen shell completions (#336)
Browse files Browse the repository at this point in the history
This PR includes adding subcommand for shell completions. Generating a man page will be done as another PR.

```
❯ tokio-console
--ascii-only            --lang                  --no-duration-colors    --retain-for            -h                      gen-config
--colorterm             --log                   --no-terminated-colors  --version               <TARGET_ADDR>           help
--help                  --no-colors             --palette               -V                      gen-completion

❯ tokio-console gen-completion
--help      --version   -V          -h          bash        elvish      fish        powershell  zsh
```

Related to #325
  • Loading branch information
nrskt authored May 23, 2022
1 parent 95a17b6 commit df4d468
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions tokio-console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ humantime = "2.1.0"
serde = { version = "1", features = ["derive"] }
toml = "0.5"
dirs = "4"
clap_complete = "3"
2 changes: 2 additions & 0 deletions tokio-console/args.example
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ OPTIONS:
Print version information

SUBCOMMANDS:
gen-completion
Generate shell completions
gen-config
Generate a `console.toml` config file with the default configuration values, overridden
by any provided command-line arguments
Expand Down
30 changes: 29 additions & 1 deletion tokio-console/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::view::Palette;
use clap::{ArgGroup, Parser as Clap, Subcommand, ValueHint};
use clap::{ArgGroup, IntoApp, Parser as Clap, Subcommand, ValueHint};
use clap_complete::Shell;
use color_eyre::eyre::WrapErr;
use serde::{Deserialize, Serialize};
use std::fmt;
Expand Down Expand Up @@ -103,6 +104,18 @@ pub enum OptionalCmd {
/// $ tokio-console gen-config > console.toml
///
GenConfig,

/// Generate shell completions
///
/// The completion script will be written to stdout.
/// The completion script should be saved in the shell's completion directory.
/// This depends on which shell is in use.
GenCompletion {
#[clap(name = "install", long = "install")]
install: bool,
#[clap(arg_enum)]
shell: Shell,
},
}

#[derive(Debug, Clone, Copy, Deserialize)]
Expand Down Expand Up @@ -632,6 +645,21 @@ impl ConfigPath {
}
}

/// Generete completion scripts for each specified shell.
pub fn gen_completion(install: bool, shell: Shell) -> color_eyre::Result<()> {
let mut app = Config::command();
let mut buf: Box<dyn std::io::Write> = if install {
color_eyre::eyre::bail!(
"Automatically installing completion scripts is not currently supported on {}",
shell
)
} else {
Box::new(std::io::stdout())
};
clap_complete::generate(shell, &mut app, "tokio-console", &mut buf);
Ok(())
}

#[cfg(test)]
mod tests {
use std::{
Expand Down
16 changes: 11 additions & 5 deletions tokio-console/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ mod warnings;
async fn main() -> color_eyre::Result<()> {
let mut args = config::Config::parse()?;

if args.subcmd == Some(config::OptionalCmd::GenConfig) {
// Generate a default config file and exit.
let toml = args.gen_config_file()?;
println!("{}", toml);
return Ok(());
match args.subcmd {
Some(config::OptionalCmd::GenConfig) => {
// Generate a default config file and exit.
let toml = args.gen_config_file()?;
println!("{}", toml);
return Ok(());
}
Some(config::OptionalCmd::GenCompletion { install, shell }) => {
return config::gen_completion(install, shell);
}
None => {}
}

let retain_for = args.retain_for();
Expand Down

0 comments on commit df4d468

Please sign in to comment.