From 3486989e9191af00248efb4a5a973fac64b1d8c1 Mon Sep 17 00:00:00 2001 From: kotontrion Date: Thu, 23 May 2024 17:34:06 +0200 Subject: add cli tool --- src/cli.vala | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/cli.vala (limited to 'src/cli.vala') diff --git a/src/cli.vala b/src/cli.vala new file mode 100644 index 0000000..c22e096 --- /dev/null +++ b/src/cli.vala @@ -0,0 +1,55 @@ +static bool help; +static bool version; +static bool daemonize; + +const OptionEntry[] options = { + { "version", 'v', OptionFlags.NONE, OptionArg.NONE, ref version, "Print version number", null }, + { "daemon", 'd', OptionFlags.NONE, OptionArg.NONE, ref daemonize, "Monitor the systemtray", null }, + { null }, +}; + +int main(string[] argv) { + try { + var opts = new OptionContext(); + opts.add_main_entries(options, null); + opts.set_help_enabled(true); + opts.set_ignore_unknown_options(false); + opts.parse(ref argv); + } catch (OptionError err) { + printerr (err.message); + return 1; + } + + var loop = new MainLoop(); + var tray = new AstalTray.Tray(); + + if (version) { + print(AstalTray.VERSION); + return 0; + } + + if (daemonize) { + tray.item_added.connect((id) => { + + AstalTray.TrayItem item = tray.get_item(id); + + string item_json = item.to_json_string(); + stdout.printf("{\"event\":\"item_added\",\"id\":\"%s\",\"item\":%s}\n", + id, item_json); + stdout.flush(); + + item.changed.connect(() => { + stdout.printf("{\"event\":\"item_changed\",\"id\":\"%s\",\"item\":%s}\n", + id, item_json); + stdout.flush(); + }); + }); + tray.item_removed.connect((id) => { + stdout.printf("{\"event\":\"item_removed\",\"id\":\"%s\"}\n", id); + stdout.flush(); + }); + } + + loop.run(); + return 0; +} -- cgit v1.2.3