Skip to content

Commit

Permalink
converted shell command function to async function
Browse files Browse the repository at this point in the history
  • Loading branch information
sakithb committed Sep 6, 2021
1 parent 291188b commit 081ac78
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
],
"url": "https://github.com/cliffniff/media-controls",
"uuid": "[email protected]",
"version": 13
"version": 14
}
29 changes: 17 additions & 12 deletions prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { Gio, Gtk, GObject, GLib } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();

const { Utf8ArrayToStr } = Me.imports.utils;
const { Utf8ArrayToStr, execCommunicate } = Me.imports.utils;

const Config = imports.misc.config;
const [major] = Config.PACKAGE_VERSION.split(".");
Expand Down Expand Up @@ -190,10 +190,12 @@ const signalHandler = {

on_clear_cache_clicked: () => {
let dir = GLib.get_user_config_dir() + "/media-controls";
let [ok, out, err, status] = GLib.spawn_command_line_sync(`rm -r '${dir}'`);
if (ok) {
widgetCacheSize.set_text(getCacheSize());
} else {
try {
(async () => {
await execCommunicate(["rm", "-r", dir]);
widgetCacheSize.set_text(await getCacheSize());
})();
} catch (error) {
widgetCacheSize.set_text("Failed to clear cache");
}
},
Expand Down Expand Up @@ -353,7 +355,9 @@ const initWidgets = () => {
widgetMouseActionLeft.set_active(mouseActionNameIds.indexOf(mouseActions[0]));
widgetMouseActionRight.set_active(mouseActionNameIds.indexOf(mouseActions[1]));

widgetCacheSize.set_text(getCacheSize());
(async () => {
widgetCacheSize.set_text(await getCacheSize());
})();
};

const init = () => {
Expand Down Expand Up @@ -381,12 +385,13 @@ const buildPrefsWidget = () => {
return builder.get_object("main_prefs");
};

const getCacheSize = () => {
const getCacheSize = async () => {
// du -hs ./.config/media-controls | awk '{NF=1}1'
const [ok, out, err, status] = GLib.spawn_command_line_sync(
"/bin/bash -c \"du -hs ./.config/media-controls | awk '{NF=1}1'\""
);
if (ok) {
return Utf8ArrayToStr(out).trim() || "0B";
try {
let dir = GLib.get_user_config_dir() + "/media-controls";
const result = await execCommunicate(["/bin/bash", "-c", `du -hs ${dir} | awk '{NF=1}1'`]);
return result || "0K";
} catch (error) {
logError(error);
}
};
41 changes: 41 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,44 @@ const _getRequest = (url) => {
});
});
};

var execCommunicate = async (argv, input = null, cancellable = null) => {
let cancelId = 0;
let flags = Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE;

if (input !== null) flags |= Gio.SubprocessFlags.STDIN_PIPE;

let proc = new Gio.Subprocess({
argv: argv,
flags: flags,
});
proc.init(cancellable);

if (cancellable instanceof Gio.Cancellable) {
cancelId = cancellable.connect(() => proc.force_exit());
}

return new Promise((resolve, reject) => {
proc.communicate_utf8_async(input, null, (proc, res) => {
try {
let [, stdout, stderr] = proc.communicate_utf8_finish(res);
let status = proc.get_exit_status();

if (status !== 0) {
throw new Gio.IOErrorEnum({
code: Gio.io_error_from_errno(status),
message: stderr ? stderr.trim() : GLib.strerror(status),
});
}

resolve(stdout.trim());
} catch (e) {
reject(e);
} finally {
if (cancelId > 0) {
cancellable.disconnect(cancelId);
}
}
});
});
};

0 comments on commit 081ac78

Please sign in to comment.