From 15c6c6da96eb438da9342f5052644d50671ce5d6 Mon Sep 17 00:00:00 2001 From: Tim <57221600+TiltedToast@users.noreply.github.com> Date: Fri, 23 Aug 2024 20:24:31 +0200 Subject: [PATCH 1/5] add wsl support for --hyperlink flag --- src/hyperlink.rs | 13 ++++++++++++- tests/tests.rs | 22 +++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/hyperlink.rs b/src/hyperlink.rs index 4ddd3efad..4bc781663 100644 --- a/src/hyperlink.rs +++ b/src/hyperlink.rs @@ -42,12 +42,23 @@ fn encode(f: &mut Formatter, byte: u8) -> fmt::Result { #[cfg(unix)] fn host() -> &'static str { - use std::sync::OnceLock; + use std::{process::Command, sync::OnceLock}; static HOSTNAME: OnceLock = OnceLock::new(); HOSTNAME .get_or_init(|| { + let output = Command::new("wslpath").args(["-w", "/"]).output(); + + if let Ok(output) = output { + if output.status.success() { + return String::from_utf8_lossy(&output.stdout) + .trim() + .trim_end_matches('\\') + .to_string(); + } + } + nix::unistd::gethostname() .ok() .and_then(|h| h.into_string().ok()) diff --git a/tests/tests.rs b/tests/tests.rs index 3d11cdcda..469179896 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -5,6 +5,7 @@ use nix::unistd::{Gid, Group, Uid, User}; use std::fs; use std::io::Write; use std::path::Path; +use std::process::Command; use std::time::{Duration, SystemTime}; use test_case::test_case; @@ -2677,10 +2678,25 @@ fn test_gitignore_parent() { fn test_hyperlink() { let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES); + let mut hostname = String::new(); + #[cfg(unix)] - let hostname = nix::unistd::gethostname().unwrap().into_string().unwrap(); - #[cfg(not(unix))] - let hostname = ""; + { + let output = Command::new("wslpath").args(["-w", "/"]).output(); + + if let Ok(output) = output { + if output.status.success() { + hostname = String::from_utf8_lossy(&output.stdout) + .trim() + .trim_end_matches('\\') + .to_string(); + } + } + + if hostname.is_empty() { + hostname = nix::unistd::gethostname().unwrap().into_string().unwrap(); + } + } let expected = format!( "\x1b]8;;file://{}{}/a.foo\x1b\\a.foo\x1b]8;;\x1b\\", From 9fc30861304dd1adaea6113286514a89488dbbcb Mon Sep 17 00:00:00 2001 From: Tim <57221600+TiltedToast@users.noreply.github.com> Date: Fri, 23 Aug 2024 22:48:58 +0200 Subject: [PATCH 2/5] use env vars instead of wslpath --- src/hyperlink.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/hyperlink.rs b/src/hyperlink.rs index 4bc781663..656bffe8c 100644 --- a/src/hyperlink.rs +++ b/src/hyperlink.rs @@ -42,27 +42,21 @@ fn encode(f: &mut Formatter, byte: u8) -> fmt::Result { #[cfg(unix)] fn host() -> &'static str { - use std::{process::Command, sync::OnceLock}; + use std::{env, sync::OnceLock}; static HOSTNAME: OnceLock = OnceLock::new(); HOSTNAME .get_or_init(|| { - let output = Command::new("wslpath").args(["-w", "/"]).output(); - - if let Ok(output) = output { - if output.status.success() { - return String::from_utf8_lossy(&output.stdout) - .trim() - .trim_end_matches('\\') - .to_string(); - } - } - - 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.localhost/{distro}"), + ) }) .as_ref() } From 8db2800a9539b3d4b9b2df8647888e566dc1e34c Mon Sep 17 00:00:00 2001 From: Tim <57221600+TiltedToast@users.noreply.github.com> Date: Fri, 23 Aug 2024 22:54:33 +0200 Subject: [PATCH 3/5] copy wsl prefix from ripgrep --- src/hyperlink.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hyperlink.rs b/src/hyperlink.rs index 656bffe8c..83cf3a1de 100644 --- a/src/hyperlink.rs +++ b/src/hyperlink.rs @@ -55,7 +55,7 @@ fn host() -> &'static str { .and_then(|h| h.into_string().ok()) .unwrap_or_default() }, - |distro| format!("wsl.localhost/{distro}"), + |distro| format!("wsl$/{distro}"), ) }) .as_ref() From 22df08ed0150ff46b8e6073908913bf805f6f6aa Mon Sep 17 00:00:00 2001 From: Tim <57221600+TiltedToast@users.noreply.github.com> Date: Mon, 26 Aug 2024 06:57:00 +0200 Subject: [PATCH 4/5] update test with new env variable based approach too --- tests/tests.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/tests/tests.rs b/tests/tests.rs index 469179896..1fe4b91b8 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -2,11 +2,11 @@ mod testenv; #[cfg(unix)] use nix::unistd::{Gid, Group, Uid, User}; -use std::fs; use std::io::Write; use std::path::Path; use std::process::Command; use std::time::{Duration, SystemTime}; +use std::{env, fs}; use test_case::test_case; use normpath::PathExt; @@ -2678,25 +2678,19 @@ fn test_gitignore_parent() { fn test_hyperlink() { let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES); - let mut hostname = String::new(); + #[cfg(not(unix))] + let hostname = String::new(); #[cfg(unix)] - { - let output = Command::new("wslpath").args(["-w", "/"]).output(); - - if let Ok(output) = output { - if output.status.success() { - hostname = String::from_utf8_lossy(&output.stdout) - .trim() - .trim_end_matches('\\') - .to_string(); - } - } - - if hostname.is_empty() { - hostname = nix::unistd::gethostname().unwrap().into_string().unwrap(); - } - } + 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\\", From 60e430f03f2a475ba5bedc1b2825ee827a201cba Mon Sep 17 00:00:00 2001 From: Tim <57221600+TiltedToast@users.noreply.github.com> Date: Mon, 26 Aug 2024 07:05:57 +0200 Subject: [PATCH 5/5] remove unused import in test --- tests/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/tests.rs b/tests/tests.rs index 1fe4b91b8..e7b032847 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -4,7 +4,6 @@ mod testenv; use nix::unistd::{Gid, Group, Uid, User}; use std::io::Write; use std::path::Path; -use std::process::Command; use std::time::{Duration, SystemTime}; use std::{env, fs}; use test_case::test_case;