-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #407 from corecoding/develop
Develop
- Loading branch information
Showing
10 changed files
with
888 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import GLib from 'gi://GLib'; | ||
import Gio from 'gi://Gio'; | ||
const ByteArray = imports.byteArray; | ||
|
||
var Decoder; | ||
try { | ||
Decoder = new TextDecoder('utf-8'); | ||
} catch(error) {} | ||
|
||
// convert Uint8Array into a literal string | ||
function convertUint8ArrayToString(contents) { | ||
// Starting with Gnome 41, we use TextDecoder as ByteArray is deprecated | ||
if (Decoder) | ||
return Decoder.decode(contents).trim(); | ||
|
||
// Supports ByteArray on Gnome 40 | ||
// fixes #304, replaces invalid character | ||
contents[contents.indexOf(208)] = 0; | ||
return ByteArray.toString(contents).trim(); | ||
} | ||
|
||
export function SubProcess(command) { | ||
this.sub_process = Gio.Subprocess.new(command, Gio.SubprocessFlags.STDOUT_PIPE); | ||
this.stdout = this.sub_process.get_stdout_pipe(); | ||
} | ||
|
||
SubProcess.prototype.read = function(delimiter = '') { | ||
return new Promise((resolve, reject) => { | ||
this.stdout.read_bytes_async(512, GLib.PRIORITY_LOW, null, function(stdout, res) { | ||
try { | ||
let read_bytes = stdout.read_bytes_finish(res).get_data(); | ||
|
||
// convert contents to string | ||
let read_str = convertUint8ArrayToString(read_bytes); | ||
|
||
// split read_str by delimiter if passed in | ||
if (delimiter) { | ||
if (read_str == '') | ||
read_str = []; // EOF, ''.split(delimiter) would return [''] | ||
else | ||
read_str = read_str.split(delimiter); | ||
} | ||
|
||
// return results | ||
resolve(read_str); | ||
} catch (e) { | ||
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.PENDING)) { | ||
// previous read attempt is still waiting for something from stdout | ||
// ignore second attempt, return empty data (like EOF) | ||
if (delimiter) resolve([]); | ||
else resolve(''); | ||
} else { | ||
reject(e.message); | ||
} | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
SubProcess.prototype.terminate = function() { | ||
const SIGINT = 2; | ||
this.sub_process.send_signal(SIGINT); | ||
this.sub_process = null; | ||
this.stdout.close_async(GLib.PRIORITY_LOW, null, null); | ||
this.stdout = null; | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,5 @@ | |
], | ||
"url": "https://github.com/corecoding/Vitals", | ||
"uuid": "[email protected]", | ||
"version": 63 | ||
"version": 64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.