Skip to content

Commit

Permalink
add with function
Browse files Browse the repository at this point in the history
  • Loading branch information
momf authored and Byron committed Mar 8, 2020
1 parent d3db8c7 commit 9b83669
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
#[cfg(windows)]
extern crate winapi;

#[cfg(not(windows))]
use std::process::Stdio;

use std::{
ffi::OsStr,
process::{Command, Stdio},
io,
process::ExitStatus,
thread
Expand Down Expand Up @@ -108,6 +110,54 @@ pub fn that<T: AsRef<OsStr> + Sized>(path: T) -> io::Result<ExitStatus> {
.wait()
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
pub fn with<T:AsRef<OsStr>+Sized>(path: T, app: impl Into<String>) -> io::Result<ExitStatus> {
Command::new(app.into())
.arg(path.as_ref())
.spawn()?
.wait()
}

#[cfg(target_os = "windows")]
pub fn with<T:AsRef<OsStr>+Sized>(path: T, app: impl Into<String>) -> io::Result<ExitStatus> {
use std::os::windows::ffi::OsStrExt;
use std::os::windows::process::ExitStatusExt;
use std::ptr;
use winapi::ctypes::c_int;
use winapi::um::shellapi::ShellExecuteW;

const SW_SHOW: c_int = 5;

let path = windows::convert_path(path.as_ref())?;
let operation: Vec<u16> = OsStr::new("open\0").encode_wide().collect();
let app_name: Vec<u16> = OsStr::new(&format!("{}\0", app.into())).encode_wide().collect();
let result = unsafe {
ShellExecuteW(
ptr::null_mut(),
operation.as_ptr(),
app_name.as_ptr(),
path.as_ptr(),
ptr::null(),
SW_SHOW,
)
};
if result as c_int > 32 {
Ok(ExitStatus::from_raw(0))
} else {
Err(io::Error::last_os_error())
}
}

#[cfg(target_os = "macos")]
pub fn with<T:AsRef<OsStr>+Sized>(path: T, app: impl Into<String>) -> io::Result<ExitStatus> {
Command::new("open")
.arg(path.as_ref())
.arg("-a")
.arg(app.into())
.spawn()?
.wait()
}

/// Convenience function for opening the passed path in a new thread.
/// See documentation of `that(...)` for more details.
pub fn that_in_background<T: AsRef<OsStr> + Sized>(
Expand All @@ -117,6 +167,15 @@ pub fn that_in_background<T: AsRef<OsStr> + Sized>(
thread::spawn(|| that(path))
}

pub fn with_in_background<T: AsRef<OsStr> + Sized>(
path: T,
app: impl Into<String>
) -> thread::JoinHandle<io::Result<ExitStatus>> {
let path = path.as_ref().to_os_string();
let app = app.into();
thread::spawn(|| with(path, app))
}

#[cfg(windows)]
mod windows {
use std::ffi::OsStr;
Expand Down

0 comments on commit 9b83669

Please sign in to comment.