Handle unicode characters and empty lines

main
Jerry Aldrich 5 years ago
parent a0b1282bbe
commit 25939bc131
  1. 44
      player.html

@ -163,21 +163,27 @@
return text return text
} }
// Handle unicode (i.e. Japanese/Chinese chars)
// This is so bad...but special chars mess up '.length' function renderedLength(text) {
function badThing(text) { regEx = /&#\d+;|./g
text = text.replace(/[^\x00-\x7F]/g, '') if (text.match(regEx) !== null) {
return text return text.match(regEx).length
} else {
return text.length
}
} }
function buildLine(width, text, prefix) { function buildLine(width, text, prefix) {
text = badThing(text)
prefix = "|" + prefix 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) { if(spacing < 0) {
spacing = 0 spacing = 0
} }
line = prefix + text + " ".repeat(spacing) + " |" line = prefix + text + " ".repeat(spacing) + " |"
// Trim line if too long // Trim line if too long
if(line.length > width) { if(line.length > width) {
end = (width - line.length) end = (width - line.length)
@ -218,6 +224,7 @@
function renderStreamInfo(title="", songName="", listeners="", streamURL="") { function renderStreamInfo(title="", songName="", listeners="", streamURL="") {
width = 60 width = 60
titleLine = buildTitleLine(width, title) titleLine = buildTitleLine(width, title)
songNameLine = buildLine(width, songName, " Song Title: ") songNameLine = buildLine(width, songName, " Song Title: ")
listenersLine = buildLine(width, listeners, " Listeners: ") listenersLine = buildLine(width, listeners, " Listeners: ")
@ -226,11 +233,24 @@
var streamLines = [] var streamLines = []
streamLines.push("/" + "-".repeat(width - 2) + "\\") streamLines.push("/" + "-".repeat(width - 2) + "\\")
streamLines.push(titleLine) streamLines.push(titleLine)
streamLines.push("|" + " ".repeat(width - 2) + "|")
streamLines.push(songNameLine) if (songName != "") {
streamLines.push(listenersLine) streamLines.push("|" + " ".repeat(width - 2) + "|")
streamLines.push(downloadLine) streamLines.push(songNameLine)
streamLines.push("|" + " ".repeat(width - 2) + "|") }
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) + "/") streamLines.push("\\" + "-".repeat(width - 2) + "/")
document.getElementById("stream-info").innerHTML = streamLines.join("\n") document.getElementById("stream-info").innerHTML = streamLines.join("\n")

Loading…
Cancel
Save