Skip to content

Commit cdcac43

Browse files
authored
Merge pull request #1830 from LPGhatguy/use-opener
Migrate browser opening logic to match Cargo
2 parents fd5f1c2 + e661fc6 commit cdcac43

File tree

5 files changed

+13
-73
lines changed

5 files changed

+13
-73
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ git-testament = "0.1.4"
3131
lazy_static = "1"
3232
libc = "0.2"
3333
markdown = "0.2"
34+
opener = "0.4.0"
3435
# Used by `curl` or `reqwest` backend although it isn't imported
3536
# by our rustup.
3637
openssl = { version = "0.10", optional = true }

src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ error_chain! {
2525
foreign_links {
2626
Temp(temp::Error);
2727
Io(io::Error);
28+
Open(opener::OpenError);
2829
}
2930

3031
errors {

src/utils/raw.rs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -353,74 +353,6 @@ pub fn find_cmd<'a>(cmds: &[&'a str]) -> Option<&'a str> {
353353
cmds.iter().cloned().find(|&s| has_cmd(s))
354354
}
355355

356-
pub fn open_browser(path: &Path) -> io::Result<bool> {
357-
#[cfg(not(windows))]
358-
fn inner(path: &Path) -> io::Result<bool> {
359-
use std::process::Stdio;
360-
361-
let env_browser = env::var_os("BROWSER").map(|b| env::split_paths(&b).collect::<Vec<_>>());
362-
let env_commands: Vec<&str> = env_browser
363-
.as_ref()
364-
.map(|cmds| cmds.iter().by_ref().filter_map(|b| b.to_str()).collect())
365-
.unwrap_or_default();
366-
367-
let commands = [
368-
"xdg-open",
369-
"open",
370-
"firefox",
371-
"chromium",
372-
"sensible-browser",
373-
];
374-
if let Some(cmd) = find_cmd(&env_commands).or_else(|| find_cmd(&commands)) {
375-
Command::new(cmd)
376-
.arg(path)
377-
.stdin(Stdio::null())
378-
.stdout(Stdio::null())
379-
.stderr(Stdio::null())
380-
.spawn()
381-
.map(|_| true)
382-
} else {
383-
Ok(false)
384-
}
385-
}
386-
#[cfg(windows)]
387-
fn inner(path: &Path) -> io::Result<bool> {
388-
use std::ptr;
389-
use winapi::ctypes;
390-
use winapi::shared::minwindef::HINSTANCE;
391-
use winapi::shared::ntdef::LPCWSTR;
392-
use winapi::shared::windef::HWND;
393-
394-
// FIXME: When winapi has this function, use their version
395-
extern "system" {
396-
pub fn ShellExecuteW(
397-
hwnd: HWND,
398-
lpOperation: LPCWSTR,
399-
lpFile: LPCWSTR,
400-
lpParameters: LPCWSTR,
401-
lpDirectory: LPCWSTR,
402-
nShowCmd: ctypes::c_int,
403-
) -> HINSTANCE;
404-
}
405-
const SW_SHOW: ctypes::c_int = 5;
406-
407-
let path = windows::to_u16s(path)?;
408-
let operation = windows::to_u16s("open")?;
409-
let result = unsafe {
410-
ShellExecuteW(
411-
ptr::null_mut(),
412-
operation.as_ptr(),
413-
path.as_ptr(),
414-
ptr::null(),
415-
ptr::null(),
416-
SW_SHOW,
417-
)
418-
};
419-
Ok(result as usize > 32)
420-
}
421-
inner(path)
422-
}
423-
424356
#[cfg(windows)]
425357
pub mod windows {
426358
use std::ffi::OsStr;

src/utils/utils.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,7 @@ pub fn read_dir(name: &'static str, path: &Path) -> Result<fs::ReadDir> {
355355
}
356356

357357
pub fn open_browser(path: &Path) -> Result<()> {
358-
match raw::open_browser(path) {
359-
Ok(true) => Ok(()),
360-
Ok(false) => Err("no browser installed".into()),
361-
Err(e) => Err(e).chain_err(|| "could not open browser"),
362-
}
358+
opener::open(path).chain_err(|| "couldn't open browser")
363359
}
364360

365361
pub fn set_permissions(path: &Path, perms: fs::Permissions) -> Result<()> {

0 commit comments

Comments
 (0)