Skip to content

Commit

Permalink
feat(windows): use std Command
Browse files Browse the repository at this point in the history
Use std `Command` instead of `ShellExecuteW` from windows sys crate.

This change was already attempted in: Byron#25
and later reverted in: Byron#27
and it it seems that it didn't work due to incorrect usage of
`explorer` instead of `cmd /c start`.
(see helix-editor/helix#5820 (comment)
for detailed explanation).

Related: helix-editor/helix#5820
  • Loading branch information
matoous committed Feb 5, 2023
1 parent eea4f27 commit 056cb00
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ test = false
doc = false
name = "open"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.42", features = ["Win32_UI_Shell", "Win32_Foundation", "Win32_UI_WindowsAndMessaging"] }

[target.'cfg(all(unix, not(macos)))'.dependencies]
pathdiff = "0.2.0"
41 changes: 12 additions & 29 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::{
};

use std::os::raw::c_int;
use windows_sys::Win32::UI::Shell::ShellExecuteW;
use windows_sys::Win32::UI::WindowsAndMessaging::SW_SHOW;

use crate::IntoResult;

Expand All @@ -32,35 +30,20 @@ fn convert_path(path: &OsStr) -> io::Result<Vec<u16>> {

pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {
let path = convert_path(path.as_ref())?;
let operation: Vec<u16> = OsStr::new("open\0").encode_wide().collect();
let result = unsafe {
ShellExecuteW(
0,
operation.as_ptr(),
path.as_ptr(),
ptr::null(),
ptr::null(),
SW_SHOW,
)
};
(result as c_int).into_result()
Command::new("cmd")
.arg("/c")
.arg("start")
.arg(path.as_ref())
.status_without_output()
.into_result()
}

pub fn with<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> io::Result<()> {
let path = convert_path(path.as_ref())?;
let operation: Vec<u16> = OsStr::new("open\0").encode_wide().collect();
let app_name: Vec<u16> = OsStr::new(&format!("{}\0", app.into()))
.encode_wide()
.collect();
let result = unsafe {
ShellExecuteW(
0,
operation.as_ptr(),
app_name.as_ptr(),
path.as_ptr(),
ptr::null(),
SW_SHOW,
)
};
(result as c_int).into_result()
Command::new("cmd")
.arg("/c")
.arg(app.into())
.arg(path.as_ref())
.status_without_output()
.into_result()
}

0 comments on commit 056cb00

Please sign in to comment.