Skip to content

Commit 2da976e

Browse files
committed
Refactor linux_and_more module
1 parent f5c5564 commit 2da976e

File tree

1 file changed

+54
-47
lines changed

1 file changed

+54
-47
lines changed

opener/src/linux_and_more.rs

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::OpenError;
22
use std::ffi::OsStr;
3+
use std::io;
34
use std::io::Write;
4-
use std::process::{Command, Stdio};
5+
use std::process::{Child, Command, Stdio};
56

67
const XDG_OPEN_SCRIPT: &[u8] = include_bytes!("xdg-open");
78

@@ -14,66 +15,28 @@ pub(crate) fn open(path: &OsStr) -> Result<(), OpenError> {
1415
}
1516

1617
fn wsl_open(path: &OsStr) -> Result<(), OpenError> {
17-
let transformed_path = crate::wsl_to_windows_path(path);
18-
let transformed_path = transformed_path.as_deref();
19-
let path = match transformed_path {
20-
None => path,
21-
Some(x) => x,
22-
};
23-
let wslview = Command::new("wslview")
24-
.arg(path)
25-
.stdin(Stdio::null())
26-
.stdout(Stdio::null())
27-
.stderr(Stdio::piped())
28-
.spawn();
29-
30-
if let Ok(mut child) = wslview {
18+
let result = open_with_wslview(path);
19+
if let Ok(mut child) = result {
3120
return crate::wait_child(&mut child, "wslview".into());
3221
}
3322

34-
let mut system_xdg_open = Command::new("xdg-open")
35-
.arg(path)
36-
.stdin(Stdio::null())
37-
.stdout(Stdio::null())
38-
.stderr(Stdio::piped())
39-
.spawn()
40-
.map_err(OpenError::Io)?;
41-
23+
let mut system_xdg_open = open_with_system_xdg_open(path).map_err(OpenError::Io)?;
4224
crate::wait_child(&mut system_xdg_open, "xdg-open (system)".into())
4325
}
4426

4527
fn non_wsl_open(path: &OsStr) -> Result<(), OpenError> {
46-
let system_xdg_open = Command::new("xdg-open")
47-
.arg(path)
48-
.stdin(Stdio::null())
49-
.stdout(Stdio::null())
50-
.stderr(Stdio::piped())
51-
.spawn();
28+
let system_xdg_open = open_with_system_xdg_open(path);
5229

5330
let system_xdg_open_used;
54-
let mut xdg_open = match system_xdg_open {
31+
let mut xdg_open;
32+
match system_xdg_open {
5533
Ok(child) => {
5634
system_xdg_open_used = true;
57-
child
35+
xdg_open = child;
5836
}
5937
Err(_) => {
6038
system_xdg_open_used = false;
61-
let mut sh = Command::new("sh")
62-
.arg("-s")
63-
.arg(path)
64-
.stdin(Stdio::piped())
65-
.stdout(Stdio::null())
66-
.stderr(Stdio::piped())
67-
.spawn()
68-
.map_err(OpenError::Io)?;
69-
70-
sh.stdin
71-
.as_mut()
72-
.unwrap()
73-
.write_all(XDG_OPEN_SCRIPT)
74-
.map_err(OpenError::Io)?;
75-
76-
sh
39+
xdg_open = open_with_internal_xdg_open(path)?;
7740
}
7841
};
7942

@@ -85,3 +48,47 @@ fn non_wsl_open(path: &OsStr) -> Result<(), OpenError> {
8548

8649
crate::wait_child(&mut xdg_open, cmd_name.into())
8750
}
51+
52+
fn open_with_wslview(path: &OsStr) -> io::Result<Child> {
53+
let converted_path = crate::wsl_to_windows_path(path);
54+
let converted_path = converted_path.as_deref();
55+
let path = match converted_path {
56+
None => path,
57+
Some(x) => x,
58+
};
59+
60+
Command::new("wslview")
61+
.arg(path)
62+
.stdin(Stdio::null())
63+
.stdout(Stdio::null())
64+
.stderr(Stdio::piped())
65+
.spawn()
66+
}
67+
68+
fn open_with_system_xdg_open(path: &OsStr) -> io::Result<Child> {
69+
Command::new("xdg-open")
70+
.arg(path)
71+
.stdin(Stdio::null())
72+
.stdout(Stdio::null())
73+
.stderr(Stdio::piped())
74+
.spawn()
75+
}
76+
77+
fn open_with_internal_xdg_open(path: &OsStr) -> Result<Child, OpenError> {
78+
let mut sh = Command::new("sh")
79+
.arg("-s")
80+
.arg(path)
81+
.stdin(Stdio::piped())
82+
.stdout(Stdio::null())
83+
.stderr(Stdio::piped())
84+
.spawn()
85+
.map_err(OpenError::Io)?;
86+
87+
sh.stdin
88+
.as_mut()
89+
.unwrap()
90+
.write_all(XDG_OPEN_SCRIPT)
91+
.map_err(OpenError::Io)?;
92+
93+
Ok(sh)
94+
}

0 commit comments

Comments
 (0)