19
19
*/
20
20
21
21
// Imports
22
+ use gtk:: glib:: Bytes ;
22
23
use gtk:: prelude:: * ;
23
24
use gtk:: { gio, glib} ;
24
25
use std:: ffi:: OsStr ;
@@ -40,107 +41,50 @@ use std::ffi::OsStr;
40
41
* Made by:
41
42
* Deren Vural
42
43
*
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:
46
45
*
47
46
*/
48
47
pub fn exec_check (
49
48
argv : & [ & OsStr ] ,
50
49
cancellable : Option < & impl IsA < gio:: Cancellable > > ,
51
50
) -> Result < ( ) , glib:: Error > {
52
51
// Create subprocess
53
- println ! ( "..creating subprocess" ) ; //DEBUG
54
52
match gio:: Subprocess :: newv ( argv, gio:: SubprocessFlags :: NONE ) {
55
53
Err ( err) => Err ( err) ,
56
54
Ok ( proc) => {
57
- println ! ( "..Subprocess successfully created!" ) ; //DEBUG
58
-
59
55
// 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 ( ( ) ) ,
87
58
}
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
- //*/
135
59
}
136
60
}
137
61
}
138
62
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
+ */
139
83
pub fn exec_communicate (
140
84
argv : & [ & OsStr ] ,
141
85
_input : Option < & str > ,
142
86
cancellable : Option < & impl IsA < gio:: Cancellable > > ,
143
- ) -> Result < ( ) , glib:: Error > {
87
+ ) -> Result < ( Option < Bytes > , Option < Bytes > ) , glib:: Error > {
144
88
// Create subprocess
145
89
println ! ( "..creating subprocess" ) ; //DEBUG
146
90
match gio:: Subprocess :: newv ( argv, gio:: SubprocessFlags :: NONE ) {
@@ -151,10 +95,7 @@ pub fn exec_communicate(
151
95
// Run subprocess
152
96
match proc. communicate ( None , cancellable) {
153
97
Err ( err) => Err ( err) ,
154
- Ok ( _out) => {
155
- println ! ( "....process finished" ) ; //DEBUG
156
- Ok ( ( ) )
157
- }
98
+ Ok ( out) => Ok ( out) ,
158
99
}
159
100
}
160
101
}
0 commit comments