Fix some prepAnsi code, add insert() method

This commit is contained in:
Bryan Ashby 2017-08-24 22:20:44 -06:00
parent fc200250e8
commit dda4ad6698
1 changed files with 10 additions and 0 deletions

View File

@ -12,6 +12,7 @@ const _ = require('lodash');
exports.stylizeString = stylizeString;
exports.pad = pad;
exports.insert = insert;
exports.replaceAt = replaceAt;
exports.isPrintable = isPrintable;
exports.stripAllLineFeeds = stripAllLineFeeds;
@ -172,6 +173,10 @@ function pad(s, len, padChar, dir, stringSGR, padSGR, useRenderLen) {
return stringSGR + s;
}
function insert(s, index, substr) {
return `${s.slice(0, index)}${substr}${s.slice(index)}`;
}
function replaceAt(s, n, t) {
return s.substring(0, n) + t + s.substring(n + 1);
}
@ -484,6 +489,10 @@ function prepAnsi(input, options, cb) {
case 'new' :
line = _.trimStart(line);
if(line) {
if(output) {
output += '\r\n';
}
textState = 'cont';
}
break;
@ -543,6 +552,7 @@ function isAnsi(input) {
});
*/
// :TODO: if a similar method is kept, use exec() until threshold
const ANSI_DET_REGEXP = /(?:\x1b\x5b)[\?=;0-9]*?[ABCDEFGHJKLMSTfhlmnprsu]/g;
const m = input.match(ANSI_DET_REGEXP) || [];
return m.length >= 4; // :TODO: do this reasonably, e.g. a percent or soemthing