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

Add WSL2 support for hyperlinks #1608

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions src/hyperlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,21 @@ fn encode(f: &mut Formatter, byte: u8) -> fmt::Result {

#[cfg(unix)]
fn host() -> &'static str {
use std::sync::OnceLock;
use std::{env, sync::OnceLock};

static HOSTNAME: OnceLock<String> = OnceLock::new();

HOSTNAME
.get_or_init(|| {
nix::unistd::gethostname()
.ok()
.and_then(|h| h.into_string().ok())
.unwrap_or_default()
env::var("WSL_DISTRO_NAME").map_or_else(
|_| {
nix::unistd::gethostname()
.ok()
.and_then(|h| h.into_string().ok())
.unwrap_or_default()
},
|distro| format!("wsl$/{distro}"),
)
})
.as_ref()
}
Expand Down
17 changes: 13 additions & 4 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ mod testenv;

#[cfg(unix)]
use nix::unistd::{Gid, Group, Uid, User};
use std::fs;
use std::io::Write;
use std::path::Path;
use std::time::{Duration, SystemTime};
use std::{env, fs};
use test_case::test_case;

use normpath::PathExt;
Expand Down Expand Up @@ -2677,10 +2677,19 @@ fn test_gitignore_parent() {
fn test_hyperlink() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

#[cfg(unix)]
let hostname = nix::unistd::gethostname().unwrap().into_string().unwrap();
#[cfg(not(unix))]
let hostname = "";
let hostname = String::new();

#[cfg(unix)]
let hostname = env::var("WSL_DISTRO_NAME").map_or_else(
|_| {
nix::unistd::gethostname()
.ok()
.and_then(|h| h.into_string().ok())
.unwrap_or_default()
},
|distro| format!("wsl$/{distro}"),
);

let expected = format!(
"\x1b]8;;file://{}{}/a.foo\x1b\\a.foo\x1b]8;;\x1b\\",
Expand Down