Skip to content

Commit

Permalink
feat: with_command() to obtain the command that opens a program wit…
Browse files Browse the repository at this point in the history
…h a given application.
  • Loading branch information
Byron committed Mar 6, 2023
1 parent c2e0eb2 commit 66e0d7c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 40 deletions.
11 changes: 4 additions & 7 deletions src/haiku.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use std::{ffi::OsStr, io, process::Command};

use crate::{CommandExt, IntoResult};
use std::{ffi::OsStr, process::Command};

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

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
pub fn with_command<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> Command {
let mut cmd = Command::new(app.into());
cmd.arg(path.as_ref())
.status_without_output()
.into_result(&cmd)
cmd.arg(path.as_ref());
cmd
}
11 changes: 4 additions & 7 deletions src/ios.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use std::{ffi::OsStr, io, process::Command};

use crate::{CommandExt, IntoResult};
use std::{ffi::OsStr, process::Command};

pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
let mut cmd = Command::new("uiopen");
cmd.arg("--url").arg(path.as_ref());
vec![cmd]
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
pub fn with_command<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> Command {
let mut cmd = Command::new("uiopen");
cmd.arg("--url")
.arg(path.as_ref())
.arg("--bundleid")
.arg(app.into())
.status_without_output()
.into_result(&cmd)
.arg(app.into());
cmd
}
18 changes: 17 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
/// A [`std::io::Error`] is returned on failure. Because different operating systems
/// handle errors differently it is recommend to not match on a certain error.
pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
os::with(path, app)
let mut cmd = with_command(path, app);
cmd.status_without_output().into_result(&cmd)
}

/// Get multiple commands that open `path` with the default application.
Expand All @@ -168,6 +169,21 @@ pub fn commands<'a, T: AsRef<OsStr>>(path: T) -> Vec<Command> {
os::commands(path)
}

/// Get a command that uses `app` to open `path`.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let path = "http://rust-lang.org";
/// assert!(open::with_command(path, "app").status()?.success());
/// # Ok(())
/// # }
/// ```
pub fn with_command<'a, T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> Command {
os::with_command(path, app)
}

/// Open path with the default application in a new thread to assure it's non-blocking.
///
/// See documentation of [`that()`] for more details.
Expand Down
13 changes: 4 additions & 9 deletions src/macos.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use std::{ffi::OsStr, io, process::Command};

use crate::{CommandExt, IntoResult};
use std::{ffi::OsStr, process::Command};

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

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
pub fn with_command<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> Command {
let mut cmd = Command::new("/usr/bin/open");
cmd.arg(path.as_ref())
.arg("-a")
.arg(app.into())
.status_without_output()
.into_result(&cmd)
cmd.arg(path.as_ref()).arg("-a").arg(app.into());
cmd
}
10 changes: 3 additions & 7 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::{
env,
ffi::{OsStr, OsString},
io,
path::{Path, PathBuf},
process::Command,
};

use crate::{CommandExt, IntoResult};

pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
let path = path.as_ref();
[
Expand All @@ -26,11 +23,10 @@ pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
.collect()
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
pub fn with_command<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> Command {
let mut cmd = Command::new(app.into());
cmd.arg(path.as_ref())
.status_without_output()
.into_result(&cmd)
cmd.arg(path.as_ref());
cmd
}

// Polyfill to workaround absolute path bug in wslu(wslview). In versions before
Expand Down
13 changes: 4 additions & 9 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use std::{ffi::OsStr, io, process::Command};

use crate::{CommandExt, IntoResult};
use std::{ffi::OsStr, process::Command};

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

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
pub fn with_command<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> Command {
let mut cmd = Command::new("cmd");
cmd.arg("/c")
.arg(app.into())
.arg(path.as_ref())
.status_without_output()
.into_result(&cmd)
cmd.arg("/c").arg(app.into()).arg(path.as_ref());
cmd
}

0 comments on commit 66e0d7c

Please sign in to comment.