diff --git a/player.html b/player.html index a4a7a0c..05205b0 100644 --- a/player.html +++ b/player.html @@ -164,34 +164,51 @@ return text } - // Handle unicode (i.e. Japanese/Chinese chars) function renderedLength(text) { - regEx = /&#\d+;|./g + regEx = /&#\d+;/g if (text.match(regEx) !== null) { - return text.match(regEx).length + return text.match(/&#\d+;|./g).length + } + return text.length + } + + function trimLine(text, width) { + // If text has unicode char, a different trim is needed due to rendering + // unicode characters like "面" as one "character". + containsUnicode = text.match(/&#\d+;/) + if (containsUnicode !== null) { + chars = text.match(/&#\d+;|./g) + numUnicodes = text.match(/&#\d+;/g).length + chars.splice(-1, 1) // Remove "|" + text = chars.join("") + fudge = 0.69 // TODO: Fix this...non-monospaced fonts may kill me + spacing = width - renderedLength(text) - (numUnicodes * fudge) + if (spacing < 0) { + spacing = 0 + } + text = text + " ".repeat(spacing) + "|" } else { - return text.length + if (text.length > width) { + end = (width - text.length) + start = width - 2 // Subtract 2 for " |" + text = text.substring(end, start) + " |" + } } + return text } function buildLine(width, text, prefix) { prefix = "|" + prefix // Subtract 2 for " |" - spacing = (width - prefix.length - renderedLength(text) - 2) + spacing = (width - prefix.length - text.length - 2) if(spacing < 0) { spacing = 0 } line = prefix + text + " ".repeat(spacing) + " |" - // Trim line if too long - if(line.length > width) { - end = (width - line.length) - start = width - 2 // Subtract 2 for " |" - line = line.substring(end, start) + " |" - } - return line + return trimLine(line, width) } function buildDownloadLine(width, url) {