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

feat: make hyperlinks usable inside a WSL environment (support all paths) #925

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 32 additions & 4 deletions src/output/file_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,39 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {

// On Windows, `std::fs::canonicalize` adds the Win32 File prefix, which we need to remove
#[cfg(target_os = "windows")]
let abs_path = abs_path.strip_prefix("\\\\?\\").unwrap_or(&abs_path);
let abs_path = abs_path
.strip_prefix("\\\\?\\")
.unwrap_or(&abs_path)
.to_string();

let distro_name = std::env::var("WSL_DISTRO_NAME").ok();
let path = if let Some(distro_name) = distro_name {
if abs_path.starts_with("/mnt/") {
let parts: Vec<&str> = abs_path.split('/').collect();
if parts.len() > 2
&& parts[2].len() == 1
&& parts[2].chars().next().unwrap().is_ascii_alphabetic()
{
let mut windows_path = format!("{}:\\", parts[2].to_uppercase());
for part in &parts[3..] {
if !part.is_empty() {
windows_path.push_str(part);
windows_path.push('\\');
}
}
windows_path
} else {
format!("wsl$/{distro_name}{abs_path}")
}
} else {
format!("wsl$/{distro_name}{abs_path}")
}
} else {
abs_path
};
Comment on lines +435 to +459
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer if this code was written in a less nested way, the level of indentation makes it hard to read.


bits.push(ANSIString::from(format!(
"{HYPERLINK_START}file://{abs_path}{HYPERLINK_END}"
)));
let hyperlink = format!("{HYPERLINK_START}file://{path}{HYPERLINK_END}");
bits.push(ANSIString::from(hyperlink));

display_hyperlink = true;
}
Expand Down