1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
use std::io::{self, Write};
use std::process;
use std::sync::mpsc;
use std::thread;
use crate::config::AppMimetypeEntry;
use crate::event::AppEvent;
pub fn fork_execute<I, S>(
entry: &AppMimetypeEntry,
paths: I,
event_tx: mpsc::Sender<AppEvent>,
) -> std::io::Result<(u32, thread::JoinHandle<()>)>
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
let program = String::from(entry.get_command());
let mut command = process::Command::new(program);
if entry.get_silent() {
command.stdout(process::Stdio::null());
command.stderr(process::Stdio::null());
}
command.args(entry.get_args());
command.args(paths);
let mut child = command.spawn()?;
let child_id = child.id();
let handle = thread::spawn(move || {
let child_id = child.id();
let _ = child.wait();
let _ = event_tx.send(AppEvent::ChildProcessComplete(child_id));
});
Ok((child_id, handle))
}
pub fn execute_and_wait<I, S>(entry: &AppMimetypeEntry, paths: I) -> std::io::Result<()>
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
let program = String::from(entry.get_command());
let mut command = process::Command::new(program);
if entry.get_silent() {
command.stdout(process::Stdio::null());
command.stderr(process::Stdio::null());
}
command.args(entry.get_args());
command.args(paths);
let _ = command.status()?;
if entry.get_confirm_exit() {
wait_for_enter()?;
}
Ok(())
}
pub fn wait_for_enter() -> io::Result<()> {
print!("===============\nPress ENTER to continue... ");
std::io::stdout().flush()?;
let mut user_input = String::with_capacity(4);
std::io::stdin().read_line(&mut user_input)?;
Ok(())
}
|