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

Implement wsl compatibility #56

Merged
merged 1 commit into from
Dec 27, 2022
Merged
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
23 changes: 21 additions & 2 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro_rules! try_browser {
/// The mechanism of opening the default browser is as follows:
/// 1. Attempt to use $BROWSER env var if available
/// 2. Attempt to use xdg-open
/// 3. Attempt to use window manager specific commands, like gnome-open, kde-open etc.
/// 3. Attempt to use window manager specific commands, like gnome-open, kde-open etc. incl. WSL
/// 4. Fallback to x-www-browser
#[inline]
pub fn open_browser_internal(browser: Browser, url: &str, options: &BrowserOptions) -> Result<()> {
Expand Down Expand Up @@ -63,6 +63,10 @@ fn open_browser_default(url: &str, options: &BrowserOptions) -> Result<()> {
.or_else(|_| try_browser!(options, "gio", "open", url))
.or_else(|_| try_browser!(options, "gvfs-open", url)),

"wsl" => try_browser!(options, "cmd.exe", "/c", "start", url)
.or_else(|_| try_browser!(options, "powershell.exe", "Start", url))
.or_else(|_| try_browser!(options, "wsl-open", url)),

_ => Err(r),
})
// at the end, we'll try x-www-browser and return the result as is
Expand Down Expand Up @@ -116,6 +120,18 @@ fn try_with_browser_env(url: &str, options: &BrowserOptions) -> Result<()> {
))
}

/// Check if we are inside WSL on Windows
#[inline]
fn is_wsl() -> bool {
if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") {
if let Ok(s) = std::str::from_utf8(&b) {
let a = s.to_ascii_lowercase();
return a.contains("microsoft") || a.contains("wsl");
}
}
false
}

/// Detect the desktop environment
#[inline]
fn guess_desktop_env() -> &'static str {
Expand All @@ -142,6 +158,9 @@ fn guess_desktop_env() -> &'static str {
} else if xcd.contains("xfce") || dsession.contains("xfce") {
// XFCE
"xfce"
} else if is_wsl() {
// WSL
"wsl"
} else {
// All others
unknown
Expand Down Expand Up @@ -230,7 +249,7 @@ fn run_command(cmd: &mut Command, background: bool, options: &BrowserOptions) ->
} else {
// if we're in foreground, use status() instead of spawn(), as we'd like to wait
// till completion.
// We also specifically don't supress anything here, because we're running here
// We also specifically don't suppress anything here, because we're running here
// most likely because of a text browser
cmd.status().and_then(|status| {
if status.success() {
Expand Down