Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include shell completion for Cargo #1425

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ gist is as simple as using one of the following:

```
# Bash
$ rustup completions bash > /etc/bash_completion.d/rustup.bash-completion
$ rustup completions bash >> ~/.bash_completion

# Bash (macOS/Homebrew)
$ rustup completions bash > $(brew --prefix)/etc/bash_completion.d/rustup.bash-completion
Expand Down Expand Up @@ -500,9 +500,9 @@ If you need a more complex setup, rustup supports the convention used by
the __curl__ program, documented in the ENVIRONMENT section of
[its manual page][curlman].

Note that some versions of `libcurl` apparently require you to drop the
`http://` or `https://` prefix in environment variables. For example,
`export http_proxy=proxy.example.com:1080` (and likewise for HTTPS).
Note that some versions of `libcurl` apparently require you to drop the
`http://` or `https://` prefix in environment variables. For example,
`export http_proxy=proxy.example.com:1080` (and likewise for HTTPS).
If you are getting an SSL `unknown protocol` error from `rustup` via `libcurl`
but the command-line `curl` command works fine, this may be the problem.

Expand Down
33 changes: 21 additions & 12 deletions src/rustup-cli/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Just a dumping ground for cli stuff

use clap::ArgMatches;
use rustup::{self, Cfg, Notification, Toolchain, UpdateStatus};
use rustup::telemetry_analysis::TelemetryAnalysis;
use errors::*;
Expand Down Expand Up @@ -326,24 +327,32 @@ pub fn list_components(toolchain: &Toolchain) -> Result<()> {
Ok(())
}

pub fn list_toolchains(cfg: &Cfg) -> Result<()> {
pub fn list_toolchains(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert the changes to this file as they should not be necessary.

let toolchains = cfg.list_toolchains()?;

if toolchains.is_empty() {
println!("no installed toolchains");
if !m.is_present("default") {
println!("no installed toolchains");
}
} else {
if let Ok(Some(def_toolchain)) = cfg.find_default() {
for toolchain in toolchains {
let if_default = if def_toolchain.name() == &*toolchain {
" (default)"
} else {
""
};
println!("{}{}", &toolchain, if_default);
if !m.is_present("default") {
if let Ok(Some(def_toolchain)) = cfg.find_default() {
for toolchain in toolchains {
let if_default = if def_toolchain.name() == &*toolchain {
" (default)"
} else {
""
};
println!("{}{}", &toolchain, if_default);
}
} else {
for toolchain in toolchains {
println!("{}", &toolchain);
}
}
} else {
for toolchain in toolchains {
println!("{}", &toolchain);
if let Ok(Some(def_toolchain)) = cfg.find_default() {
println!("{}", def_toolchain.name());
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/rustup-cli/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ r"DISCUSSION:

BASH:

Completion files are commonly stored in `/etc/bash_completion.d/`.
Run the command:
Completion files are commonly stored in `/etc/bash_completion.d/`,
but can be appended in `~/.bash_completion` too. Run the command:

$ rustup completions bash > /etc/bash_completion.d/rustup.bash-completion
$ rustup completions bash >> ~/.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.
Expand Down
37 changes: 34 additions & 3 deletions src/rustup-cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn main() -> Result<()> {
("default", Some(m)) => default_(cfg, m)?,
("toolchain", Some(c)) => match c.subcommand() {
("install", Some(m)) => update(cfg, m)?,
("list", Some(_)) => common::list_toolchains(cfg)?,
("list", Some(m)) => common::list_toolchains(cfg, m)?,
("link", Some(m)) => toolchain_link(cfg, m)?,
("uninstall", Some(m)) => toolchain_remove(cfg, m)?,
(_, _) => unreachable!(),
Expand Down Expand Up @@ -80,11 +80,34 @@ pub fn main() -> Result<()> {
},
("completions", Some(c)) => {
if let Some(shell) = c.value_of("shell") {
let shell = shell.parse::<Shell>().unwrap();
let prefix = "~/.rustup/toolchains/$(rustup toolchain list --default)";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prefix is not correct - rustup is not necessarily installed here, and the default toolchain isn't necessarily correct.

To get the installation directory of the active toolchain, you should use rustc --print sysroot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does to replace the rustup toolchain list --default for rustc --print sysroot solves the problem? I have no way to know the toolchain that the user is using, just the default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Diggsey Help me to finish this, please.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustc --print sysroot will call the binary of the currently used toolchain.


cli().gen_completions_to(
"rustup",
shell.parse::<Shell>().unwrap(),
shell,
&mut io::stdout(),
);

match shell {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-trivial logic like this should be pulled out into its own function.

Shell::Bash => {
writeln!(
&mut io::stdout(),
"\n. {}{}",
prefix,
"/etc/bash_completion.d/cargo"
);
}
Shell::Zsh => {
writeln!(
&mut io::stdout(),
"\n. {}{}",
prefix,
"/share/zsh/site-functions/_cargo"
);
}
_ => (),
};
}
}
(_, _) => unreachable!(),
Expand Down Expand Up @@ -172,7 +195,15 @@ pub fn cli() -> App<'static, 'static> {
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list").about("List installed toolchains"))
.subcommand(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should not be necessary

Copy link
Contributor Author

@ricvelozo ricvelozo Jun 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you requesting to remove the new --default arg?

SubCommand::with_name("list")
.about("List installed toolchains")
.arg(
Arg::with_name("default")
.long("default")
.help("Show only the default toolchain"),
),
)
.subcommand(
SubCommand::with_name("install")
.about("Install or update a given toolchain")
Expand Down