diff options
author | Aylur <[email protected]> | 2024-10-27 22:12:35 +0000 |
---|---|---|
committer | Aylur <[email protected]> | 2024-10-27 22:12:35 +0000 |
commit | 439571abec531d9b50d22f647335112645e43d86 (patch) | |
tree | 0ada2ff7fc3b2f24ee532b83a2bcaa56e38bb71c /lib | |
parent | 197bffbf05d62aeefceb58011b0f031cd8836ca6 (diff) |
cli: better err logs
Diffstat (limited to 'lib')
-rw-r--r-- | lib/astal/io/application.vala | 69 | ||||
-rw-r--r-- | lib/astal/io/cli.vala | 53 | ||||
-rw-r--r-- | lib/mpris/player.vala | 6 |
3 files changed, 61 insertions, 67 deletions
diff --git a/lib/astal/io/application.vala b/lib/astal/io/application.vala index c7bd311..09b61b5 100644 --- a/lib/astal/io/application.vala +++ b/lib/astal/io/application.vala @@ -103,75 +103,58 @@ public static List<string> get_instances() { * Quit an an Astal instances. * It is the equivalent of `astal --quit -i instance`. */ -public static void quit_instance(string instance) { - try { - IApplication proxy = Bus.get_proxy_sync( - BusType.SESSION, - "io.Astal." + instance, - "/io/Astal/Application" - ); +public static void quit_instance(string instance) throws Error { + IApplication proxy = Bus.get_proxy_sync( + BusType.SESSION, + "io.Astal." + instance, + "/io/Astal/Application" + ); - proxy.quit(); - } catch (Error err) { - critical(err.message); - } + proxy.quit(); } /** * Open the Gtk debug tool of an an Astal instances. * It is the equivalent of `astal --inspector -i instance`. */ -public static void open_inspector(string instance) { - try { - IApplication proxy = Bus.get_proxy_sync( - BusType.SESSION, - "io.Astal." + instance, - "/io/Astal/Application" - ); +public static void open_inspector(string instance) throws Error { + IApplication proxy = Bus.get_proxy_sync( + BusType.SESSION, + "io.Astal." + instance, + "/io/Astal/Application" + ); - proxy.inspector(); - } catch (Error err) { - critical(err.message); - } + proxy.inspector(); } /** * Toggle a Window of an Astal instances. * It is the equivalent of `astal -i instance --toggle window`. */ -public static void toggle_window_by_name(string instance, string window) { - try { - IApplication proxy = Bus.get_proxy_sync( - BusType.SESSION, - "io.Astal." + instance, - "/io/Astal/Application" - ); +public static void toggle_window_by_name(string instance, string window) throws Error { + IApplication proxy = Bus.get_proxy_sync( + BusType.SESSION, + "io.Astal." + instance, + "/io/Astal/Application" + ); - proxy.toggle_window(window); - } catch (Error err) { - critical(err.message); - } + proxy.toggle_window(window); } /** * Send a message to an Astal instances. * It is the equivalent of `astal -i instance content of the message`. */ -public static string send_message(string instance, string msg) { +public static string send_message(string instance, string msg) throws Error { var rundir = Environment.get_user_runtime_dir(); var socket_path = @"$rundir/astal/$instance.sock"; var client = new SocketClient(); - try { - var conn = client.connect(new UnixSocketAddress(socket_path), null); - conn.output_stream.write(msg.concat("\x04").data); + var conn = client.connect(new UnixSocketAddress(socket_path), null); + conn.output_stream.write(msg.concat("\x04").data); - var stream = new DataInputStream(conn.input_stream); - return stream.read_upto("\x04", -1, null, null); - } catch (Error err) { - printerr(err.message); - return ""; - } + var stream = new DataInputStream(conn.input_stream); + return stream.read_upto("\x04", -1, null, null); } /** diff --git a/lib/astal/io/cli.vala b/lib/astal/io/cli.vala index 8fc0523..f69cf0b 100644 --- a/lib/astal/io/cli.vala +++ b/lib/astal/io/cli.vala @@ -11,13 +11,19 @@ const OptionEntry[] options = { { "help", 'h', OptionFlags.NONE, OptionArg.NONE, ref help, null, null }, { "list", 'l', OptionFlags.NONE, OptionArg.NONE, ref list, null, null }, { "quit", 'q', OptionFlags.NONE, OptionArg.NONE, ref quit, null, null }, - { "quit", 'q', OptionFlags.NONE, OptionArg.NONE, ref quit, null, null }, { "inspector", 'I', OptionFlags.NONE, OptionArg.NONE, ref inspector, null, null }, { "toggle-window", 't', OptionFlags.NONE, OptionArg.STRING, ref toggle_window, null, null }, { "instance", 'i', OptionFlags.NONE, OptionArg.STRING, ref instance_name, null, null }, { null }, }; +int err(string msg) { + var red = "\x1b[31m"; + var r = "\x1b[0m"; + printerr(@"$(red)error: $(r)$msg"); + return 1; +} + int main(string[] argv) { try { var opts = new OptionContext(); @@ -25,9 +31,8 @@ int main(string[] argv) { opts.set_help_enabled(false); opts.set_ignore_unknown_options(false); opts.parse(ref argv); - } catch (OptionError err) { - printerr (err.message); - return 1; + } catch (OptionError e) { + return err(e.message); } if (help) { @@ -55,24 +60,30 @@ int main(string[] argv) { if (list) { foreach (var name in AstalIO.get_instances()) - stdout.printf("%s\n", name); + print(@"$name\n"); return 0; } - if (quit) { - AstalIO.quit_instance(instance_name); - return 0; - } + try { + if (quit) { + AstalIO.quit_instance(instance_name); + return 0; + } - if (inspector) { - AstalIO.open_inspector(instance_name); - return 0; - } + if (inspector) { + AstalIO.open_inspector(instance_name); + return 0; + } - if (toggle_window != null) { - AstalIO.toggle_window_by_name(instance_name, toggle_window); - return 0; + if (toggle_window != null) { + AstalIO.toggle_window_by_name(instance_name, toggle_window); + return 0; + } + } catch (DBusError.SERVICE_UNKNOWN e) { + return err(@"there is no \"$instance_name\" instance runnning"); + } catch (Error e) { + return err(e.message); } var request = ""; @@ -80,8 +91,14 @@ int main(string[] argv) { request = request.concat(" ", argv[i]); } - var reply = AstalIO.send_message(instance_name, request); - print("%s\n", reply); + try { + var reply = AstalIO.send_message(instance_name, request); + print("%s\n", reply); + } catch (IOError.NOT_FOUND e) { + return err(@"there is no \"$instance_name\" instance runnning"); + } catch (Error e) { + return err(e.message); + } return 0; } diff --git a/lib/mpris/player.vala b/lib/mpris/player.vala index 2776047..9b03f0b 100644 --- a/lib/mpris/player.vala +++ b/lib/mpris/player.vala @@ -101,7 +101,6 @@ public class AstalMpris.Player : Object { /** * Skips to the next track in the tracklist. - * * If there is no next track (and endless playback and track repeat are both off), stop playback. * If [[email protected]:can_go_next] is `false` this method has no effect. */ @@ -111,7 +110,6 @@ public class AstalMpris.Player : Object { /** * Skips to the previous track in the tracklist. - * * If there is no previous track (and endless playback and track repeat are both off), stop playback. * If [[email protected]:can_go_previous] is `false` this method has no effect. */ @@ -121,7 +119,6 @@ public class AstalMpris.Player : Object { /** * Pauses playback. - * * If playback is already paused, this has no effect. * If [[email protected]:can_pause] is `false` this method has no effect. */ @@ -131,7 +128,6 @@ public class AstalMpris.Player : Object { /** * Pauses playback. - * * If playback is already paused, resumes playback. * If playback is stopped, starts playback. */ @@ -141,7 +137,6 @@ public class AstalMpris.Player : Object { /** * Stops playback. - * * If playback is already stopped, this has no effect. * If [[email protected]:can_control] is `false` this method has no effect. */ @@ -151,7 +146,6 @@ public class AstalMpris.Player : Object { /** * Starts or resumes playback. - * * If already playing, this has no effect. * If paused, playback resumes from the current position. * If [[email protected]:can_play] is `false` this method has no effect. |