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

feat: add commands function #64

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions src/haiku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ use crate::{CommandExt, IntoResult};
pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
Command::new("/bin/open")
.arg(path.as_ref())
.status_without_output()
.without_io()
.status()
.into_result()
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Command::new(app.into())
.arg(path.as_ref())
.status_without_output()
.without_io()
.status()
.into_result()
}

pub fn commands<T: AsRef<OsStr>>(path: T) -> impl Iterator<Item = Command> {
let mut cmd = Command::new("/bin/open");
cmd.arg(path.as_ref());
Some(cmd).into_iter()
}
12 changes: 10 additions & 2 deletions src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
Command::new("uiopen")
.arg("--url")
.arg(path.as_ref())
.status_without_output()
.without_io()
.status()
.into_result()
}

Expand All @@ -16,6 +17,13 @@ pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()>
.arg(path.as_ref())
.arg("--bundleid")
.arg(app.into())
.status_without_output()
.without_io()
.status()
.into_result()
}

pub fn commands<T: AsRef<OsStr>>(path: T) -> impl Iterator<Item = Command> {
let mut cmd = Command::new("uiopen");
cmd.arg("--url").arg(path.as_ref());
Some(cmd).into_iter()
}
33 changes: 28 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
//! open::with("http://rust-lang.org", "firefox").unwrap();
//! ```
//!
//! Or obtain commands for opening the URL without running them.
//!
//! ```no_run
//! let cmds = open::commands("http://rust-lang.org");
//! ```
//!
//! # Notes
//!
//! ## Nonblocking operation
Expand Down Expand Up @@ -131,6 +137,21 @@ pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()>
os::with(path, app)
}

/// Get iterator over commands that open path with the default application.
/// Iterator over Commands is returned as for certain platforms there are
/// multiple command options.
/// It is the responsibility of the callee to try all commands.
///
/// # Examples
///
/// ```no_run
/// let path = "http://rust-lang.org";
/// let cmds = open::commands(path);
/// ```
pub fn commands<T: AsRef<OsStr>>(path: T) -> impl Iterator<Item = Command> {
os::commands(path)
}

/// Open path with the default application in a new thread.
///
/// See documentation of [`that()`] for more details.
Expand Down Expand Up @@ -182,17 +203,19 @@ impl IntoResult<io::Result<()>> for std::os::raw::c_int {
}

trait CommandExt {
fn status_without_output(&mut self) -> io::Result<std::process::ExitStatus>;
fn without_io(&mut self) -> &mut Self;
fn status(&mut self) -> io::Result<std::process::ExitStatus>;
}

impl CommandExt for Command {
fn status_without_output(&mut self) -> io::Result<std::process::ExitStatus> {
let mut process = self
.stdin(Stdio::null())
fn without_io(&mut self) -> &mut Self {
self.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?;
}

fn status(&mut self) -> io::Result<std::process::ExitStatus> {
let mut process = self.spawn()?;
process.wait()
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use crate::{CommandExt, IntoResult};
pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
Command::new("/usr/bin/open")
.arg(path.as_ref())
.status_without_output()
.without_io()
.status()
.into_result()
}

Expand All @@ -14,6 +15,13 @@ pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()>
.arg(path.as_ref())
.arg("-a")
.arg(app.into())
.status_without_output()
.without_io()
.status()
.into_result()
}

pub fn commands<T: AsRef<OsStr>>(path: T) -> impl Iterator<Item = Command> {
let mut cmd = Command::new("/usr/bin/open");
cmd.arg(path.as_ref());
Some(cmd).into_iter()
}
18 changes: 13 additions & 5 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use crate::{CommandExt, IntoResult};

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
pub fn commands<T: AsRef<OsStr>>(path: T) -> impl Iterator<Item = Command> {
let path = path.as_ref();
let open_handlers = [
("xdg-open", &[path] as &[_]),
Expand All @@ -18,12 +18,19 @@ pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
("wslview", &[&wsl_path(path)]),
];

open_handlers.into_iter().map(|&(cmd, args)| {
let mut cmd = Command::new("/usr/bin/open");
cmd.args(args);
cmd
})
}

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
let mut unsuccessful = None;
let mut io_error = None;

for (command, args) in &open_handlers {
let result = Command::new(command).args(*args).status_without_output();

for cmd in commands(path) {
let result = cmd.without_io().status().into_result();
match result {
Ok(status) if status.success() => return Ok(()),
Ok(status) => {
Expand All @@ -46,7 +53,8 @@ pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Command::new(app.into())
.arg(path.as_ref())
.status_without_output()
.without_io()
.status()
.into_result()
}

Expand Down
19 changes: 13 additions & 6 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
use std::{ffi::OsStr, io};
use std::{ffi::OsStr, io, process::Command};

use crate::{CommandExt, IntoResult};

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
std::process::Command::new("cmd")
Command::new("cmd")
.arg("/c")
.arg("start")
.arg(path.as_ref())
.status_without_output()
.into_result()
.without_io()
.status()
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
std::process::Command::new("cmd")
Command::new("cmd")
.arg("/c")
.arg(app.into())
.arg(path.as_ref())
.status_without_output()
.without_io()
.status()
.into_result()
}

pub fn commands<T: AsRef<OsStr>>(path: T) -> impl Iterator<Item = Command> {
let mut cmd = Command::new("cmd");
cmd.arg("/c").arg("start").arg(path.as_ref());
Some(cmd).into_iter()
}