Skip to content

Commit b39e405

Browse files
authored
Merge pull request #5 from derenv/subprocess_communicate
Subprocess communicate
2 parents 0c0dde0 + 0e0d9f3 commit b39e405

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

src/main.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ fn build_ui(app: &Application) {
8383
.build();
8484
// Connect to "clicked" signal of `button`
8585
button2.connect_clicked(move |_| {
86-
println!("Trying to open settings!"); //DEBUG
87-
8886
// Ideally we should grab if nvidia-settings 'failed' somehow or exited normally
8987
match subprocess::exec_communicate(
9088
&[
@@ -93,14 +91,38 @@ fn build_ui(app: &Application) {
9391
OsStr::new("GpuUUID"),
9492
OsStr::new("-t"),
9593
],
96-
None,
9794
None::<&gio::Cancellable>,
9895
) {
99-
Ok(_x) => {
100-
println!("........yay"); //DEBUG
96+
Ok(return_val) => {
97+
match return_val {
98+
(None, None) => println!("no stdout or stderr, something went really wrong..."),
99+
(None, Some(stderr_buffer)) => {
100+
match std::str::from_utf8(&stderr_buffer) {
101+
Ok(stderr_buffer_contents) => println!("Process failed with error: {}", stderr_buffer_contents),
102+
Err(err) => panic!("{}", err),
103+
}
104+
},
105+
(Some(stdout_buffer), None) => {
106+
match std::str::from_utf8(&stdout_buffer) {
107+
Ok(stdout_buffer_contents) => println!("Process suceeded, returning: {}", stdout_buffer_contents),
108+
Err(err) => panic!("{}", err),
109+
}
110+
},
111+
(Some(stdout_buffer), Some(stderr_buffer)) => {
112+
match std::str::from_utf8(&stdout_buffer) {
113+
Ok(stdout_buffer_contents)=> {
114+
match std::str::from_utf8(&stderr_buffer) {
115+
Ok(stderr_buffer_contents) => println!("Process suceeded, returning: {} but with error: {}", stdout_buffer_contents, stderr_buffer_contents),
116+
Err(err) => panic!("{}", err),
117+
}
118+
},
119+
Err(err) => panic!("{}", err),
120+
}
121+
},
122+
}
101123
}
102124
Err(_y) => {
103-
println!("........fak"); //DEBUG
125+
println!("something went wrong!"); //DEBUG
104126
}
105127
};
106128
});
@@ -132,8 +154,8 @@ fn build_ui(app: &Application) {
132154
window.set_show_menubar(true);
133155

134156
// Add children to window
135-
window.set_child(Some(&button1));
136-
//window.set_child(Some(&button2));
157+
//window.set_child(Some(&button1));
158+
window.set_child(Some(&button2));
137159

138160
// Present window
139161
window.show();

src/processor/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl Processor {
5454
}
5555

5656
pub fn get_name(self) -> String {
57-
self.name
57+
todo!()
58+
//self.name
5859
}
5960
}

src/subprocess/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
* https://blog.logrocket.com/a-practical-guide-to-async-in-rust/
1919
*/
2020

21-
// Imports
2221
use gtk::glib::Bytes;
22+
// Imports
2323
use gtk::prelude::*;
2424
use gtk::{gio, glib};
2525
use std::ffi::OsStr;
@@ -82,20 +82,23 @@ pub fn exec_check(
8282
*/
8383
pub fn exec_communicate(
8484
argv: &[&OsStr],
85-
_input: Option<&str>,
8685
cancellable: Option<&impl IsA<gio::Cancellable>>,
8786
) -> Result<(Option<Bytes>, Option<Bytes>), glib::Error> {
8887
// Create subprocess
89-
println!("..creating subprocess"); //DEBUG
90-
match gio::Subprocess::newv(argv, gio::SubprocessFlags::NONE) {
88+
match gio::Subprocess::newv(argv, gio::SubprocessFlags::STDOUT_PIPE) {
9189
Err(err) => Err(err),
9290
Ok(proc) => {
93-
println!("..Subprocess successfully created!"); //DEBUG
94-
9591
// Run subprocess
9692
match proc.communicate(None, cancellable) {
9793
Err(err) => Err(err),
98-
Ok(out) => Ok(out),
94+
Ok(buffers) => match buffers {
95+
(None, None) => Ok((None, None)),
96+
(None, Some(stderr_buffer)) => Ok((None, Some(stderr_buffer))),
97+
(Some(stdout_buffer), None) => Ok((Some(stdout_buffer), None)),
98+
(Some(stdout_buffer), Some(stderr_buffer)) => {
99+
Ok((Some(stdout_buffer), Some(stderr_buffer)))
100+
}
101+
},
99102
}
100103
}
101104
}

0 commit comments

Comments
 (0)