Bug fix: Word wrap when dealing with pipe codes

Bug fix: Assumed FES styleizers
This commit is contained in:
Bryan Ashby 2021-01-02 15:04:16 -07:00
parent 14b1b2b145
commit 862031c57f
No known key found for this signature in database
GPG key ID: B49EB437951D2542
3 changed files with 44 additions and 7 deletions

View file

@ -20,6 +20,7 @@ exports.stringFromNullTermBuffer = stringFromNullTermBuffer;
exports.stringToNullTermBuffer = stringToNullTermBuffer;
exports.renderSubstr = renderSubstr;
exports.renderStringLength = renderStringLength;
exports.ansiRenderStringLength = ansiRenderStringLength;
exports.formatByteSizeAbbr = formatByteSizeAbbr;
exports.formatByteSize = formatByteSize;
exports.formatCountAbbr = formatCountAbbr;
@ -297,7 +298,7 @@ function renderStringLength(s) {
let len = 0;
const re = ANSI_OR_PIPE_REGEXP;
re.lastIndex = 0; // we recycle the rege; reset
re.lastIndex = 0; // we recycle the regex; reset
//
// Loop counting only literal (non-control) sequences
@ -312,7 +313,41 @@ function renderStringLength(s) {
len += s.slice(pos, m.index).length;
}
if('C' === m[3]) { // ESC[<N>C is foward/right
if('C' === m[3]) { // ESC[<N>C is forward/right
len += parseInt(m[2], 10) || 0;
}
}
} while(0 !== re.lastIndex);
if(pos < s.length) {
len += s.slice(pos).length;
}
return len;
}
// Like renderStringLength() but ANSI only (no pipe codes accounted for)
function ansiRenderStringLength(s) {
let m;
let pos;
let len = 0;
const re = ANSI.getFullMatchRegExp();
//
// Loop counting only literal (non-control) sequences
// paying special attention to ESC[<N>C which means forward <N>
//
do {
pos = re.lastIndex;
m = re.exec(s);
if(m) {
if(m.index > pos) {
len += s.slice(pos, m.index).length;
}
if('C' === m[3]) { // ESC[<N>C is forward/right
len += parseInt(m[2], 10) || 0;
}
}