Fix word wrap crash reported by user when pipe codes are in play

This commit is contained in:
Bryan Ashby 2018-02-04 10:27:32 -07:00
parent 7a6188bba0
commit 0a486d290f
2 changed files with 14 additions and 5 deletions

View File

@ -218,8 +218,6 @@ function stringToNullTermBuffer(s, options = { encoding : 'utf8', maxBufLen : -1
}
const PIPE_REGEXP = /(\|[A-Z\d]{2})/g;
//const ANSI_REGEXP = /[\u001b\u009b][[()#;?]*([0-9]{1,4}(?:;[0-9]{0,4})*)?([0-9A-ORZcf-npqrsuy=><])/g;
//const ANSI_OR_PIPE_REGEXP = new RegExp(PIPE_REGEXP.source + '|' + ANSI_REGEXP.source, 'g');
const ANSI_OR_PIPE_REGEXP = new RegExp(PIPE_REGEXP.source + '|' + ANSI.getFullMatchRegExp().source, 'g');
//
@ -275,12 +273,23 @@ function renderSubstr(str, start, length) {
//
// See also https://github.com/chalk/ansi-regex/blob/master/index.js
//
function renderStringLength(s) {
function renderStringLength(s, options = { pipe : true, ansi : true } ) {
let m;
let pos;
let len = 0;
const re = ANSI_OR_PIPE_REGEXP;
let re;
if(options.pipe && options.ansi) {
re = ANSI_OR_PIPE_REGEXP;
} else if(options.pipe) {
re = PIPE_REGEXP;
} else if(options.ansi) {
re = ANSI.getFullMatchRegExp();
} else {
// no options - just return string length.
return s.length;
}
re.lastIndex = 0; // we recycle the rege; reset
//

View File

@ -49,7 +49,7 @@ function wordWrapText(text, options) {
function appendWord() {
word.match(REGEXP_GOBBLE).forEach( w => {
renderLen = renderStringLength(w);
renderLen = renderStringLength(w, { ansi : true, pipe : false } );
if(result.renderLen[i] + renderLen > options.width) {
if(0 === i) {