Skip to content

Commit

Permalink
feat/cli: generate shell completions
Browse files Browse the repository at this point in the history
Close #22
  • Loading branch information
kxxt committed May 15, 2024
1 parent 0828d3f commit 84c5ee8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ unicode-segmentation = "1.11.0"
unicode-width = "0.1.12"
serial_test = { version = "3.1.1", features = ["file_locks"]}
arcstr = "1.2.0"
clap_complete = "4.5.2"
# tui-prompts = { version = "0.3.11", path = "../../contrib/tui-prompts" }
# tui-popup = { version = "0.3.0", path = "../../contrib/tui-popup" }

Expand Down
16 changes: 14 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{num::ParseFloatError, path::PathBuf};
use std::{io::stdout, num::ParseFloatError, path::PathBuf};

use clap::{Parser, Subcommand};
use clap::{CommandFactory, Parser, Subcommand};

use crate::tui::app::AppLayout;

Expand Down Expand Up @@ -106,6 +106,11 @@ pub enum CliCommand {
)]
frame_rate: f64,
},
#[clap(about = "Generate shell completions for tracexec")]
GenerateCompletions {
#[arg(required = true, help = "The shell to generate completions for")]
shell: clap_complete::Shell,
},
}

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -134,3 +139,10 @@ fn frame_rate_parser(s: &str) -> Result<f64, ParseFrameRateError> {
Ok(v)
}
}

impl Cli {
pub fn generate_completions(shell: clap_complete::Shell) {
let mut cmd = Cli::command();
clap_complete::generate(shell, &mut cmd, env!("CARGO_CRATE_NAME"), &mut stdout())
}
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod action;
mod arch;
mod cache;
mod cli;
mod cmdbuilder;
mod event;
Expand All @@ -11,7 +12,6 @@ mod pty;
mod seccomp;
mod tracer;
mod tui;
mod cache;

use std::{
io::{stderr, stdout, BufWriter},
Expand Down Expand Up @@ -202,6 +202,9 @@ async fn main() -> color_eyre::Result<()> {
tui::restore_tui()?;
tracer_thread.join().unwrap()?;
}
CliCommand::GenerateCompletions { shell } => {
Cli::generate_completions(shell);
}
}
Ok(())
}
Expand Down

0 comments on commit 84c5ee8

Please sign in to comment.