Expand description
Use this library to open a given path or URL in some application, obeying the current user’s desktop configuration.
It is expected that http:
and https:
URLs will open in a web browser, although desktop configuration may override this.
The choice of application for opening other path types is highly system-dependent.
§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),
}
§UNIX Desktop
The choice of application for opening a given URL or path on a UNIX desktop is highly dependent on the user’s GUI framework (if any) and its configuration.
open::that()
tries a sequence of opener commands to open the specified path.
The configuration of these openers is dependent on the window system.
On Windows WSL, wslview
is tried first, then xdg-open
. In other UNIX environments, xdg-open
is tried first. If this fails, a sequence of other openers is tried.
Currently the BROWSER
environment variable is ignored even for http:
and https:
URLs unless the opener being used happens to respect it.
It cannot be overemphasized how fragile this all is in UNIX environments. It is common for the various MIME tables to incorrectly specify the application “owning” a given filetype. It is common for openers to behave strangely. Use with caution, as this crate merely inherits a particular platforms shortcomings.
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 openpath
. - 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.