diff options
author | Aylur <[email protected]> | 2024-05-21 22:08:41 +0200 |
---|---|---|
committer | Aylur <[email protected]> | 2024-05-21 22:08:41 +0200 |
commit | 81c4e3a12d05a3550f1d8c942f0d919b4688c3bc (patch) | |
tree | dd108b8fe2c569ce9208fe9c24bab9ffde768460 /src/cli.vala.in | |
parent | 5929e35962914331704f88dc45e8b96b30d1448b (diff) |
fix proxy, add remaining cli functionality
Diffstat (limited to 'src/cli.vala.in')
-rw-r--r-- | src/cli.vala.in | 88 |
1 files changed, 74 insertions, 14 deletions
diff --git a/src/cli.vala.in b/src/cli.vala.in index 056a69d..f49a056 100644 --- a/src/cli.vala.in +++ b/src/cli.vala.in @@ -1,9 +1,19 @@ -private static bool version; -private static bool help; +static bool help; +static bool version; +static bool daemonize; +static bool list; +static string invoke; +static int close_n; +static int get_n; private const OptionEntry[] options = { { "version", 'v', OptionFlags.NONE, OptionArg.NONE, ref version, null, null }, { "help", 'h', OptionFlags.NONE, OptionArg.NONE, ref help, null, null }, + { "daemon", 'd', OptionFlags.NONE, OptionArg.NONE, ref daemonize, null, null }, + { "list", 'l', OptionFlags.NONE, OptionArg.NONE, ref list, null, null }, + { "invoke", 'i', OptionFlags.NONE, OptionArg.STRING, ref invoke, null, null }, + { "close", 'c', OptionFlags.NONE, OptionArg.INT, ref close_n, null, null }, + { "get", 'g', OptionFlags.NONE, OptionArg.INT, ref get_n, null, null }, { null }, }; @@ -22,26 +32,76 @@ int main(string[] argv) { if (help) { print("Cli client for astal-notifd\n\n"); print("Usage:\n"); - print(" %s [flags] message\n\n", argv[0]); + print(" %s [flags]\n\n", argv[0]); print("Flags:\n"); - print(" -h, --help Print this help and exit\n"); - print(" -v, --version Print version number and exit\n"); + print(" -h, --help Print this help and exit\n"); + print(" -v, --version Print version number and exit\n"); + print(" -l, --list Print every notification and exit\n"); + print(" -d, --daemonize Watch for new notifications\n"); + print(" -i, --invoke Invoke a notification action\n"); + print(" -c, --close Close a notification by its id\n"); + print(" -g, --get Print a notification by its id\n"); return 0; } + var loop = new MainLoop(); + var notifd = new AstalNotifd.Notifd(); + if (version) { print("@VERSION@"); return 0; } - var notifd = new AstalNotifd.Notifd(); - notifd.notified.connect((id) => { - stdout.printf("%s\n", notifd.get_notification_json(id)); - }); - notifd.active.connect(() => { - foreach (var n in notifd.notifications) - stdout.printf("%s\n", n.to_json_string()); - }); - new MainLoop().run(); + if (list) { + var cache = Environment.get_user_cache_dir() + "/astal/notifd/notifications.json"; + if (FileUtils.test(cache, FileTest.EXISTS)) { + try { + uint8[] json; + File.new_for_path(cache).load_contents(null, out json, null); + stdout.printf("%s", (string)json); + } catch (Error err) { + stderr.printf("failed to load cache: %s", err.message); + } + } + return 0; + } + + if (daemonize) { + notifd.notified.connect((id) => { + stdout.printf("%s\n", notifd.get_notification_json(id)); + }); + } + + if (invoke != null) { + if (!invoke.contains(":")) { + stderr.printf("invoke format needs to be <notif-id>:<action-id>"); + return 1; + } + + var split = invoke.split(":"); + var n_id = int.parse(split[0]); + var a_id = split[1]; + + notifd.active.connect(() => { + notifd.get_notification(n_id).invoke(a_id); + loop.quit(); + }); + } + + if (close_n > 0) { + notifd.active.connect(() => { + notifd.get_notification(close_n).dismiss(); + loop.quit(); + }); + } + + if (get_n > 0) { + notifd.active.connect(() => { + stdout.printf("%s", notifd.get_notification(get_n).to_json_string()); + loop.quit(); + }); + } + + loop.run(); return 0; } |