Skip to content

Commit

Permalink
Fix open::with() on Windows. (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuXiaojie committed Jun 25, 2023
1 parent 659b8a0 commit 5607cd6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ fn main() {
}
};

#[cfg(not(windows))]
let result = match std::env::var("OPEN_WITH").ok() {
Some(program) => open::with(&path_or_url, program),
None => open::that(&path_or_url),
};

#[cfg(windows)]
let result = match env::args().nth(2) {
Some(arg) if arg == "--with" || arg == "-w" => match env::args().nth(3) {
Some(program) => open::with(&path_or_url, program),
None => open::that(&path_or_url),
},
_ => open::that(&path_or_url),
};

match result {
Ok(()) => println!("Opened '{}' successfully.", path_or_url),
Err(err) => {
Expand Down
2 changes: 2 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
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 5607cd6

Please sign in to comment.