forked from MichalW/gnome-bluetooth-battery-indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
59 lines (48 loc) · 1.73 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
function spawn(command, callback) {
let [status, pid] = GLib.spawn_async(
null,
['/usr/bin/env', 'bash', '-c', command],
null,
GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null
);
if (callback)
GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, callback);
}
function addSignalsHelperMethods(prototype) {
prototype._connectSignal = function (subject, signal_name, method) {
if (!this._signals) this._signals = [];
let signal_id = subject.connect(signal_name, method);
this._signals.push({
subject: subject,
signal_id: signal_id
});
}
prototype._disconnectSignals = function () {
if (!this._signals) return;
this._signals.forEach((signal) => {
signal.subject.disconnect(signal.signal_id);
});
this._signals = [];
};
}
function getPythonExec() {
//return ['python', 'python3', 'python2'].find(cmd => GLib.find_program_in_path(cmd));
return ['python3'].find(cmd => GLib.find_program_in_path(cmd)); //Hotfix for no percentage shown
}
function runPythonScript(argv, onSuccess) {
try {
const proc = Gio.Subprocess.new(argv, Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE);
proc.communicate_utf8_async(null, null, (proc, res) => {
const [, stdout] = proc.communicate_utf8_finish(res);
if (proc.get_successful() && stdout) {
log('[bluetooth-battery-indicator] Percentage from script: ' + stdout);
onSuccess(stdout);
}
});
} catch (e) {
log('ERROR: Python execution failed: ' + e);
}
}