From 25939bc131484140e1f6f8e20d6f3c15cf78dad0 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Fri, 26 Jun 2020 18:05:38 -0700 Subject: [PATCH] Handle unicode characters and empty lines --- player.html | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/player.html b/player.html index 895a9d6..b24f7b6 100644 --- a/player.html +++ b/player.html @@ -163,21 +163,27 @@ return text } - - // This is so bad...but special chars mess up '.length' - function badThing(text) { - text = text.replace(/[^\x00-\x7F]/g, '') - return text + // Handle unicode (i.e. Japanese/Chinese chars) + function renderedLength(text) { + regEx = /&#\d+;|./g + if (text.match(regEx) !== null) { + return text.match(regEx).length + } else { + return text.length + } } function buildLine(width, text, prefix) { - text = badThing(text) prefix = "|" + prefix - spacing = (width - prefix.length - text.length - 2) // Subtract 2 for " |" + + // Subtract 2 for " |" + spacing = (width - prefix.length - renderedLength(text) - 2) if(spacing < 0) { spacing = 0 } + line = prefix + text + " ".repeat(spacing) + " |" + // Trim line if too long if(line.length > width) { end = (width - line.length) @@ -218,6 +224,7 @@ function renderStreamInfo(title="", songName="", listeners="", streamURL="") { width = 60 + titleLine = buildTitleLine(width, title) songNameLine = buildLine(width, songName, " Song Title: ") listenersLine = buildLine(width, listeners, " Listeners: ") @@ -226,11 +233,24 @@ var streamLines = [] streamLines.push("/" + "-".repeat(width - 2) + "\\") streamLines.push(titleLine) - streamLines.push("|" + " ".repeat(width - 2) + "|") - streamLines.push(songNameLine) - streamLines.push(listenersLine) - streamLines.push(downloadLine) - streamLines.push("|" + " ".repeat(width - 2) + "|") + + if (songName != "") { + streamLines.push("|" + " ".repeat(width - 2) + "|") + streamLines.push(songNameLine) + } + if (listeners != "") { + if (songName == "") { + streamLines.push("|" + " ".repeat(width - 2) + "|") + } + streamLines.push(listenersLine) + } + if (streamURL != "") { + if (songName == "" && listeners == "") { + streamLines.push("|" + " ".repeat(width - 2) + "|") + } + streamLines.push(downloadLine) + streamLines.push("|" + " ".repeat(width - 2) + "|") + } streamLines.push("\\" + "-".repeat(width - 2) + "/") document.getElementById("stream-info").innerHTML = streamLines.join("\n")