| 1 |
// Copyright 2020 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::io::Write as _; |
| 16 |
|
| 17 |
use clap::Command; |
| 18 |
|
| 19 |
use crate::cli_util::CommandHelper; |
| 20 |
use crate::command_error::CommandError; |
| 21 |
use crate::ui::Ui; |
| 22 |
|
| 23 |
// Using an explicit `doc` attribute prevents rustfmt from mangling the list |
| 24 |
// formatting without disabling rustfmt for the entire struct. |
| 25 |
#[doc = r#"Print a command-line-completion script |
| 26 |
|
| 27 |
Apply it by running one of these: |
| 28 |
|
| 29 |
- Bash: `source <(jj util completion bash)` |
| 30 |
- Fish: `jj util completion fish | source` |
| 31 |
- Nushell: |
| 32 |
```nu |
| 33 |
jj util completion nushell | save -f "completions-jj.nu" |
| 34 |
use "completions-jj.nu" * # Or `source "completions-jj.nu"` |
| 35 |
``` |
| 36 |
- Zsh: |
| 37 |
```shell |
| 38 |
autoload -U compinit |
| 39 |
compinit |
| 40 |
source <(jj util completion zsh) |
| 41 |
``` |
| 42 |
|
| 43 |
See the docs on [command-line completion] for more details. |
| 44 |
|
| 45 |
[command-line completion]: |
| 46 |
https://docs.jj-vcs.dev/latest/install-and-setup/#command-line-completion |
| 47 |
"#] |
| 48 |
#[derive(clap::Args, Clone, Debug)] |
| 49 |
#[command(verbatim_doc_comment)] |
| 50 |
pub struct UtilCompletionArgs { |
| 51 |
shell: ShellCompletion, |
| 52 |
} |
| 53 |
|
| 54 |
pub async fn cmd_util_completion( |
| 55 |
ui: &mut Ui, |
| 56 |
command: &CommandHelper, |
| 57 |
args: &UtilCompletionArgs, |
| 58 |
) -> Result<(), CommandError> { |
| 59 |
let mut app = command.app().clone(); |
| 60 |
let buf = args.shell.generate(&mut app); |
| 61 |
ui.stdout().write_all(&buf)?; |
| 62 |
Ok(()) |
| 63 |
} |
| 64 |
|
| 65 |
/// Available shell completions |
| 66 |
#[derive(clap::ValueEnum, Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 67 |
enum ShellCompletion { |
| 68 |
Bash, |
| 69 |
Elvish, |
| 70 |
Fish, |
| 71 |
Nushell, |
| 72 |
PowerShell, |
| 73 |
Zsh, |
| 74 |
} |
| 75 |
|
| 76 |
impl ShellCompletion { |
| 77 |
fn generate(&self, cmd: &mut Command) -> Vec<u8> { |
| 78 |
use clap_complete::Shell; |
| 79 |
use clap_complete::generate; |
| 80 |
use clap_complete_nushell::Nushell; |
| 81 |
|
| 82 |
let mut buf = Vec::new(); |
| 83 |
|
| 84 |
let bin_name = "jj"; |
| 85 |
|
| 86 |
match self { |
| 87 |
Self::Bash => generate(Shell::Bash, cmd, bin_name, &mut buf), |
| 88 |
Self::Elvish => generate(Shell::Elvish, cmd, bin_name, &mut buf), |
| 89 |
Self::Fish => generate(Shell::Fish, cmd, bin_name, &mut buf), |
| 90 |
Self::Nushell => generate(Nushell, cmd, bin_name, &mut buf), |
| 91 |
Self::PowerShell => generate(Shell::PowerShell, cmd, bin_name, &mut buf), |
| 92 |
Self::Zsh => generate(Shell::Zsh, cmd, bin_name, &mut buf), |
| 93 |
} |
| 94 |
|
| 95 |
buf |
| 96 |
} |
| 97 |
} |