Skip to content

Commit

Permalink
fix!: with() on windows to be able to lookup program in registry/…
Browse files Browse the repository at this point in the history
…PATH.

The underlying invocation of `with()` changed slightly on windows to make it more useful
as it can now find application names like `chrome` in the registry, but that change may also
be breaking for some who previously worked around the previous behaviour.

Please let us know if this truly works better, or if more changes are needed to launch something
with a program on Windows.

See #82 for details.
  • Loading branch information
Byron committed Jun 25, 2023
2 parents 659b8a0 + b5528b6 commit ddf4842
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/main.rs
@@ -1,24 +1,23 @@
use std::{env, process};
use std::env;

fn main() {
let path_or_url = match env::args().nth(1) {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = env::args();
let path_or_url = match args.nth(1) {
Some(arg) => arg,
None => {
eprintln!("usage: open <path-or-url>");
process::exit(1);
}
None => return Err("usage: open <path-or-url> [--with|-w program]".into()),
};

let result = match std::env::var("OPEN_WITH").ok() {
Some(program) => open::with(&path_or_url, program),
match args.next() {
Some(arg) if arg == "--with" || arg == "-w" => {
let program = args
.next()
.ok_or("--with must be followed by the program to use for opening")?;
open::with(&path_or_url, program)
}
Some(arg) => return Err(format!("Argument '{arg}' is invalid").into()),
None => open::that(&path_or_url),
};
}?;

match result {
Ok(()) => println!("Opened '{}' successfully.", path_or_url),
Err(err) => {
eprintln!("An error occurred when opening '{}': {}", path_or_url, err);
process::exit(3);
}
}
println!("Opened '{}' successfully.", path_or_url);
Ok(())
}
2 changes: 2 additions & 0 deletions src/windows.rs
Expand Up @@ -20,6 +20,8 @@ pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
pub fn with_command<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> Command {
let mut cmd = Command::new("cmd");
cmd.arg("/c")
.arg("start")
.raw_arg("\"\"")
.raw_arg(app.into())
.raw_arg(wrap_in_quotes(path))
.creation_flags(CREATE_NO_WINDOW);
Expand Down

0 comments on commit ddf4842

Please sign in to comment.