Crate open

Source
Expand description

Use this library to open a path or URL using the program configured on the system in a non-blocking fashion.

§Usage

Open the given URL in the default web browser, without blocking.

open::that("http://rust-lang.org")?;

Alternatively, specify the program to be used to open the path or URL.

open::with("http://rust-lang.org", "firefox")?;

Or obtain the commands to launch a file or path without running them.

let cmds = open::commands("http://rust-lang.org")[0].status()?;

It’s also possible to obtain a command that can be used to open a path in an application.

let status = open::with_command("http://rust-lang.org", "firefox").status()?;

§Notes

§Nonblocking operation

The functions provided are nonblocking as it will return even though the launched child process is still running. Note that depending on the operating system, spawning launch helpers, which this library does under the hood, might still take 100’s of milliseconds.

Beware that on some platforms and circumstances, the launcher may block. In this case, please use the commands() or with_command() accordingly to spawn() it without blocking.

§Error handling

As an operating system program is used, the open operation can fail. Therefore, you are advised to check the result and behave accordingly, e.g. by letting the user know that the open operation failed.

let path = "http://rust-lang.org";

match open::that(path) {
    Ok(()) => println!("Opened '{}' successfully.", path),
    Err(err) => eprintln!("An error occurred when opening '{}': {}", path, err),
}

Functions§

  • Get multiple commands that open path with the default application.
  • Open path with the default application without blocking.
  • Open path with the default application using a detached process. which is useful if the program ends up to be blocking or want to out-live your app
  • Open path with the default application in a new thread to assure it’s non-blocking.
  • Open path with the given application.
  • Get a command that uses app to open path.
  • Open path with the given application using a detached process, which is useful if the program ends up to be blocking or want to out-live your app. Otherwise, prefer with() for straightforward error handling.
  • Open path with the given application in a new thread, which is useful if the program ends up to be blocking. Otherwise, prefer with() for straightforward error handling.