Skip to content

Commit 90a0602

Browse files
author
Deren Vural
committed
finished async subprocess call (for fire and forget application calls)
Signed-off-by: Deren Vural <[email protected]>
1 parent 34e6a2d commit 90a0602

File tree

1 file changed

+26
-85
lines changed

1 file changed

+26
-85
lines changed

src/subprocess/mod.rs

Lines changed: 26 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
// Imports
22+
use gtk::glib::Bytes;
2223
use gtk::prelude::*;
2324
use gtk::{gio, glib};
2425
use std::ffi::OsStr;
@@ -40,107 +41,50 @@ use std::ffi::OsStr;
4041
* Made by:
4142
* Deren Vural
4243
*
43-
* @param {string[]} argv - a list of string arguments
44-
* @param {Gio.Cancellable} [cancellable] - optional cancellable object
45-
* @returns {Promise<boolean>} - The process success
44+
* Notes:
4645
*
4746
*/
4847
pub fn exec_check(
4948
argv: &[&OsStr],
5049
cancellable: Option<&impl IsA<gio::Cancellable>>,
5150
) -> Result<(), glib::Error> {
5251
// Create subprocess
53-
println!("..creating subprocess"); //DEBUG
5452
match gio::Subprocess::newv(argv, gio::SubprocessFlags::NONE) {
5553
Err(err) => Err(err),
5654
Ok(proc) => {
57-
println!("..Subprocess successfully created!"); //DEBUG
58-
5955
// Run subprocess
60-
61-
// Define callback
62-
//This should be called when the process is done
63-
/*
64-
let callback = |q: Result<(), glib::Error>| {
65-
match q {
66-
Err(err) => {
67-
Err(err)
68-
},
69-
Ok(_out) => {
70-
println!("....process finished");//DEBUG
71-
Ok(())
72-
},
73-
}
74-
};
75-
76-
struct Listener {
77-
done: bool
78-
}
79-
impl Listener {
80-
pub fn on_call(&mut self, done: bool) { self.done = done }
81-
}
82-
struct Caller<'callback> {
83-
callback: Box<dyn FnMut(bool) + 'callback>,
84-
}
85-
impl Caller<'_> {
86-
pub fn call(&mut self) { (self.callback)(true) }
56+
match proc.wait_async(cancellable, |_| ()) {
57+
_ => Ok(()),
8758
}
88-
let mut listener = Listener { done: false };
89-
let mut caller = Caller { callback: Box::new(|x| listener.on_call(x)) };
90-
91-
//fn callback_fn(x: bool){println!("callback bitch!! {}", x)}
92-
//let callback_box = Box::new(|x: bool| callback_fn(x));
93-
94-
//proc.wait_future();//not sure what this does tbh
95-
*/
96-
97-
/*
98-
Do i just not use Results with callback versions?
99-
*/
100-
// This doesn't work
101-
/*
102-
match proc.wait_async(cancellable,None) {
103-
Err(err) => {
104-
Err(err)
105-
},
106-
Ok(_out) => {
107-
println!("....process finished");//DEBUG
108-
Ok(())
109-
},
110-
}
111-
*/
112-
// This also doesn't work
113-
/*
114-
match proc.wait_async(cancellable, callback) {
115-
Err(err) => {
116-
Err(err)
117-
},
118-
Ok(_out) => {
119-
println!("....process finished");//DEBUG
120-
Ok(())
121-
},
122-
}
123-
*/
124-
125-
//*
126-
// This works but holds up main thread..
127-
match proc.wait(cancellable) {
128-
Err(err) => Err(err),
129-
Ok(_out) => {
130-
println!("....process finished"); //DEBUG
131-
Ok(())
132-
}
133-
}
134-
//*/
13559
}
13660
}
13761
}
13862

63+
/*
64+
* Name:
65+
* exec_communicate
66+
*
67+
* Description:
68+
* Execute a command and return any output
69+
*
70+
* If given, @cancellable can be used to stop the process before it finishes.
71+
*
72+
* https://gtk-rs.org/gtk-rs-core/stable/0.14/docs/src/gio/auto/subprocess.rs.html
73+
*
74+
* Made:
75+
* 15/09/2022
76+
*
77+
* Made by:
78+
* Deren Vural
79+
*
80+
* Notes:
81+
*
82+
*/
13983
pub fn exec_communicate(
14084
argv: &[&OsStr],
14185
_input: Option<&str>,
14286
cancellable: Option<&impl IsA<gio::Cancellable>>,
143-
) -> Result<(), glib::Error> {
87+
) -> Result<(Option<Bytes>, Option<Bytes>), glib::Error> {
14488
// Create subprocess
14589
println!("..creating subprocess"); //DEBUG
14690
match gio::Subprocess::newv(argv, gio::SubprocessFlags::NONE) {
@@ -151,10 +95,7 @@ pub fn exec_communicate(
15195
// Run subprocess
15296
match proc.communicate(None, cancellable) {
15397
Err(err) => Err(err),
154-
Ok(_out) => {
155-
println!("....process finished"); //DEBUG
156-
Ok(())
157-
}
98+
Ok(out) => Ok(out),
15899
}
159100
}
160101
}

0 commit comments

Comments
 (0)