UNPKG

ascii-ui

Version:

Graphic terminal emulator for HTML canvas elements

54 lines 1.63 kB
export function tokenizer(text) { const re = /(\s+)|(\S+)/g; const res = []; let match = re.exec(text); while (match) { res.push({ isSeparator: match[1] !== undefined, text: match[0], index: match.index, }); match = re.exec(text); } return res; } export function splitText(text, lineWidth, tknzr = tokenizer) { const res = []; const tokenizedText = tknzr(text); let line = ''; tokenizedText.forEach((token) => { if (!token || (line.length === 0 && token.isSeparator)) { return; } if (line.length + token.text.length <= lineWidth) { line += token.text; } else { if (line.length > 0) { res.push(line + ' '.repeat(lineWidth - line.length)); } if (token.isSeparator) { line = ''; } else { while (token.text.length > lineWidth) { res.push(token.text.substr(0, lineWidth)); token.text = token.text.substr(lineWidth); token.index += lineWidth; } line = token.text; } } }); if (line.length > 0) { res.push(line + ' '.repeat(lineWidth - line.length)); } return res; } export function noWrap(text, lineWidth, ellipsis = '') { if (text && text.length > lineWidth) { return (`${text.substr(0, lineWidth - ellipsis.length)}${ellipsis}`).substr(0, lineWidth); } return text; } //# sourceMappingURL=tokenizer.js.map