| 1 | //! [`COMPLETE=$SHELL <bin>`][CompleteEnv] completion integration |
| 2 | //! |
| 3 | //! See [`CompleteEnv`]: |
| 4 | //! ```rust |
| 5 | //! # use clap_complete::CompleteEnv; |
| 6 | //! fn cli() -> clap::Command { |
| 7 | //! // ... |
| 8 | //! # clap::Command::new("empty") |
| 9 | //! } |
| 10 | //! |
| 11 | //! CompleteEnv::with_factory(cli) |
| 12 | //! .complete(); |
| 13 | //! |
| 14 | //! // ... rest of application logic |
| 15 | //! ``` |
| 16 | //! |
| 17 | //! To customize completions, see |
| 18 | //! - [`ValueHint`][crate::ValueHint] |
| 19 | //! - [`ValueEnum`][clap::ValueEnum] |
| 20 | //! - [`ArgValueCandidates`][crate::ArgValueCandidates] |
| 21 | //! - [`ArgValueCompleter`][crate::ArgValueCompleter] |
| 22 | //! |
| 23 | //! To source your completions: |
| 24 | //! |
| 25 | //! <div class="warning"> |
| 26 | //! |
| 27 | //! **WARNING:** We recommend re-sourcing your completions on upgrade. |
| 28 | //! These completions work by generating shell code that calls into `your_program` while completing. |
| 29 | //! That interface is unstable and a mismatch between the shell code and `your_program` may result |
| 30 | //! in either invalid completions or no completions being generated. |
| 31 | //! For this reason, we recommend generating the shell code anew on shell startup so that it is |
| 32 | //! "self-correcting" on shell launch, rather than writing the generated completions to a file. |
| 33 | //! |
| 34 | //! </div> |
| 35 | //! |
| 36 | //! **Bash** |
| 37 | //! ```bash |
| 38 | //! echo "source <(COMPLETE=bash your_program)" >> ~/.bashrc |
| 39 | //! ``` |
| 40 | //! |
| 41 | //! **Elvish** |
| 42 | //! ```elvish |
| 43 | //! echo "eval (E:COMPLETE=elvish your_program | slurp)" >> ~/.elvish/rc.elv |
| 44 | //! ``` |
| 45 | //! |
| 46 | //! **Fish** |
| 47 | //! ```fish |
| 48 | //! echo "COMPLETE=fish your_program | source" >> ~/.config/fish/completions/your_program.fish |
| 49 | //! ``` |
| 50 | //! |
| 51 | //! **Powershell** |
| 52 | //! ```powershell |
| 53 | //! echo '$env:COMPLETE = "powershell"; your_program | Out-String | Invoke-Expression; Remove-Item Env:\COMPLETE' >> $PROFILE |
| 54 | //! ``` |
| 55 | //! Note that to execute scripts in PowerShell on Windows, including [`$PROFILE`][$Profile], |
| 56 | //! the [execution policy][ExecutionPolicies] needs to be set to `RemoteSigned` at minimum. |
| 57 | //! |
| 58 | //! [$Profile]: https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/creating-profiles |
| 59 | //! [ExecutionPolicies]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies |
| 60 | //! |
| 61 | //! **Zsh** |
| 62 | //! ```zsh |
| 63 | //! echo "source <(COMPLETE=zsh your_program)" >> ~/.zshrc |
| 64 | //! ``` |
| 65 | //! |
| 66 | //! To disable completions, you can set `COMPLETE=` or `COMPLETE=0` |
| 67 | |
| 68 | mod shellsrust-analyzer cargo clap_complete 4.6.8 env/shells/; |
| 69 | |
| 70 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#; |
| 71 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# as _; |
| 72 | |
| 73 | pub use shellsrust-analyzer cargo clap_complete 4.6.8 env/shells/::*; |
| 74 | |
| 75 | /// Environment-activated completions for your CLI |
| 76 | /// |
| 77 | /// Benefits over a CLI completion argument or subcommand |
| 78 | /// - Performance: we don't need to generate [`clap::Command`] twice or parse arguments |
| 79 | /// - Flexibility: there is no concern over it interfering with other CLI logic |
| 80 | /// |
| 81 | /// **Warning:** `stdout` should not be written to before [`CompleteEnv::complete`] has had a |
| 82 | /// chance to run. |
| 83 | /// |
| 84 | /// # Examples |
| 85 | /// |
| 86 | /// ```rust |
| 87 | /// # use clap_complete::CompleteEnv; |
| 88 | /// fn cli() -> clap::Command { |
| 89 | /// // ... |
| 90 | /// # clap::Command::new("empty") |
| 91 | /// } |
| 92 | /// |
| 93 | /// CompleteEnv::with_factory(cli) |
| 94 | /// .complete() |
| 95 | /// |
| 96 | /// // ... rest of application logic |
| 97 | /// ``` |
| 98 | pub struct CompleteEnvrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#<'s, F> { |
| 99 | factoryrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#factory.: F, |
| 100 | varrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#var.: &'static str, |
| 101 | binrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#bin.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 102 | completerrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#completer.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 103 | shellsrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#shells.: Shellsrust-analyzer cargo clap_complete 4.6.8 env/Shells#<'s>, |
| 104 | } |
| 105 | |
| 106 | impl<'s, F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#() -> claprust-analyzer cargo clap 4.6.6 crate/::Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#> CompleteEnvrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#<'s, F> { |
| 107 | /// Complete a [`clap::Command`] |
| 108 | /// |
| 109 | /// # Example |
| 110 | /// |
| 111 | /// Builder: |
| 112 | /// ```rust |
| 113 | /// # use clap_complete::CompleteEnv; |
| 114 | /// fn cli() -> clap::Command { |
| 115 | /// // ... |
| 116 | /// # clap::Command::new("empty") |
| 117 | /// } |
| 118 | /// |
| 119 | /// CompleteEnv::with_factory(cli) |
| 120 | /// .complete() |
| 121 | /// |
| 122 | /// // ... rest of application logic |
| 123 | /// ``` |
| 124 | /// |
| 125 | /// Derive: |
| 126 | /// ``` |
| 127 | /// # use clap::Parser; |
| 128 | /// # use clap_complete::CompleteEnv; |
| 129 | /// use clap::CommandFactory as _; |
| 130 | /// |
| 131 | /// #[derive(Debug, Parser)] |
| 132 | /// struct Cli { |
| 133 | /// custom: Option<String>, |
| 134 | /// } |
| 135 | /// |
| 136 | /// CompleteEnv::with_factory(Cli::command) |
| 137 | /// .complete() |
| 138 | /// |
| 139 | /// // ... rest of application logic |
| 140 | /// ``` |
| 141 | pub fn with_factoryrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]with_factory().(factory: F) -> Selfrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`] { |
| 142 | Selfrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`] { |
| 143 | factoryrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#factory., |
| 144 | varrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#var.: "COMPLETE", |
| 145 | binrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#bin.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 146 | completerrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#completer.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 147 | shellsrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#shells.: Shellsrust-analyzer cargo clap_complete 4.6.8 env/Shells#::builtinsrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]builtins().(), |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /// Override the environment variable used for enabling completions |
| 152 | pub fn varrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]var().(mut self, var: &'static str) -> Selfrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`] { |
| 153 | self.varrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#var. = var; |
| 154 | self |
| 155 | } |
| 156 | |
| 157 | /// Override the name of the binary to complete |
| 158 | /// |
| 159 | /// Default: `Command::get_bin_name` |
| 160 | pub fn binrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]bin().(mut self, bin: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Selfrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`] { |
| 161 | self.binrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#bin. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(bin.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()); |
| 162 | self |
| 163 | } |
| 164 | |
| 165 | /// Override the binary to call to get completions |
| 166 | /// |
| 167 | /// Default: `args_os()[0]` |
| 168 | pub fn completerrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]completer().(mut self, completer: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) -> Selfrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`] { |
| 169 | self.completerrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#completer. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(completer.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()); |
| 170 | self |
| 171 | } |
| 172 | |
| 173 | /// Override the shells supported for completions |
| 174 | pub fn shellsrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]shells().(mut self, shells: Shellsrust-analyzer cargo clap_complete 4.6.8 env/Shells#<'s>) -> Selfrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`] { |
| 175 | self.shellsrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#shells. = shells; |
| 176 | self |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | impl<'s, F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#() -> claprust-analyzer cargo clap 4.6.6 crate/::Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#> CompleteEnvrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#<'s, F> { |
| 181 | /// Process the completion request and exit |
| 182 | /// |
| 183 | /// **Warning:** `stdout` should not be written to before this has had a |
| 184 | /// chance to run. |
| 185 | pub fn completerust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]complete().(self) { |
| 186 | let args = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::args_osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/args_os().(); |
| 187 | let current_dir = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::current_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/current_dir().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 188 | if self |
| 189 | .try_completerust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]try_complete().(args, current_dir.as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()) |
| 190 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|e| e.exitrust-analyzer cargo clap_builder 4.6.6 error/impl#[`Error<F>`]exit().()) |
| 191 | { |
| 192 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(0) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /// Process the completion request |
| 197 | /// |
| 198 | /// **Warning:** `stdout` should not be written to before or after this has run. |
| 199 | /// |
| 200 | /// Returns `true` if a command was completed and `false` if this is a regular run of your |
| 201 | /// application |
| 202 | pub fn try_completerust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]try_complete().( |
| 203 | self, |
| 204 | args: impl IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#>>, |
| 205 | current_dir: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, |
| 206 | ) -> claprust-analyzer cargo clap 4.6.6 crate/::errorrust-analyzer cargo clap_builder 4.6.6 error/::Resultrust-analyzer cargo clap_builder 4.6.6 error/Result#<bool> { |
| 207 | self.try_complete_rust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]try_complete_().(args.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|a| a.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), current_dir) |
| 208 | } |
| 209 | |
| 210 | fn try_complete_rust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]try_complete_().( |
| 211 | self, |
| 212 | mut args: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#>, |
| 213 | current_dir: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, |
| 214 | ) -> claprust-analyzer cargo clap 4.6.6 crate/::errorrust-analyzer cargo clap_builder 4.6.6 error/::Resultrust-analyzer cargo clap_builder 4.6.6 error/Result#<bool> { |
| 215 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name) = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::var_osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var_os().(self.varrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#var.) else { |
| 216 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false); |
| 217 | }; |
| 218 | if name.is_emptyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]is_empty().() || name rust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`PartialEq<&str>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`PartialEq<&str>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`PartialEq<&str>`]eq()."0" { |
| 219 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false); |
| 220 | } |
| 221 | |
| 222 | // Ensure any child processes called for custom completers don't activate their own |
| 223 | // completion logic. |
| 224 | // SAFETY: should only be called during application initialization. Yeah, this is a weak |
| 225 | // argument but the choice is that or staying on the old edition. |
| 226 | unsafe { |
| 227 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::remove_varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/remove_var().(self.varrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#var.); |
| 228 | } |
| 229 | |
| 230 | let shell = self |
| 231 | .shellsrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#shells. |
| 232 | .completer_for_pathrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]completer_for_path().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&name))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 233 | |
| 234 | let mut cmd = (self.factoryrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#factory.)(); |
| 235 | cmd.buildrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]build().(); |
| 236 | |
| 237 | let completer = args.removerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]remove().(0); |
| 238 | let escape_index = args |
| 239 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 240 | .positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|a| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().a rust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`PartialEq<&str>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`PartialEq<&str>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString][`PartialEq<&str>`]eq()."--") |
| 241 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|i| i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) |
| 242 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(args.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 243 | args.drainrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]drain().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#escape_index); |
| 244 | if args.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 245 | let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 246 | self.write_registrationrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]write_registration().(&cmd, current_dir, shell, completer, &mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 247 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().().write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(&buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 248 | } else { |
| 249 | let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 250 | shell.write_completerust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#write_complete().(&mut cmd, args, current_dir, &mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 251 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().().write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(&buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 252 | } |
| 253 | |
| 254 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(true) |
| 255 | } |
| 256 | |
| 257 | fn write_registrationrust-analyzer cargo clap_complete 4.6.8 env/impl#[`CompleteEnv<'s, F>`]write_registration().( |
| 258 | &self, |
| 259 | cmd: &claprust-analyzer cargo clap 4.6.6 crate/::Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#, |
| 260 | current_dir: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, |
| 261 | shell: &dyn EnvCompleterrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#, |
| 262 | completer: OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#, |
| 263 | buf: &mut dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 264 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 265 | let name = cmd.get_namerust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_name().(); |
| 266 | let bin = self |
| 267 | .binrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#bin. |
| 268 | .as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().() |
| 269 | .or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| cmd.get_bin_namerust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_bin_name().()) |
| 270 | .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| cmd.get_namerust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_name().()); |
| 271 | let completer = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(completer) = self.completerrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#completer..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().() { |
| 272 | completer.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().() |
| 273 | } else { |
| 274 | let mut completer = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<OsString>`]from().(completer); |
| 275 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(current_dir) = current_dir { |
| 276 | if 1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().completer.componentsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]components().().countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().() { |
| 277 | completer = current_dir.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(completer); |
| 278 | } |
| 279 | } |
| 280 | completer.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_string_lossy().().into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().() |
| 281 | }; |
| 282 | |
| 283 | shell.write_registrationrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#write_registration().(self.varrust-analyzer cargo clap_complete 4.6.8 env/CompleteEnv#var., name, bin, &completer, buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 284 | |
| 285 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /// Collection of shell-specific completers |
| 290 | pub struct Shellsrust-analyzer cargo clap_complete 4.6.8 env/Shells#<'s>(pub &'s [&'s dyn EnvCompleterrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#]); |
| 291 | |
| 292 | impl<'s> Shellsrust-analyzer cargo clap_complete 4.6.8 env/Shells#<'s> { |
| 293 | /// Select all of the built-in shells |
| 294 | pub const fn builtinsrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]builtins().() -> Selfrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`] { |
| 295 | Selfrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`](&[&Bashrust-analyzer cargo clap_complete 4.6.8 env/shells/Bash#, &Elvishrust-analyzer cargo clap_complete 4.6.8 env/shells/Elvish#, &Fishrust-analyzer cargo clap_complete 4.6.8 env/shells/Fish#, &Powershellrust-analyzer cargo clap_complete 4.6.8 env/shells/Powershell#, &Zshrust-analyzer cargo clap_complete 4.6.8 env/shells/Zsh#]) |
| 296 | } |
| 297 | |
| 298 | /// Find the specified [`EnvCompleter`] |
| 299 | pub fn completerrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]completer().(&self, name: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&dyn EnvCompleterrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#> { |
| 300 | self.0rust-analyzer cargo clap_complete 4.6.8 env/Shells#0..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|c| c.isrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#is().(name)) |
| 301 | } |
| 302 | |
| 303 | /// Find an [`EnvCompleter`] for the specified path to a shell. |
| 304 | fn completer_for_pathrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]completer_for_path().( |
| 305 | &self, |
| 306 | name: &stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, |
| 307 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&dyn EnvCompleterrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 308 | // Strip off the parent dir in case `$SHELL` was used |
| 309 | let name = name.file_stemrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_stem().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(name.as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]as_os_str().()); |
| 310 | // lossy won't match but this will delegate to unknown |
| 311 | // error |
| 312 | let name = name.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().(); |
| 313 | |
| 314 | let shell = self.completerrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]completer().(&name).ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| { |
| 315 | let shells = self |
| 316 | .namesrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]names().() |
| 317 | .enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() |
| 318 | .foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/enumerate/impl#[`Enumerate<I>`][Iterator]fold().(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(), |mut seed, (i, name)| { |
| 319 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write# as _; |
| 320 | let prefix = if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { "" } else { ", " }; |
| 321 | let _ = writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(&mut seed, "{prefix}`{name}`"); |
| 322 | seed |
| 323 | }); |
| 324 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]other().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("unknown shell `{name}`, expected one of {shells}")) |
| 325 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 326 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(shell) |
| 327 | } |
| 328 | |
| 329 | /// Collect all [`EnvCompleter::name`]s |
| 330 | pub fn namesrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]names().(&self) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = &'static str> + 's { |
| 331 | self.0rust-analyzer cargo clap_complete 4.6.8 env/Shells#0..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|c| c.namerust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#name().()) |
| 332 | } |
| 333 | |
| 334 | /// Iterate over [`EnvCompleter`]s |
| 335 | pub fn iterrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]iter().(&self) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = &dyn EnvCompleterrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#> { |
| 336 | self.0rust-analyzer cargo clap_complete 4.6.8 env/Shells#0..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().() |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /// Shell-integration for completions |
| 341 | /// |
| 342 | /// This will generally be called by [`CompleteEnv`]. |
| 343 | /// |
| 344 | /// This handles adapting between the shell and [`completer`][crate::engine::complete()]. |
| 345 | /// A `EnvCompleter` can choose how much of that lives within the registration script or |
| 346 | /// lives in [`EnvCompleter::write_complete`]. |
| 347 | pub trait EnvCompleterrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter# { |
| 348 | /// Canonical name for this shell |
| 349 | /// |
| 350 | /// **Post-conditions:** |
| 351 | /// ```rust,ignore |
| 352 | /// assert!(completer.is(completer.name())); |
| 353 | /// ``` |
| 354 | fn namerust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#name().(&self) -> &'static str; |
| 355 | /// Whether the name matches this shell |
| 356 | /// |
| 357 | /// This should match [`EnvCompleter::name`] and any alternative names, particularly used by |
| 358 | /// `$SHELL`. |
| 359 | fn isrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#is().(&self, name: &str) -> bool; |
| 360 | /// Register for completions |
| 361 | /// |
| 362 | /// Write the `buf` the logic needed for calling into `<VAR>=<shell> <cmd> --`, passing needed |
| 363 | /// arguments to [`EnvCompleter::write_complete`] through the environment. |
| 364 | /// |
| 365 | /// - `var`: see [`CompleteEnv::var`] |
| 366 | /// - `name`: an identifier to use in the script |
| 367 | /// - `bin`: see [`CompleteEnv::bin`] |
| 368 | /// - `completer`: see [`CompleteEnv::completer`] |
| 369 | /// |
| 370 | /// <div class="warning"> |
| 371 | /// |
| 372 | /// **WARNING:** There are no stability guarantees between the call to |
| 373 | /// [`EnvCompleter::write_complete`] that this generates and actually calling [`EnvCompleter::write_complete`]. |
| 374 | /// Caching the results of this call may result in invalid or no completions to be generated. |
| 375 | /// |
| 376 | /// </div> |
| 377 | fn write_registrationrust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#write_registration().( |
| 378 | &self, |
| 379 | var: &str, |
| 380 | name: &str, |
| 381 | bin: &str, |
| 382 | completer: &str, |
| 383 | buf: &mut dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 384 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>; |
| 385 | /// Complete the given command |
| 386 | /// |
| 387 | /// Adapt information from arguments and [`EnvCompleter::write_registration`]-defined env |
| 388 | /// variables to what is needed for [`completer`][crate::engine::complete()]. |
| 389 | /// |
| 390 | /// Write out the [`CompletionCandidate`][crate::engine::CompletionCandidate]s in a way the shell will understand. |
| 391 | fn write_completerust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#write_complete().( |
| 392 | &self, |
| 393 | cmd: &mut claprust-analyzer cargo clap 4.6.6 crate/::Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#, |
| 394 | args: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#>, |
| 395 | current_dir: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, |
| 396 | buf: &mut dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 397 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#>; |
| 398 | } |
| 399 | |
| 400 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 401 | fn shells_completer_for_path_finds_powershellrust-analyzer cargo clap_complete 4.6.8 env/shells_completer_for_path_finds_powershell().() { |
| 402 | const SHELLSrust-analyzer cargo clap_complete 4.6.8 env/SHELLS.: Shellsrust-analyzer cargo clap_complete 4.6.8 env/Shells#<'_> = Shellsrust-analyzer cargo clap_complete 4.6.8 env/Shells#::builtinsrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]builtins().(); |
| 403 | |
| 404 | let paths = &["/usr/bin/pwsh", "powershell.exe", "powershell_ise.exe"]; |
| 405 | for path in paths { |
| 406 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!( |
| 407 | matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(SHELLSrust-analyzer cargo clap_complete 4.6.8 env/SHELLS..completer_for_pathrust-analyzer cargo clap_complete 4.6.8 env/impl#[`Shells<'s>`]completer_for_path().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(path)), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(s) if s.namerust-analyzer cargo clap_complete 4.6.8 env/EnvCompleter#name().() == "powershell"), |
| 408 | "failed to detect 'powershell' from: {path}", |
| 409 | ); |
| 410 | } |
| 411 | } |