Skip to content

Commit 0872b7c

Browse files
ede1998djc
authored andcommitted
add shell completion for flamegraph
1 parent ef0f77a commit 0872b7c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ This will make the `flamegraph` and
3333
binary directory. On most systems this is
3434
usually something like `~/.cargo/bin`.
3535

36+
## Shell auto-completion
37+
38+
At the moment, only `flamegraph` supports auto-completion. Supported shells are `bash`, `fish`, `zsh`, `powershell` and `elvish`.
39+
`cargo-flamegraph` does not support auto-completion because it is not as straight-forward to implement for custom cargo subcommands. See #153 for details.
40+
41+
How you enable auto-completion depends on your shell, e.g.
42+
```bash
43+
flamegraph --completions bash > $XDG_CONFIG_HOME/bash_completion # or /etc/bash_completion.d/
44+
```
45+
3646
## Examples
3747

3848
```

src/bin/flamegraph.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ struct Opt {
1212
#[structopt(short = "p", long = "pid")]
1313
pid: Option<u32>,
1414

15+
/// Generate shell completions for the given shell.
16+
#[structopt(long = "completions")]
17+
completions: Option<structopt::clap::Shell>,
18+
1519
#[structopt(flatten)]
1620
graph: flamegraph::Options,
1721

@@ -20,6 +24,12 @@ struct Opt {
2024

2125
fn main() -> anyhow::Result<()> {
2226
let opt = Opt::from_args();
27+
28+
if let Some(shell) = opt.completions {
29+
Opt::clap().gen_completions_to("flamegraph", shell, &mut std::io::stdout().lock());
30+
return Ok(());
31+
}
32+
2333
let workload = match (opt.pid, opt.trailing_arguments.is_empty()) {
2434
(Some(p), true) => Workload::Pid(p),
2535
(None, false) => Workload::Command(opt.trailing_arguments.clone()),

0 commit comments

Comments
 (0)