Skip to content

Commit

Permalink
Modularize Code
Browse files Browse the repository at this point in the history
  • Loading branch information
hybras committed Feb 19, 2021
1 parent 311ad44 commit cb5bbd3
Showing 1 changed file with 121 additions and 107 deletions.
228 changes: 121 additions & 107 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,120 +46,17 @@
//! }
//! # }
//! ```
#[cfg(windows)]
extern crate winapi;

#[cfg(not(windows))]
use std::process::{Command, Stdio};

use std::{ffi::OsStr, io, process::ExitStatus, thread};

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
pub fn that<T: AsRef<OsStr> + Sized>(path: T) -> io::Result<ExitStatus> {
let path_ref = path.as_ref();
let mut last_err: io::Error = io::Error::from_raw_os_error(0);
for program in &["xdg-open", "gnome-open", "kde-open", "wslview"] {
match Command::new(program)
.stdout(Stdio::null())
.stderr(Stdio::null())
.arg(path_ref)
.spawn()
{
Ok(mut child) => return child.wait(),
Err(err) => {
last_err = err;
continue;
}
}
}
Err(last_err)
}

#[cfg(target_os = "windows")]
pub fn that<T: AsRef<OsStr> + Sized>(path: T) -> 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 result = unsafe {
ShellExecuteW(
ptr::null_mut(),
operation.as_ptr(),
path.as_ptr(),
ptr::null(),
ptr::null(),
SW_SHOW,
)
};
if result as c_int > 32 {
Ok(ExitStatus::from_raw(0))
} else {
Err(io::Error::last_os_error())
}
}
pub use windows::{that, with};

#[cfg(target_os = "macos")]
pub fn that<T: AsRef<OsStr> + Sized>(path: T) -> io::Result<ExitStatus> {
Command::new("open")
.stdout(Stdio::null())
.stderr(Stdio::null())
.arg(path.as_ref())
.spawn()?
.wait()
}
pub use macos::{that, with};

#[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()
}
pub use unix::{that, with};

/// Convenience function for opening the passed path in a new thread.
/// See documentation of `that(...)` for more details.
Expand All @@ -183,7 +80,12 @@ pub fn with_in_background<T: AsRef<OsStr> + Sized>(
mod windows {
use std::ffi::OsStr;
use std::io;
use std::os::windows::ffi::OsStrExt;
use std::os::windows::{ffi::OsStrExt, process::ExitStatusExt};
use std::process::ExitStatus;
use std::ptr;

use winapi::ctypes::c_int;
use winapi::um::shellapi::ShellExecuteW;

pub fn convert_path(path: &OsStr) -> io::Result<Vec<u16>> {
let mut maybe_result: Vec<_> = path.encode_wide().collect();
Expand All @@ -196,4 +98,116 @@ mod windows {
maybe_result.push(0);
Ok(maybe_result)
}

pub fn that<T: AsRef<OsStr> + Sized>(path: T) -> io::Result<ExitStatus> {
const SW_SHOW: c_int = 5;

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

pub fn with<T: AsRef<OsStr> + Sized>(
path: T,
app: impl Into<String>,
) -> io::Result<ExitStatus> {
const SW_SHOW: c_int = 5;

let path = 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")]
mod macos {

use std::{
ffi::OsStr,
io::Result,
process::{Command, ExitStatus, Stdio},
};

pub fn that<T: AsRef<OsStr> + Sized>(path: T) -> Result<ExitStatus> {
Command::new("open")
.stdout(Stdio::null())
.stderr(Stdio::null())
.arg(path.as_ref())
.spawn()?
.wait()
}

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

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
mod unix {

use std::{
ffi::OsStr,
io::{Error, Result},
process::{Command, ExitStatus, Stdio},
};

pub fn that<T: AsRef<OsStr> + Sized>(path: T) -> Result<ExitStatus> {
let path_ref = path.as_ref();
let mut last_err = Error::from_raw_os_error(0);
for program in &["xdg-open", "gnome-open", "kde-open", "wslview"] {
match Command::new(program)
.stdout(Stdio::null())
.stderr(Stdio::null())
.arg(path_ref)
.spawn()
{
Ok(mut child) => return child.wait(),
Err(err) => {
last_err = err;
continue;
}
}
}
Err(last_err)
}

pub fn with<T: AsRef<OsStr> + Sized>(path: T, app: impl Into<String>) -> Result<ExitStatus> {
Command::new(app.into()).arg(path.as_ref()).spawn()?.wait()
}
}

0 comments on commit cb5bbd3

Please sign in to comment.