Skip to content

Commit 5373d93

Browse files
majaharami3l
authored andcommitted
Update format of toolchain list
Update the format of `rustup toolchain list` to have a similar flavour to the new `rustup show` format.
1 parent d83cdbf commit 5373d93

File tree

4 files changed

+75
-75
lines changed

4 files changed

+75
-75
lines changed

src/cli/common.rs

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use once_cell::sync::Lazy;
1414

1515
use super::self_update;
1616
use crate::cli::download_tracker::DownloadTracker;
17-
use crate::config::ActiveReason;
1817
use crate::currentprocess::{
1918
argsource::ArgSource,
2019
filesource::{StdinSource, StdoutSource},
@@ -24,6 +23,7 @@ use crate::currentprocess::{
2423
use crate::dist::dist::{TargetTriple, ToolchainDesc};
2524
use crate::dist::manifest::ComponentStatus;
2625
use crate::install::UpdateStatus;
26+
use crate::toolchain::names::{LocalToolchainName, ToolchainName};
2727
use crate::utils::notifications as util_notifications;
2828
use crate::utils::notify::NotificationLevel;
2929
use crate::utils::utils;
@@ -426,78 +426,72 @@ fn list_items(
426426
Ok(utils::ExitCode(0))
427427
}
428428

429-
fn print_toolchain_path(
430-
cfg: &Cfg,
431-
toolchain: &str,
432-
if_default: &str,
433-
if_override: &str,
434-
verbose: bool,
435-
) -> Result<()> {
436-
let toolchain_path = cfg.toolchains_dir.join(toolchain);
437-
let toolchain_meta = fs::symlink_metadata(&toolchain_path)?;
438-
let toolchain_path = if verbose {
439-
if toolchain_meta.is_dir() {
440-
format!("\t{}", toolchain_path.display())
441-
} else {
442-
format!("\t{}", fs::read_link(toolchain_path)?.display())
443-
}
444-
} else {
445-
String::new()
446-
};
447-
writeln!(
448-
process().stdout().lock(),
449-
"{}{}{}{}",
450-
&toolchain,
451-
if_default,
452-
if_override,
453-
toolchain_path
454-
)?;
455-
Ok(())
456-
}
457-
458429
pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCode> {
459-
// Work with LocalToolchainName to accommodate path based overrides
460-
let toolchains = cfg
461-
.list_toolchains()?
462-
.iter()
463-
.map(Into::into)
464-
.collect::<Vec<_>>();
430+
let toolchains = cfg.list_toolchains()?;
465431
if toolchains.is_empty() {
466432
writeln!(process().stdout().lock(), "no installed toolchains")?;
467433
} else {
468-
let def_toolchain_name = cfg.get_default()?.map(|t| (&t).into());
434+
let default_toolchain_name = cfg.get_default()?;
469435
let cwd = utils::current_dir()?;
470-
let ovr_toolchain_name =
471-
if let Ok(Some((toolchain, reason))) = cfg.find_active_toolchain(&cwd) {
472-
match reason {
473-
ActiveReason::Default => None,
474-
_ => Some(toolchain),
475-
}
436+
let active_toolchain_name: Option<ToolchainName> =
437+
if let Ok(Some((LocalToolchainName::Named(toolchain), _reason))) =
438+
cfg.find_active_toolchain(&cwd)
439+
{
440+
Some(toolchain)
476441
} else {
477442
None
478443
};
444+
479445
for toolchain in toolchains {
480-
let if_default = if def_toolchain_name.as_ref() == Some(&toolchain) {
481-
" (default)"
482-
} else {
483-
""
484-
};
485-
let if_override = if ovr_toolchain_name.as_ref() == Some(&toolchain) {
486-
" (override)"
487-
} else {
488-
""
489-
};
446+
let is_default_toolchain = default_toolchain_name.as_ref() == Some(&toolchain);
447+
let is_active_toolchain = active_toolchain_name.as_ref() == Some(&toolchain);
490448

491-
print_toolchain_path(
449+
print_toolchain(
492450
cfg,
493451
&toolchain.to_string(),
494-
if_default,
495-
if_override,
452+
is_default_toolchain,
453+
is_active_toolchain,
496454
verbose,
497455
)
498456
.context("Failed to list toolchains' directories")?;
499457
}
500458
}
459+
460+
fn print_toolchain(
461+
cfg: &Cfg,
462+
toolchain: &str,
463+
is_default: bool,
464+
is_active: bool,
465+
verbose: bool,
466+
) -> Result<()> {
467+
let toolchain_path = cfg.toolchains_dir.join(toolchain);
468+
let toolchain_meta = fs::symlink_metadata(&toolchain_path)?;
469+
let toolchain_path = if verbose {
470+
if toolchain_meta.is_dir() {
471+
format!(" {}", toolchain_path.display())
472+
} else {
473+
format!(" {}", fs::read_link(toolchain_path)?.display())
474+
}
475+
} else {
476+
String::new()
477+
};
478+
let status_str = match (is_default, is_active) {
479+
(true, true) => " (active, default)",
480+
(true, false) => " (default)",
481+
(false, true) => " (active)",
482+
(false, false) => "",
483+
};
484+
485+
writeln!(
486+
process().stdout().lock(),
487+
"{}{}{}",
488+
&toolchain,
489+
status_str,
490+
toolchain_path
491+
)?;
492+
Ok(())
493+
}
494+
501495
Ok(utils::ExitCode(0))
502496
}
503497

tests/suite/cli_rustup.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -834,27 +834,36 @@ fn list_default_toolchain() {
834834
config.expect_ok(&["rustup", "default", "nightly"]);
835835
config.expect_ok_ex(
836836
&["rustup", "toolchain", "list"],
837-
for_host!(
838-
r"nightly-{0} (default)
839-
"
840-
),
837+
for_host!("nightly-{0} (active, default)\n"),
841838
r"",
842839
);
843840
})
844841
});
845842
}
846843

