From cb6b3750228ae4041a254e344fb24dbd50f38434 Mon Sep 17 00:00:00 2001
From: Christian Hesse <mail@eworm.de>
Date: Fri, 1 Apr 2016 09:26:47 +0200
Subject: Allow one line notifications

This is related to #9.
---
 README.md          |  1 +
 config.def.h       |  4 ++--
 mpd-notification.c | 28 ++++++++++++++++++++--------
 mpd-notification.h |  2 +-
 4 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md
index 08e021f..52b9bbd 100644
--- a/README.md
+++ b/README.md
@@ -59,6 +59,7 @@ or `systemctl --user enable mpd-notification`.
 * *-h*: show help
 * *-H HOST*: connect to HOST
 * *-m MUSIC-DIR*: use MUSIC-DIR for artwork lookup
+* *-o*: Notification text is one line (no line breaks)
 * *-p PORT*: connect to PORT
 * *-t TIMEOUT*: notification timeout
 * *-v*: verbose output
diff --git a/config.def.h b/config.def.h
index e574e37..f10e131 100644
--- a/config.def.h
+++ b/config.def.h
@@ -18,8 +18,8 @@
  * TEXT_PLAY_* need to include one string modifier '%s' each. */
 #define TEXT_TOPIC		"MPD Notification"
 #define TEXT_PLAY_TITLE		"Playing <b>%s</b>"
-#define TEXT_PLAY_ARTIST	"\nby <i>%s</i>"
-#define TEXT_PLAY_ALBUM		"\nfrom <i>%s</i>"
+#define TEXT_PLAY_ARTIST	"by <i>%s</i>"
+#define TEXT_PLAY_ALBUM		"from <i>%s</i>"
 #define TEXT_PAUSE		"Paused playback"
 #define TEXT_STOP		"Stopped playback"
 #define TEXT_NONE		"No action received yet."
diff --git a/mpd-notification.c b/mpd-notification.c
index 49673e4..56dbaa2 100644
--- a/mpd-notification.c
+++ b/mpd-notification.c
@@ -7,12 +7,13 @@
 
 #include "mpd-notification.h"
 
-const static char optstring[] = "hH:m:p:t:vV";
+const static char optstring[] = "hH:m:op:t:vV";
 const static struct option options_long[] = {
 	/* name		has_arg			flag	val */
 	{ "help",	no_argument,		NULL,	'h' },
 	{ "host",	required_argument,	NULL,	'H' },
 	{ "music-dir",	required_argument,	NULL,	'm' },
+	{ "oneline",	no_argument,		NULL,	'o' },
 	{ "port",	required_argument,	NULL,	'p' },
 	{ "timeout",	required_argument,	NULL,	't' },
 	{ "verbose",	no_argument,		NULL,	'v' },
@@ -26,6 +27,7 @@ NotifyNotification * notification = NULL;
 struct mpd_connection * conn = NULL;
 uint8_t doexit = 0;
 uint8_t verbose = 0;
+uint8_t oneline = 0;
 
 /*** received_signal ***/
 void received_signal(int signal) {
@@ -157,14 +159,21 @@ char * get_icon(const char * music_dir, const char * uri) {
 }
 
 /*** append_string ***/
-char * append_string(char * string, const char * format, const char * s) {
-	char * tmp;
+char * append_string(char * string, const char * format, const char delim, const char * s) {
+	char * tmp, * offset;
 
 	tmp = g_markup_escape_text(s, -1);
 
-	string = realloc(string, strlen(string) + strlen(format) + strlen(tmp));
+	string = realloc(string, strlen(string) + strlen(format) + strlen(tmp) + 2 /* delim + line break */);
 
-	sprintf(string + strlen(string), format, tmp);
+	offset = string + strlen(string);
+
+	if (delim > 0) {
+		*offset = delim;
+		offset++;
+	}
+
+	sprintf(offset, format, tmp);
 
 	free(tmp);
 
@@ -201,6 +210,9 @@ int main(int argc, char ** argv) {
 			case 'h':
 				help++;
 				break;
+			case 'o':
+				oneline++;
+				break;
 			case 'v':
 				verbose++;
 				break;
@@ -319,13 +331,13 @@ int main(int argc, char ** argv) {
 			/* initial allocation and string termination */
 			notifystr = strdup("");
 
-			notifystr = append_string(notifystr, TEXT_PLAY_TITLE, title);
+			notifystr = append_string(notifystr, TEXT_PLAY_TITLE, 0, title);
 
 			if ((artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0)) != NULL)
-				notifystr = append_string(notifystr, TEXT_PLAY_ARTIST, artist);
+				notifystr = append_string(notifystr, TEXT_PLAY_ARTIST, oneline ? ' ' : '\n', artist);
 
 			if ((album = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0)) != NULL)
-				notifystr = append_string(notifystr, TEXT_PLAY_ALBUM, album);
+				notifystr = append_string(notifystr, TEXT_PLAY_ALBUM, oneline ? ' ' : '\n', album);
 
 			uri = mpd_song_get_uri(song);
 
diff --git a/mpd-notification.h b/mpd-notification.h
index 541b63d..429f3ed 100644
--- a/mpd-notification.h
+++ b/mpd-notification.h
@@ -43,7 +43,7 @@ GdkPixbuf * retrieve_album_art(const char * music_dir, const char * uri);
 char * get_icon(const char * music_dir, const char * uri);
 
 /*** append_string ***/
-char * append_string(char * string, const char * format, const char * s);
+char * append_string(char * string, const char * format, const char delim, const char * s);
 
 /*** main ***/
 int main(int argc, char ** argv);
-- 
cgit v1.2.3