From ce6765e8456290c585ebf8c8bc3921a60b2cbb79 Mon Sep 17 00:00:00 2001 From: randombyte-developer Date: Mon, 23 Jul 2018 13:32:11 +0200 Subject: [PATCH] v1.0.2: - Fixed a bug --- build.gradle | 2 +- .../kotlin/de/randombyte/discordrpcmusic/Main.kt | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index 2078651..c1fec03 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group "de.randombyte" -version "1.0.1" +version "1.0.2" repositories { jcenter() diff --git a/src/main/kotlin/de/randombyte/discordrpcmusic/Main.kt b/src/main/kotlin/de/randombyte/discordrpcmusic/Main.kt index e20dea0..e0b5eb7 100644 --- a/src/main/kotlin/de/randombyte/discordrpcmusic/Main.kt +++ b/src/main/kotlin/de/randombyte/discordrpcmusic/Main.kt @@ -11,10 +11,6 @@ private fun String.execute(lines: (Sequence) -> T): T = Runtime.getRuntime().exec(this).inputStream.bufferedReader().useLines(lines) private class TrackInfo(val title: String, val artist: String) { - companion object { - fun fromPair(pair: Pair) = TrackInfo(title = pair.first, artist = pair.second) - } - fun buildRichPresenceObject() = DiscordRichPresence { details = title state = artist @@ -25,7 +21,7 @@ private class TrackInfo(val title: String, val artist: String) { * @return TrackInfo of the currently playing (or paused) track or null if no track or multiple ones could be found */ private fun getCurrentTrackInfo(): TrackInfo? { - val trackInfoPair = "xprop -root".execute { lines -> + val trackInfo = "xprop -root".execute { lines -> lines .filter { XPROP_WINDOW_IDS_PROPERTY_PREFIX in it } .map { it.removePrefix(XPROP_WINDOW_IDS_PROPERTY_PREFIX) } @@ -39,16 +35,17 @@ private fun getCurrentTrackInfo(): TrackInfo? { .map { it.removeSurrounding("\"") } .filter { GOOGLE_PLAY_MUSIC_CHROME_WINDOW_NAME_SUFFIX in it } .map { it.removeSuffix(GOOGLE_PLAY_MUSIC_CHROME_WINDOW_NAME_SUFFIX) } - .map { - val (title, artist) = it.split(" - ", limit = 2) - Pair(title, artist) + .mapNotNull innerMapNotNull@ { + val splits = it.split(" - ", limit = 2) + if (splits.size != 2) return@innerMapNotNull null + TrackInfo(title = splits[0], artist = splits[1]) }.singleOrNull() } } .singleOrNull() } - return trackInfoPair?.let { TrackInfo.fromPair(it) } + return trackInfo } fun main(args: Array) {