From 6e0119fdac76c46787cb962094a5f9e43bbd5466 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Sun, 23 Oct 2016 19:51:11 -0400 Subject: [PATCH 1/3] Add command line completion support This commit adds two sections to the command line options to enable building of Shell Completion Scripts to `stdout`. This allows these scripts to be redirected to a file, effectively allowing the user to install these scripts at the location of their choosing. The arguments only accept the values `zsh`, `fish` and `bash`, which clap guards against. To use these scripts one must do one of the following *Note:* The commands `rustup completions ` and `rustup self --completions ` are synonyms for each other, and do the exact same thing. ``` $ rustup completions bash > /path/to/completions/dir/rustup.bash-completion ``` ``` $ rustup completions fish > ~/.config/fish/completions/rustup.fish ``` ``` $ mkdir ~/.zfunc $ rustup completions zsh > ~/.zfunc/_rustup $ echo "fpath+=~/.zfunc\ncominit" >> ~/.zshrc $ exec zsh ``` Relates to #278 --- src/rustup-cli/help.rs | 68 +++++++++++++++++++++++++++++++++++ src/rustup-cli/rustup_mode.rs | 15 ++++++-- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/rustup-cli/help.rs b/src/rustup-cli/help.rs index dcb8be4818..b9ab4f4faf 100644 --- a/src/rustup-cli/help.rs +++ b/src/rustup-cli/help.rs @@ -131,3 +131,71 @@ default browser. By default, it opens the documentation index. Use the various flags to open specific pieces of documentation."; + +pub static COMPLETIONS_HELP: &'static str = +r" +One can generate a completion script for `rustup` that is compatible with +a given shell. The script is output on `stdout` allowing one to re-direct +the output to the file of their choosing. Where you place the file will +depend on which shell, and which operating system you are using. Your +particular configuration may also determine where these scripts need +to be placed. + +Here are some common set ups for the three supported shells under +Unix and similar operating systems (such as GNU/Linux). + +BASH: + +Completion files are commonly stored in `/etc/bash_completion.d/` + +Run the command: + +`rustup completions bash > /etc/bash_completion.d/rustup.bash-completion` + +This installs the completion script. You may have to log out and log +back in to your shell session for the changes to take affect. + +FISH: + +Fish completion files are commonly stored in +`$HOME/.config/fish/completions` + +Run the command: +`rustup completions fish > ~/.config/fish/completions/rustup.fish` + +This installs the completion script. You may have to log out and log +back in to your shell session for the changes to take affect. + +ZSH: + +ZSH completions are commonly stored in any directory listed in your +`$fpath` variable. To use these completions, you must either add the +generated script to one of those directories, or add your own +to this list. + +Adding a custom directory is often the safest best if you're unsure +of which directory to use. First create the directory, for this +example we'll create a hidden directory inside our `$HOME` directory + +`mkdir ~/.zfunc` + +Then add the following lines to your `.zshrc` just before `compinit` + +`fpath+=~/.zfunc` + +Now you can install the completions script using the following command + +`rustup completions zsh > ~/.zfunc/_rustup` + +You must then either log out and log back in, or simply run + +`exec zsh` + +For the new completions to take affect. + +CUSTOM LOCATIONS: + +Alternatively, you could save these files to the place of your choosing, +such as a custom directory inside your $HOME. Doing so will require you +to add the proper directives, such as `source`ing inside your login +script. Consult your shells documentation for how to add such directives."; diff --git a/src/rustup-cli/rustup_mode.rs b/src/rustup-cli/rustup_mode.rs index 6ba71161aa..f005fe7db6 100644 --- a/src/rustup-cli/rustup_mode.rs +++ b/src/rustup-cli/rustup_mode.rs @@ -1,4 +1,4 @@ -use clap::{App, Arg, ArgGroup, AppSettings, SubCommand, ArgMatches}; +use clap::{App, Arg, ArgGroup, AppSettings, SubCommand, ArgMatches, Shell}; use common; use rustup::{Cfg, Toolchain, command}; use rustup::settings::TelemetryMode; @@ -11,7 +11,7 @@ use std::path::Path; use std::process::Command; use std::iter; use term2; -use std::io::Write; +use std::io::{self, Write}; use help::*; pub fn main() -> Result<()> { @@ -100,6 +100,11 @@ pub fn main() -> Result<()> { (_, _) => unreachable!(), } } + ("completions", Some(c)) => { + if let Some(shell) = c.value_of("shell") { + cli().gen_completions_to("rustup", shell.parse::().unwrap(), &mut io::stdout()); + } + } (_, _) => unreachable!(), } @@ -343,6 +348,12 @@ pub fn cli() -> App<'static, 'static> { .about("The triple used to identify toolchains when not specified") .arg(Arg::with_name("host_triple") .required(true)))) + .subcommand(SubCommand::with_name("completions") + .about("Generate completion scripts for your shell") + .after_help(COMPLETIONS_HELP) + .setting(AppSettings::ArgRequiredElseHelp) + .arg(Arg::with_name("shell") + .possible_values(&["bash", "fish", "zsh"]))) } fn maybe_upgrade_data(cfg: &Cfg, m: &ArgMatches) -> Result { From 09d3a39c41eafba9c8ad3374652e918f2717e392 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Sun, 23 Oct 2016 19:59:02 -0400 Subject: [PATCH 2/3] Updates README.md with instructions for installing completion scripts --- README.md | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d61ffbf37e..aae79a91a6 100644 --- a/README.md +++ b/README.md @@ -58,18 +58,36 @@ you are ready to Rust. If you decide Rust isn't your thing, you can completely remove it from your system by running `rustup self uninstall`. -#### Enable tab completion for Zsh +#### Enable tab completion for Bash, Fish, or Zsh -Copy [`src/rustup-cli/zsh/_rustup`](https://github.com/rust-lang-nursery/rustup.rs/blob/master/src/rustup-cli/zsh/_rustup) into a directory, e.g. `~/.zfunc/`, -then add the following line in your `~/.zshrc` before `compinit`: +`rustup` now supports generating completion scripts for Bash, Fish, +and Zsh. See `rustup help completions` for full details, but the +gist is as simple as using one of the following: -```zsh -fpath+=~/.zfunc ``` +# Bash +$ rustup completions bash > /etc/bash_completions.d/rustup.bash-completion + +# Fish +$ rustup completions fish > ~/.config/fish/completions/rustup.fish + +# Zsh +$ rustup completions zsh > ~/.zfunc/_rustup +``` + +*Note:* you may need to restart your shell in order for the changes to take affect. -#### Enable tab completion for Bash +*Note:* For Zsh, see additional details below -Waiting for [#278](https://github.com/rust-lang-nursery/rustup.rs/issues/278) +#### Addtional Notes for Zsh + +One can alternatively copy [`src/rustup-cli/zsh/_rustup`](https://github.com/rust-lang-nursery/rustup.rs/blob/master/src/rustup-cli/zsh/_rustup) into a directory, e.g. `~/.zfunc/`. + +Regardless of method, you must then add the following line in your `~/.zshrc` before `compinit`: + +```zsh +fpath+=~/.zfunc +``` ## How rustup works From 79dcc1e142cecb7ea45048596970ce25ff245e00 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Sun, 23 Oct 2016 20:15:34 -0400 Subject: [PATCH 3/3] updates clap for zsh completions --- Cargo.lock | 6 +++--- Cargo.toml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b9734d3325..3f1170f5ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -87,7 +87,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "clap" -version = "2.15.0" +version = "2.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -544,7 +544,7 @@ dependencies = [ name = "rustup" version = "0.6.4" dependencies = [ - "clap 2.15.0 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.16.1 (registry+https://github.com/rust-lang/crates.io-index)", "download 0.3.0", "error-chain 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -983,7 +983,7 @@ dependencies = [ "checksum base64 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ce110e5c96df1817009271c910626fa4b79c2f178d70f9857d768c3886ba6a0" "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" -"checksum clap 2.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c3ad95014a5d1926493801463817b2e7e3ee64e051361a560f805c5320cd17b1" +"checksum clap 2.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7d3f517329dfa6300c8415693c54e524f777dc470265bd73dfd1ff1699c3ee30" "checksum cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0e3d6405328b6edb412158b3b7710e2634e23f3614b9bb1c412df7952489a626" "checksum core-foundation 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "20a6d0448d3a99d977ae4a2aa5a98d886a923e863e81ad9ff814645b6feb3bbd" "checksum core-foundation-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "05eed248dc504a5391c63794fe4fb64f46f071280afaa1b73308f3c0ce4574c5" diff --git a/Cargo.toml b/Cargo.toml index 1a00f00357..49f93efa5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ rustls-backend = ["download/rustls-backend"] # Include in the default set to disable self-update and uninstall. no-self-update = [] -# Used to change behavior of self-update and uninstall if installed via MSI +# Used to change behavior of self-update and uninstall if installed via MSI msi-installed = [] [dependencies] @@ -36,7 +36,7 @@ rustup-dist = { path = "src/rustup-dist", version = "0.6.4" } rustup-utils = { path = "src/rustup-utils", version = "0.6.4" } download = { path = "src/download" } error-chain = "0.5.0" -clap = "2.11.3" +clap = "2.16.1" regex = "0.1.41" url = "1.1.0" term = "0.4.4"