commit a9fb768d561fb33ee1889e495636821abe705485
parent ad6967adb6637c0b49448345eaaf1b3ecd1a022a
Author: Sweets <Sweets@users.noreply.github.com>
Date: Sat, 19 Sep 2020 20:32:00 -0700
Add argument processing to replace config.h
Diffstat:
4 files changed, 63 insertions(+), 20 deletions(-)
diff --git a/config.h b/config.h
@@ -1,2 +0,0 @@
-#define PRINT_JSON
-#define OUTPUT_DELIMITER "\n"
diff --git a/src/output.c b/src/output.c
@@ -47,13 +47,12 @@ void output_notification(GVariant *parameters) {
char *summary_sanitized = sanitize(summary);
char *body_sanitized = sanitize(body);
- #ifdef PRINT_JSON
- json_output(app_name_sanitized, app_icon_sanitized, replaces_id, timeout,
- hints, actions, summary_sanitized, body_sanitized);
- #else
- default_output(app_icon_sanitized, app_icon_sanitized, replaces_id,
- timeout, hints, actions, summary_sanitized, body_sanitized);
- #endif
+ if (print_json)
+ json_output(app_name_sanitized, app_icon_sanitized, replaces_id,
+ timeout, hints, actions, summary_sanitized, body_sanitized);
+ else
+ default_output(app_icon_sanitized, app_icon_sanitized, replaces_id,
+ timeout, hints, actions, summary_sanitized, body_sanitized);
free(app_name_sanitized);
free(app_icon_sanitized);
@@ -128,21 +127,39 @@ void default_output(gchar *app_name, gchar *app_icon, guint32 replaces_id,
gint32 timeout, GVariant *hints, gchar **actions, gchar *summary,
gchar *body) {
- printf("app_name: %s\napp_icon: %s\nreplaces_id: %u\ntimeout: %d\n",
- app_name, app_icon, replaces_id, timeout);
+ printf("app_name: %s%sapp_icon: %s%sreplaces_id: %u%stimeout: %d%s",
+ app_name, delimiter, app_icon, delimiter, replaces_id,
+ delimiter, timeout, delimiter);
+
+ printf("hints:%s", delimiter);
+
+ char *str_format = NULL,
+ *int_format = NULL,
+ *uint_format = NULL,
+ *double_format = NULL,
+ *boolean_format = NULL,
+ *byte_format = NULL;
+
+ sprintf(str_format, "\t%%s: %%s%s", delimiter);
+ sprintf(int_format, "\t%%s: %%d%s", delimiter);
+ sprintf(uint_format, "\t%%s: %%u%s", delimiter);
+ sprintf(double_format, "\t%%s: %%f%s", delimiter);
+ sprintf(boolean_format, "\t%%s: %%x%s", delimiter);
+ sprintf(byte_format, "\t%%s: %%d%s", delimiter);
- printf("hints:\n");
- hints_output_iterator(hints, "\t%s: %s\n", "\t%s: %d\n", "\t%s: %u",
- "\t%s: %f\n", "\t%s: %x\n", "\t%s: %d\n");
- printf("actions:\n");
+ hints_output_iterator(hints,
+ str_format, int_format, uint_format, double_format, boolean_format,
+ byte_format);
+ printf("actions:%s", delimiter);
unsigned int index = 0;
while (actions[index] && actions[index + 1]) {
- printf("\t%s: %s\n", actions[index + 1], actions[index]);
+ printf("\t%s: %s%s", actions[index + 1], actions[index], delimiter);
index += 2;
}
- printf("summary: %s\nbody: %s\n", summary, body);
+ printf("summary: %s%sbody: %s%s",
+ summary, delimiter, body, delimiter);
}
diff --git a/src/tiramisu.c b/src/tiramisu.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <signal.h>
+#include <unistd.h>
#include <gio/gio.h>
#include <glib.h>
@@ -8,12 +9,14 @@
#include "tiramisu.h"
#include "output.h"
-#include "config.h"
GDBusConnection *dbus_connection = NULL;
GDBusNodeInfo *introspection = NULL;
GMainLoop *main_loop = NULL;
+
unsigned int notification_id = 0;
+char print_json = 0;
+char *delimiter = "\n";
gboolean stop_main_loop(gpointer user_data) {
g_main_loop_quit(main_loop);
@@ -22,6 +25,30 @@ gboolean stop_main_loop(gpointer user_data) {
}
int main(int argc, char **argv) {
+ /* Parse arguments */
+
+ char argument;
+ while ((argument = getopt(argc, argv, "hjd:"))) {
+ switch (argument) {
+ case 'd':
+ delimiter = optarg;
+ break;
+ case 'h':
+ printf("%s\n",
+ "tiramisu -[h|d|j]\n"
+ "-h\tHelp dialog\n"
+ "-d\tDelimeter for default output style.\n"
+ "-j\tUse JSON output style\n");
+ return EXIT_SUCCESS;
+ break;
+ case 'j':
+ print_json = 1;
+ break;
+ default:
+ break;
+ }
+ }
+
guint owned_name;
/* Connect to DBUS */
@@ -52,6 +79,7 @@ int main(int argc, char **argv) {
g_clear_pointer(&introspection, g_dbus_node_info_unref);
g_bus_unown_name(owned_name);
+ return EXIT_SUCCESS;
}
void bus_acquired(GDBusConnection *connection, const gchar *name,
diff --git a/src/tiramisu.h b/src/tiramisu.h
@@ -6,13 +6,13 @@
#include <gio/gio.h>
#include <glib.h>
-#include "config.h"
-
extern GDBusConnection *dbus_connection;
extern GDBusNodeInfo *introspection;
extern GMainLoop *main_loop;
extern unsigned int notification_id;
+extern char print_json;
+extern char *delimiter;
#ifdef DEBUG
#define print(...) fprintf(stderr, __VA_ARGS__);