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 2 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
11 changes: 7 additions & 4 deletions src/haiku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use std::{ffi::OsStr, io, process::Command};

use crate::{CommandExt, IntoResult};

pub fn command<T: AsRef<OsStr>>(path: T) -> Command {
let mut cmd = Command::new("/bin/open");
cmd.arg(path.as_ref());
cmd
}

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
Command::new("/bin/open")
.arg(path.as_ref())
.status_without_output()
.into_result()
command(path).status_without_output().into_result()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this CommandExt method should be split into two, so the configuration of IO channels is separate from executing it.

Thus, future invocations would look more like this:

Command::new().without_io().status().into_result()

}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Expand Down
12 changes: 7 additions & 5 deletions src/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use std::{ffi::OsStr, io, process::Command};

use crate::{CommandExt, IntoResult};

pub fn command<T: AsRef<OsStr>>(path: T) -> Command {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature here could be

Suggested change
pub fn command<T: AsRef<OsStr>>(path: T) -> Command {
pub fn commands<T: AsRef<OsStr>>(path: T) -> impl Iterator<Item = Command> {

Note that Option implements Iterator, so single-commands can be wrapped in an option when returning them.

let mut cmd = Command::new("uiopen");
cmd.arg("--url").arg(path.as_ref());
cmd
}

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
Command::new("uiopen")
.arg("--url")
.arg(path.as_ref())
.status_without_output()
.into_result()
command(path).status_without_output().into_result()
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Expand Down
18 changes: 18 additions & 0 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 the command without running it.
//!
//! ```no_run
//! let cmd = open::command("http://rust-lang.org");
//! ```
//!
//! # Notes
//!
//! ## Nonblocking operation
Expand Down Expand Up @@ -131,6 +137,18 @@ pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()>
os::with(path, app)
}

/// Get command that opens path with the default application.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be mentioned that this is a 'no-run' sibling of open::that(…), as opposed to, say, open::with(…).

///
/// # Examples
///
/// ```no_run
/// let path = "http://rust-lang.org";
/// let cmd = open::command(path);
/// ```
pub fn command<'a, T: AsRef<OsStr>>(path: T) -> Command {
os::command(path)
}

/// Open path with the default application in a new thread.
///
/// See documentation of [`that()`] for more details.
Expand Down
11 changes: 7 additions & 4 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use std::{ffi::OsStr, io, process::Command};

use crate::{CommandExt, IntoResult};

pub fn command<T: AsRef<OsStr>>(path: T) -> Command {
let mut cmd = Command::new("/usr/bin/open");
cmd.arg(path.as_ref());
cmd
}

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
Command::new("/usr/bin/open")
.arg(path.as_ref())
.status_without_output()
.into_result()
command(path).status_without_output().into_result()
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Expand Down
38 changes: 18 additions & 20 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 command<T: AsRef<OsStr>>(path: T) -> Command {
let path = path.as_ref();
let open_handlers = [
("xdg-open", &[path] as &[_]),
Expand All @@ -18,29 +18,27 @@ pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
("wslview", &[&wsl_path(path)]),
];

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();
let result = Command::new(command).status_without_output();

match result {
Ok(status) if status.success() => return Ok(()),
Ok(status) => {
unsuccessful = unsuccessful.or_else(|| {
Some(std::io::Error::new(
std::io::ErrorKind::Other,
status.to_string(),
))
})
}
Err(err) => io_error = io_error.or(Some(err)),
}
if let Ok(status) = result {
if status.success() {
let mut cmd = Command::new(command);
cmd.args(*args);
return cmd;
};
};
}

Err(unsuccessful
.or(io_error)
.expect("successful cases don't get here"))
// fallback to xdg-open
let (command, args) = &open_handlers[0];
let mut cmd = Command::new(command);
cmd.args(*args);
cmd
}

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
command(path).status_without_output().into_result()
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
Expand Down
17 changes: 9 additions & 8 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use std::{ffi::OsStr, io};
use std::{ffi::OsStr, io, process::Command};

use crate::{CommandExt, IntoResult};

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

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

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())
Expand Down