Skip to content

Commit

Permalink
add shell completion for flamegraph
Browse files Browse the repository at this point in the history
  • Loading branch information
ede1998 committed Sep 5, 2021
1 parent ef0f77a commit d8ee9a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ This will make the `flamegraph` and
binary directory. On most systems this is
usually something like `~/.cargo/bin`.

## Shell auto-completion

At the moment, only `flamegraph` supports auto-completion. Supported shells are `bash`, `fish`, `zsh`, `powershell` and `elvish`.
`cargo-flamegraph` does not support auto-completion because it is not as straight-forward to implement for custom cargo subcommands. See #153 for details.

How you enable auto-completion depends on your shell, e.g.
```bash
flamegraph --completions bash > $XDG_CONFIG_HOME/bash_completion # or /etc/bash_completion.d/
```

## Examples

```
Expand Down
10 changes: 10 additions & 0 deletions src/bin/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ struct Opt {
#[structopt(short = "p", long = "pid")]
pid: Option<u32>,

/// Generate shell completions for the given shell.
#[structopt(long = "completions")]
completions: Option<structopt::clap::Shell>,

#[structopt(flatten)]
graph: flamegraph::Options,

Expand All @@ -20,6 +24,12 @@ struct Opt {

fn main() -> anyhow::Result<()> {
let opt = Opt::from_args();

if let Some(shell) = opt.completions {
Opt::clap().gen_completions_to("flamegraph", shell, &mut std::io::stdout().lock());
return Ok(());
}

let workload = match (opt.pid, opt.trailing_arguments.is_empty()) {
(Some(p), true) => Workload::Pid(p),
(None, false) => Workload::Command(opt.trailing_arguments.clone()),
Expand Down

0 comments on commit d8ee9a7

Please sign in to comment.