Skip to content
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