Skip to content

Commit

Permalink
frontend: open now uses wsl view on wsl systems
Browse files Browse the repository at this point in the history
Signed-off-by: aserowy <serowy@hotmail.com>
  • Loading branch information
aserowy committed Feb 19, 2024
1 parent 9643520 commit 88b86e3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 110 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# yeet (y337)
# yeet or y337

## the name, the vision

Expand Down
3 changes: 3 additions & 0 deletions yeet-frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ thiserror = "1.0.56"
notify = { version = "6.1.1", default-features = false, features = ["macos_fsevent"] }
infer = "0.15.0"
dirs = "5.0.1"

[target.'cfg(all(unix, not(macos)))'.dependencies]
pathdiff = "0.2.0"
175 changes: 66 additions & 109 deletions yeet-frontend/src/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,138 +9,95 @@ use std::{

use tokio::process::Command;

// pub fn that(path: impl AsRef<OsStr>) -> io::Result<()> {
// let mut last_err = None;
// for mut cmd in commands(path) {
// match cmd.status_without_output() {
// Ok(status) => {
// return Ok(status).into_result(&cmd);
// }
// Err(err) => last_err = Some(err),
// }
// }
// Err(last_err.expect("no launcher worked, at least one error"))
// }

// pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
// let path = path.as_ref();
// let mut commands: Vec<(&str, Vec<&OsStr>)> = vec![];
//
// let wsl_path = wsl_path(path);
// CUSTOM DEPENDENCY: is_wsl
// if is_wsl::is_wsl() {
// commands.push(("wslview", vec![&wsl_path]));
// }
//
// commands.extend_from_slice(&[
// ("xdg-open", vec![&path]),
// ("gio", vec![OsStr::new("open"), path]),
// ("gnome-open", vec![path]),
// ("kde-open", vec![path]),
// ]);
//
// commands
// .iter()
// .map(|(command, args)| {
// let mut cmd = Command::new(command);
// cmd.args(args);
// cmd
// })
// .collect()
// }

// fn wsl_path<T: AsRef<OsStr>>(path: T) -> OsString {
// fn path_relative_to_current_dir<T: AsRef<OsStr>>(path: T) -> Option<PathBuf> {
// let path = Path::new(&path);
//
// if path.is_relative() {
// return None;
// }
//
// let base = env::current_dir().ok()?;
// pathdiff::diff_paths(path, base)
// }
//
// match path_relative_to_current_dir(&path) {
// None => OsString::from(&path),
// Some(relative) => OsString::from(relative),
// }
// }

// TODO: add wsl support
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
target_os = "solaris"
))]
#[cfg(all(unix, not(macos)))]
pub async fn path(path: &Path) -> Result<ExitStatus, io::Error> {
let mut cmd = Command::new("xdg-open")
.arg(path)
.spawn()
.expect("Failed to open file");
use std::{env, path::PathBuf};

use tokio::fs;

async fn is_wsl() -> bool {
if std::env::consts::OS != "linux" {
return false;
}
if let Ok(osrelease) = fs::read_to_string("/proc/sys/kernel/osrelease").await {
if osrelease.to_lowercase().contains("microsoft") {
return !is_docker().await;
}
}
if let Ok(version) = fs::read_to_string("/proc/version").await {
if version.to_lowercase().contains("microsoft") {
return !is_docker().await;
}
}

false
}

async fn is_docker() -> bool {
match fs::read_to_string("/proc/self/cgroup").await {
Ok(file_contents) => {
file_contents.contains("docker") || fs::metadata("/.dockerenv").await.is_ok()
}
Err(_error) => false,
}
}

fn wsl_path<T: AsRef<OsStr>>(path: T) -> OsString {
fn path_relative_to_current_dir<T: AsRef<OsStr>>(path: T) -> Option<PathBuf> {
let path = Path::new(&path);
if path.is_relative() {
return None;
}

let base = env::current_dir().ok()?;
pathdiff::diff_paths(path, base)
}

match path_relative_to_current_dir(&path) {
None => OsString::from(&path),
Some(relative) => OsString::from(relative),
}
}

let mut cmd = if is_wsl().await {
Command::new("wslview").arg(wsl_path(path)).spawn()?
} else {
Command::new("xdg-open").arg(path).spawn()?
};
cmd.wait().await
}

#[cfg(target_os = "macos")]
#[cfg(macos)]
pub async fn path(path: &Path) -> Result<ExitStatus, io::Error> {
let mut cmd = Command::new("/usr/bin/open")
.arg(path)
.spawn()
.expect("Failed to open file");

let mut cmd = Command::new("/usr/bin/open").arg(path).spawn()?;
cmd.wait().await
}

#[cfg(target_os = "redox")]
#[cfg(redox)]
pub async fn path(path: &Path) -> Result<ExitStatus, io::Error> {
let mut cmd = Command::new("/ui/bin/launcher")
.arg(path)
.spawn()
.expect("Failed to open file");

let mut cmd = Command::new("/ui/bin/launcher").arg(path).spawn()?;
cmd.wait().await
}

#[cfg(target_os = "windows")]
#[cfg(windows)]
pub async fn path(path: &Path) -> Result<ExitStatus, io::Error> {
const CREATE_NO_WINDOW: u32 = 0x08000000;
// const CREATE_NO_WINDOW: u32 = 0x08000000;

fn wrap_in_quotes<T: AsRef<OsStr>>(path: T) -> OsString {
fn wrap_in_quotes<T: AsRef<OsStr>>(path: T) -> String {
let mut result = OsString::from("\"");
result.push(path);
result.push("\"");

result
result.to_string_lossy().to_string()
}

let mut cmd = Command::new("cmd")
.arg("/c")
.arg("start")
.raw_arg("\"\"")
.raw_arg(wrap_in_quotes(path))
.creation_flags(CREATE_NO_WINDOW);
.args(&["/c", "start", "\"\"", &wrap_in_quotes(path)])
// .creation_flags(CREATE_NO_WINDOW); missing
.spawn()?;

// ... spawn n stuff
cmd.wait().await
}

#[cfg(not(any(
target_os = "linux",
target_os = "android",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
target_os = "solaris",
target_os = "ios",
target_os = "macos",
target_os = "windows",
target_os = "redox"
)))]
#[cfg(not(any(unix, windows, target_os = "macos", target_os = "redox")))]
compile_error!("open is not supported on this platform");

0 comments on commit 88b86e3

Please sign in to comment.