847844
#[test]
848-
fn list_override_toolchain() {
845+
fn list_no_default_toolchain() {
846+
test(&|config| {
847+
config.with_scenario(Scenario::SimpleV2, &|config| {
848+
config.expect_ok(&["rustup", "install", "nightly"]);
849+
config.expect_ok(&["rustup", "default", "none"]);
850+
config.expect_ok_ex(
851+
&["rustup", "toolchain", "list"],
852+
for_host!("nightly-{0}\n"),
853+
r"",
854+
);
855+
})
856+
});
857+
}
858+
859+
#[test]
860+
fn list_no_default_override_toolchain() {
849861
test(&|config| {
850862
config.with_scenario(Scenario::SimpleV2, &|config| {
851863
config.expect_ok(&["rustup", "override", "set", "nightly"]);
852864
config.expect_ok_ex(
853865
&["rustup", "toolchain", "list"],
854-
for_host!(
855-
r"nightly-{0} (override)
856-
"
857-
),
866+
for_host!("nightly-{0} (active)\n"),
858867
r"",
859868
);
860869
})
@@ -869,10 +878,7 @@ fn list_default_and_override_toolchain() {
869878
config.expect_ok(&["rustup", "override", "set", "nightly"]);
870879
config.expect_ok_ex(
871880
&["rustup", "toolchain", "list"],
872-
for_host!(
873-
r"nightly-{0} (default) (override)
874-
"
875-
),
881+
for_host!("nightly-{0} (active, default)\n"),
876882
r"",
877883
);
878884
})

tests/suite/cli_v1.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ fn list_toolchains() {
9191
config.expect_ok(&["rustup", "update", "nightly"]);
9292
config.expect_ok(&["rustup", "update", "beta-2015-01-01"]);
9393
config.expect_stdout_ok(&["rustup", "toolchain", "list"], "nightly");
94-
config.expect_stdout_ok(&["rustup", "toolchain", "list", "-v"], "(default)\t");
94+
config.expect_stdout_ok(&["rustup", "toolchain", "list", "-v"], "(active, default) ");
9595
#[cfg(windows)]
9696
config.expect_stdout_ok(
9797
&["rustup", "toolchain", "list", "-v"],
98-
for_host!("\\toolchains\\nightly-{}"),
98+
for_host!(r"\toolchains\nightly-{}"),
9999
);
100100
#[cfg(not(windows))]
101101
config.expect_stdout_ok(
@@ -106,7 +106,7 @@ fn list_toolchains() {
106106
#[cfg(windows)]
107107
config.expect_stdout_ok(
108108
&["rustup", "toolchain", "list", "-v"],
109-
"\\toolchains\\beta-2015-01-01",
109+
r"\toolchains\beta-2015-01-01",
110110
);
111111
#[cfg(not(windows))]
112112
config.expect_stdout_ok(

tests/suite/cli_v2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ fn list_toolchains() {
129129
config.expect_ok(&["rustup", "update", "nightly"]);
130130
config.expect_ok(&["rustup", "update", "beta-2015-01-01"]);
131131
config.expect_stdout_ok(&["rustup", "toolchain", "list"], "nightly");
132-
config.expect_stdout_ok(&["rustup", "toolchain", "list", "-v"], "(default)\t");
132+
config.expect_stdout_ok(&["rustup", "toolchain", "list", "-v"], "(active, default) ");
133133
#[cfg(windows)]
134134
config.expect_stdout_ok(
135135
&["rustup", "toolchain", "list", "-v"],
136-
for_host!("\\toolchains\\nightly-{}"),
136+
for_host!(r"\toolchains\nightly-{}"),
137137
);
138138
#[cfg(not(windows))]
139139
config.expect_stdout_ok(
@@ -144,7 +144,7 @@ fn list_toolchains() {
144144
#[cfg(windows)]
145145
config.expect_stdout_ok(
146146
&["rustup", "toolchain", "list", "-v"],
147-
"\\toolchains\\beta-2015-01-01",
147+
r"\toolchains\beta-2015-01-01",
148148
);
149149
#[cfg(not(windows))]
150150
config.expect_stdout_ok(

0 commit comments

Comments
 (0)