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
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repository = "https://github.com/rust-lang/cargo"
annotate-snippets = { version = "0.12.12", features = ["simd"] }
anstream = "1.0.0"
anstyle = "1.0.13"
anstyle-hyperlink = "1.0.1"
anyhow = "1.0.102"
base64 = "0.22.1"
blake3 = "1.8.3"
Expand Down Expand Up @@ -159,6 +160,7 @@ path = "src/cargo/lib.rs"
annotate-snippets.workspace = true
anstream.workspace = true
anstyle.workspace = true
anstyle-hyperlink = { workspace = true, features = ["file"] }
anyhow.workspace = true
base64.workspace = true
blake3.workspace = true
Expand Down
66 changes: 12 additions & 54 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use anstream::AutoStream;
use anstyle::Style;

use crate::util::errors::CargoResult;
use crate::util::hostname;
use crate::util::style::*;

pub use anstyle_hyperlink::Hyperlink;

/// An abstraction around console output that remembers preferences for output
/// verbosity and color.
pub struct Shell {
Expand All @@ -22,7 +23,6 @@ pub struct Shell {
/// Flag that indicates the current line needs to be cleared before
/// printing. Used when a progress bar is currently displayed.
needs_clear: bool,
hostname: Option<String>,
}

impl fmt::Debug for Shell {
Expand Down Expand Up @@ -62,7 +62,6 @@ impl Shell {
},
verbosity: Verbosity::Verbose,
needs_clear: false,
hostname: None,
}
}

Expand All @@ -72,7 +71,6 @@ impl Shell {
output: ShellOut::Write(AutoStream::never(out)), // strip all formatting on write
verbosity: Verbosity::Verbose,
needs_clear: false,
hostname: None,
}
}

Expand Down Expand Up @@ -334,8 +332,10 @@ impl Shell {
stdout, hyperlinks, ..
} => stdout.current_choice() == anstream::ColorChoice::AlwaysAnsi && *hyperlinks,
};
Hyperlink {
url: supports_hyperlinks.then_some(url),
if supports_hyperlinks {
Hyperlink::with_url(url)
} else {
Hyperlink::default()
}
}

Expand All @@ -347,41 +347,22 @@ impl Shell {
} => stderr.current_choice() == anstream::ColorChoice::AlwaysAnsi && *hyperlinks,
};
if supports_hyperlinks {
Hyperlink { url: Some(url) }
Hyperlink::with_url(url)
} else {
Hyperlink { url: None }
Hyperlink::default()
}
}

pub fn out_file_hyperlink(&mut self, path: &std::path::Path) -> Hyperlink<url::Url> {
let url = self.file_hyperlink(path);
pub fn out_file_hyperlink(&mut self, path: &std::path::Path) -> Hyperlink<String> {
let url = anstyle_hyperlink::path_to_url(path);
url.map(|u| self.out_hyperlink(u)).unwrap_or_default()
}

pub fn err_file_hyperlink(&mut self, path: &std::path::Path) -> Hyperlink<url::Url> {
let url = self.file_hyperlink(path);
pub fn err_file_hyperlink(&mut self, path: &std::path::Path) -> Hyperlink<String> {
let url = anstyle_hyperlink::path_to_url(path);
url.map(|u| self.err_hyperlink(u)).unwrap_or_default()
}

fn file_hyperlink(&mut self, path: &std::path::Path) -> Option<url::Url> {
let mut url = url::Url::from_file_path(path).ok()?;
// Do a best-effort of setting the host in the URL to avoid issues with opening a link
// scoped to the computer you've SSH'ed into
let hostname = if cfg!(windows) {
// Not supported correctly on windows
None
} else {
if let Some(hostname) = self.hostname.as_deref() {
Some(hostname)
} else {
self.hostname = hostname().ok().and_then(|h| h.into_string().ok());
self.hostname.as_deref()
}
};
let _ = url.set_host(hostname);
Some(url)
}

fn unstable_flags_rustc_unicode(&self) -> bool {
match &self.output {
ShellOut::Write(_) => false,
Expand Down Expand Up @@ -692,29 +673,6 @@ mod tests {
}
}

pub struct Hyperlink<D: fmt::Display> {
url: Option<D>,
}

impl<D: fmt::Display> Default for Hyperlink<D> {
fn default() -> Self {
Self { url: None }
}
}

impl<D: fmt::Display> fmt::Display for Hyperlink<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Some(url) = self.url.as_ref() else {
return Ok(());
};
if f.alternate() {
write!(f, "\x1B]8;;\x1B\\")
} else {
write!(f, "\x1B]8;;{url}\x1B\\")
}
}
}

#[cfg(unix)]
mod imp {
use super::{Shell, TtyWidth};
Expand Down
77 changes: 0 additions & 77 deletions src/cargo/util/hostname.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub use self::flock::{FileLock, Filesystem};
pub use self::graph::Graph;
pub use self::hasher::StableHasher;
pub use self::hex::{hash_u64, short_hash, to_hex};
pub use self::hostname::hostname;
pub use self::into_url::IntoUrl;
pub use self::into_url_with_base::IntoUrlWithBase;
pub(crate) use self::io::LimitErrorReader;
Expand Down Expand Up @@ -47,7 +46,6 @@ pub mod frontmatter;
pub mod graph;
mod hasher;
pub mod hex;
mod hostname;
pub mod important_paths;
pub mod interning;
pub mod into_url;
Expand Down