mirror of
https://github.com/morgan9e/gnome-shell-extension-freon
synced 2026-04-15 00:44:22 +09:00
Refactor output reading
This commit is contained in:
@@ -213,45 +213,35 @@ function filterVoltage(voltageInfo) {
|
||||
const Future = new Lang.Class({
|
||||
Name: 'Future',
|
||||
|
||||
_init: function(argv, callback) {
|
||||
_init: function(argv, callback) {
|
||||
try{
|
||||
this._callback = callback;
|
||||
let [exit, pid, stdin, stdout, stderr] =
|
||||
let [exit, pid, stdinFd, stdoutFd, stderrFd] =
|
||||
GLib.spawn_async_with_pipes(null, /* cwd */
|
||||
argv, /* args */
|
||||
null, /* env */
|
||||
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
|
||||
null /* child_setup */);
|
||||
this._stdout = new Gio.UnixInputStream({fd: stdout, close_fd: true});
|
||||
this._dataStdout = new Gio.DataInputStream({base_stream: this._stdout});
|
||||
new Gio.UnixOutputStream({fd: stdin, close_fd: true}).close(null);
|
||||
new Gio.UnixInputStream({fd: stderr, close_fd: true}).close(null);
|
||||
let stdout = new Gio.UnixInputStream({fd: stdoutFd, close_fd: true});
|
||||
let outReader = new Gio.DataInputStream({base_stream: stdout});
|
||||
|
||||
this._childWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, Lang.bind(this, function(pid, status, requestObj) {
|
||||
GLib.source_remove(this._childWatch);
|
||||
GLib.close(stdinFd);
|
||||
GLib.close(stderrFd);
|
||||
|
||||
let childWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, Lang.bind(this, function(pid, status, requestObj) {
|
||||
let output = '';
|
||||
let [line, size] = [null, 0];
|
||||
while (([line, size] = outReader.read_line(null)) != null && line != null) {
|
||||
output += line.toString() + '\n';
|
||||
}
|
||||
|
||||
stdout.close(null);
|
||||
GLib.source_remove(childWatch);
|
||||
callback(output);
|
||||
}));
|
||||
|
||||
this._readStdout();
|
||||
} catch(e){
|
||||
global.log(e.toString());
|
||||
}
|
||||
},
|
||||
|
||||
_readStdout: function(){
|
||||
this._dataStdout.fill_async(-1, GLib.PRIORITY_DEFAULT, null, Lang.bind(this, function(stream, result) {
|
||||
if (stream.fill_finish(result) == 0){
|
||||
try{
|
||||
this._callback(stream.peek_buffer().toString());
|
||||
}catch(e){
|
||||
global.log(e.toString());
|
||||
}
|
||||
this._stdout.close(null);
|
||||
return;
|
||||
}
|
||||
|
||||
stream.set_buffer_size(2 * stream.get_buffer_size());
|
||||
this._readStdout();
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user