') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Reduce code duplication for show commands by djc · Pull Request #3813 · rust-lang/rustup · GitHub
Skip to content
Merged
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
102 changes: 35 additions & 67 deletions src/cli/common.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@ use crate::currentprocess::{
varsource::VarSource,
};
use crate::dist::dist::{TargetTriple, ToolchainDesc};
use crate::dist::manifest::ComponentStatus;
use crate::install::UpdateStatus;
use crate::utils::notifications as util_notifications;
use crate::utils::notify::NotificationLevel;
Expand DownExpand Up@@ -375,88 +376,55 @@ where
Ok(utils::ExitCode(0))
}

pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<utils::ExitCode> {
let mut t = process().stdout().terminal();
let manifestation = distributable.get_manifestation()?;
let config = manifestation.read_config()?.unwrap_or_default();
let manifest = distributable.get_manifest()?;
let components = manifest.query_components(distributable.desc(), &config)?;
for component in components {
if component.component.short_name_in_manifest() == "rust-std" {
let target = component
.component
.target
.as_ref()
.expect("rust-std should have a target");
if component.installed {
let _ = t.attr(terminalsource::Attr::Bold);
let _ = writeln!(t.lock(), "{target} (installed)");
let _ = t.reset();
} else if component.available {
let _ = writeln!(t.lock(), "{target}");
}
}
}

Ok(utils::ExitCode(0))
pub(crate) fn list_targets(
Comment thread
rami3l marked this conversation as resolved.
distributable: DistributableToolchain<'_>,
installed_only: bool,
) -> Result<utils::ExitCode> {
list_items(
distributable,
|c| {
(c.component.short_name_in_manifest() == "rust-std").then(|| {
c.component
.target
.as_deref()
.expect("rust-std should have a target")
})
},
installed_only,
)
}

pub(crate) fn list_installed_targets(
pub(crate) fn list_components(
distributable: DistributableToolchain<'_>,
installed_only: bool,
) -> Result<utils::ExitCode> {
let t = process().stdout();
let manifestation = distributable.get_manifestation()?;
let config = manifestation.read_config()?.unwrap_or_default();
let manifest = distributable.get_manifest()?;
let components = manifest.query_components(distributable.desc(), &config)?;
for component in components {
if component.component.short_name_in_manifest() == "rust-std" {
let target = component
.component
.target
.as_ref()
.expect("rust-std should have a target");
if component.installed {
writeln!(t.lock(), "{target}")?;
}
}
}
Ok(utils::ExitCode(0))
list_items(distributable, |c| Some(&c.name), installed_only)
}

pub(crate) fn list_components(
fn list_items(
distributable: DistributableToolchain<'_>,
f: impl Fn(&ComponentStatus) -> Option<&str>,
installed_only: bool,
) -> Result<utils::ExitCode> {
let mut t = process().stdout().terminal();

let manifestation = distributable.get_manifestation()?;
let config = manifestation.read_config()?.unwrap_or_default();
let manifest = distributable.get_manifest()?;
let components = manifest.query_components(distributable.desc(), &config)?;
for component in components {
let name = component.name;
if component.installed {
t.attr(terminalsource::Attr::Bold)?;
writeln!(t.lock(), "{name} (installed)")?;
t.reset()?;
} else if component.available {
writeln!(t.lock(), "{name}")?;
for component in distributable.components()? {
let Some(name) = f(&component) else { continue };
match (component.available, component.installed, installed_only) {
(false, _, _) | (_, false, true) => continue,
(true, true, false) => {
t.attr(terminalsource::Attr::Bold)?;
writeln!(t.lock(), "{name} (installed)")?;
t.reset()?;
}
(true, _, false) | (_, true, true) => {
writeln!(t.lock(), "{name}")?;
}
}
}

Ok(utils::ExitCode(0))
}

pub(crate) fn list_installed_components(distributable: DistributableToolchain<'_>) -> Result<()> {
let t = process().stdout();
for component in distributable.components()? {
if component.installed {
writeln!(t.lock(), "{}", component.name)?;
}
}
Ok(())
}

fn print_toolchain_path(
cfg: &Cfg,
toolchain: &str,
Expand Down
36 changes: 6 additions & 30 deletions src/cli/rustup_mode.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -1084,14 +1084,7 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
// active_toolchain will carry the reason we don't have one in its detail.
let active_targets = if let Ok(ref at) = active_toolchain {
if let Ok(distributable) = DistributableToolchain::try_from(&at.0) {
let components = (|| {
let manifestation = distributable.get_manifestation()?;
let config = manifestation.read_config()?.unwrap_or_default();
let manifest = distributable.get_manifest()?;
manifest.query_components(distributable.desc(), &config)
})();

match components {
match distributable.components() {
Ok(cs_vec) => cs_vec
.into_iter()
.filter(|c| c.component.short_name_in_manifest() == "rust-std")
Expand DownExpand Up@@ -1266,12 +1259,7 @@ fn target_list(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
// downcasting required because the toolchain files can name any toolchain
let distributable = (&toolchain).try_into()?;

if m.get_flag("installed") {
common::list_installed_targets(distributable)
} else {
common::list_targets(distributable)
}
common::list_targets(distributable, m.get_flag("installed"))
}

fn target_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
Expand All@@ -1282,11 +1270,7 @@ fn target_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
// list_components *and* add_component would both be inappropriate for
// custom toolchains.
let distributable = DistributableToolchain::try_from(&toolchain)?;
let manifestation = distributable.get_manifestation()?;
let config = manifestation.read_config()?.unwrap_or_default();
let manifest = distributable.get_manifest()?;
let components = manifest.query_components(distributable.desc(), &config)?;

let components = distributable.components()?;
let mut targets: Vec<_> = m
.get_many::<String>("target")
.unwrap()
Expand DownExpand Up@@ -1364,12 +1348,7 @@ fn component_list(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
// downcasting required because the toolchain files can name any toolchain
let distributable = (&toolchain).try_into()?;

if m.get_flag("installed") {
common::list_installed_components(distributable)?;
} else {
common::list_components(distributable)?;
}
common::list_components(distributable, m.get_flag("installed"))?;
Ok(utils::ExitCode(0))
}

Expand DownExpand Up@@ -1564,11 +1543,8 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;

if let Ok(distributable) = DistributableToolchain::try_from(&toolchain) {
let manifestation = distributable.get_manifestation()?;
let config = manifestation.read_config()?.unwrap_or_default();
let manifest = distributable.get_manifest()?;
let components = manifest.query_components(distributable.desc(), &config)?;
if let [_] = components
if let [_] = distributable
.components()?
.into_iter()
.filter(|cstatus| {
cstatus.component.short_name_in_manifest() == "rust-docs" && !cstatus.installed
Expand Down
7 changes: 1 addition & 6 deletions src/dist/manifest.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -594,12 +594,7 @@ impl Component {
distributable: &DistributableToolchain<'_>,
fallback_target: Option<&TargetTriple>,
) -> Result<Self> {
let manifestation = distributable.get_manifestation()?;
let config = manifestation.read_config()?.unwrap_or_default();
let manifest = distributable.get_manifest()?;
let manifest_components = manifest.query_components(distributable.desc(), &config)?;

for component_status in manifest_components {
for component_status in distributable.components()? {
let short_name = component_status.component.short_name_in_manifest();
let target = component_status.component.target.as_ref();

Expand Down