2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-29 05:05:01 +00:00
|
|
|
const miscUtil = require('./misc_util.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2016-04-29 05:05:01 +00:00
|
|
|
const events = require('events');
|
|
|
|
const util = require('util');
|
|
|
|
const _ = require('lodash');
|
2015-04-29 21:38:20 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
exports.ANSIEscapeParser = ANSIEscapeParser;
|
|
|
|
|
2016-04-29 05:05:01 +00:00
|
|
|
const CR = 0x0d;
|
|
|
|
const LF = 0x0a;
|
2015-05-03 23:35:55 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
function ANSIEscapeParser(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
2015-04-29 21:38:20 +00:00
|
|
|
this.column = 1;
|
|
|
|
this.row = 1;
|
|
|
|
this.scrollBack = 0;
|
2015-04-30 20:39:03 +00:00
|
|
|
this.graphicRendition = {};
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-05-03 23:35:55 +00:00
|
|
|
this.parseState = {
|
|
|
|
re : /(?:\x1b\x5b)([\?=;0-9]*?)([ABCDHJKfhlmnpsu])/g,
|
|
|
|
};
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
options = miscUtil.valueWithDefault(options, {
|
|
|
|
mciReplaceChar : '',
|
|
|
|
termHeight : 25,
|
|
|
|
termWidth : 80,
|
2015-09-27 21:35:24 +00:00
|
|
|
trailingLF : 'default', // default|omit|no|yes, ...
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.mciReplaceChar = miscUtil.valueWithDefault(options.mciReplaceChar, '');
|
|
|
|
this.termHeight = miscUtil.valueWithDefault(options.termHeight, 25);
|
|
|
|
this.termWidth = miscUtil.valueWithDefault(options.termWidth, 80);
|
2015-09-27 21:35:24 +00:00
|
|
|
this.trailingLF = miscUtil.valueWithDefault(options.trailingLF, 'default');
|
2014-10-20 03:06:39 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
self.moveCursor = function(cols, rows) {
|
|
|
|
self.column += cols;
|
|
|
|
self.row += rows;
|
|
|
|
|
|
|
|
self.column = Math.max(self.column, 1);
|
|
|
|
self.column = Math.min(self.column, self.termWidth);
|
|
|
|
self.row = Math.max(self.row, 1);
|
|
|
|
self.row = Math.min(self.row, self.termHeight);
|
|
|
|
|
|
|
|
self.emit('move cursor', self.column, self.row);
|
|
|
|
self.rowUpdated();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.saveCursorPosition = function() {
|
|
|
|
self.savedPosition = {
|
|
|
|
row : self.row,
|
|
|
|
column : self.column
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
self.restoreCursorPosition = function() {
|
|
|
|
self.row = self.savedPosition.row;
|
|
|
|
self.column = self.savedPosition.column;
|
|
|
|
delete self.savedPosition;
|
|
|
|
self.rowUpdated();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.clearScreen = function() {
|
|
|
|
// :TODO: should be doing something with row/column?
|
|
|
|
self.emit('clear screen');
|
|
|
|
};
|
|
|
|
|
|
|
|
self.rowUpdated = function() {
|
|
|
|
self.emit('row update', self.row + self.scrollBack);
|
|
|
|
};
|
|
|
|
|
|
|
|
function literal(text) {
|
|
|
|
var charCode;
|
|
|
|
|
|
|
|
var len = text.length;
|
|
|
|
for(var i = 0; i < len; i++) {
|
|
|
|
charCode = text.charCodeAt(i) & 0xff; // ensure 8 bit
|
|
|
|
switch(charCode) {
|
|
|
|
case CR :
|
|
|
|
self.column = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LF :
|
|
|
|
self.row++;
|
|
|
|
self.rowUpdated();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
// wrap
|
|
|
|
if(self.column === self.termWidth) {
|
|
|
|
self.column = 1;
|
|
|
|
self.row++;
|
|
|
|
self.rowUpdated();
|
|
|
|
} else {
|
|
|
|
self.column++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-10-30 04:23:44 +00:00
|
|
|
if(self.row === 26) { // :TODO: should be termHeight + 1 ?
|
2014-10-17 02:21:06 +00:00
|
|
|
self.scrollBack++;
|
|
|
|
self.row--;
|
|
|
|
self.rowUpdated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('chunk', text);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProcessedMCI(mci) {
|
|
|
|
if(self.mciReplaceChar.length > 0) {
|
2015-04-30 20:39:03 +00:00
|
|
|
return ansi.getSGRFromGraphicRendition(self.graphicRendition, true) + new Array(mci.length + 1).join(self.mciReplaceChar);
|
2014-10-17 02:21:06 +00:00
|
|
|
} else {
|
|
|
|
return mci;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseMCI(buffer) {
|
2015-04-29 21:38:20 +00:00
|
|
|
// :TODO: move this to "constants" seciton @ top
|
2015-07-02 04:53:01 +00:00
|
|
|
var mciRe = /\%([A-Z]{2})([0-9]{1,2})?(?:\(([0-9A-Za-z,]+)\))*/g;
|
2014-10-17 02:21:06 +00:00
|
|
|
var pos = 0;
|
|
|
|
var match;
|
|
|
|
var mciCode;
|
|
|
|
var args;
|
2014-10-28 03:58:34 +00:00
|
|
|
var id;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
pos = mciRe.lastIndex;
|
|
|
|
match = mciRe.exec(buffer);
|
|
|
|
|
|
|
|
if(null !== match) {
|
|
|
|
if(match.index > pos) {
|
|
|
|
literal(buffer.slice(pos, match.index));
|
|
|
|
}
|
|
|
|
|
|
|
|
mciCode = match[1];
|
2014-10-28 03:58:34 +00:00
|
|
|
id = match[2] || null;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2014-10-28 03:58:34 +00:00
|
|
|
if(match[3]) {
|
|
|
|
args = match[3].split(',');
|
2014-10-17 02:21:06 +00:00
|
|
|
} else {
|
|
|
|
args = [];
|
|
|
|
}
|
|
|
|
|
2014-11-13 06:16:47 +00:00
|
|
|
// if MCI codes are changing, save off the current color
|
|
|
|
var fullMciCode = mciCode + (id || '');
|
|
|
|
if(self.lastMciCode !== fullMciCode) {
|
|
|
|
|
|
|
|
self.lastMciCode = fullMciCode;
|
2015-04-29 21:38:20 +00:00
|
|
|
|
|
|
|
self.graphicRenditionForErase = _.clone(self.graphicRendition, true);
|
2014-11-13 06:16:47 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-04-30 20:39:03 +00:00
|
|
|
self.emit('mci', {
|
|
|
|
mci : mciCode,
|
|
|
|
id : id ? parseInt(id, 10) : null,
|
|
|
|
args : args,
|
|
|
|
SGR : ansi.getSGRFromGraphicRendition(self.graphicRendition, true)
|
2016-04-12 02:23:11 +00:00
|
|
|
});
|
2014-10-30 04:23:44 +00:00
|
|
|
|
|
|
|
if(self.mciReplaceChar.length > 0) {
|
2015-04-29 21:38:20 +00:00
|
|
|
self.emit('chunk', ansi.getSGRFromGraphicRendition(self.graphicRenditionForErase));
|
2014-10-30 04:23:44 +00:00
|
|
|
literal(new Array(match[0].length + 1).join(self.mciReplaceChar));
|
|
|
|
} else {
|
|
|
|
literal(match[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//literal(getProcessedMCI(match[0]));
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2014-10-30 04:23:44 +00:00
|
|
|
//self.emit('chunk', getProcessedMCI(match[0]));
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} while(0 !== mciRe.lastIndex);
|
|
|
|
|
|
|
|
if(pos < buffer.length) {
|
|
|
|
literal(buffer.slice(pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 23:35:55 +00:00
|
|
|
self.reset = function(buffer) {
|
|
|
|
self.parseState = {
|
|
|
|
// ignore anything past EOF marker, if any
|
|
|
|
buffer : buffer.split(String.fromCharCode(0x1a), 1)[0],
|
|
|
|
re : /(?:\x1b\x5b)([\?=;0-9]*?)([ABCDHJKfhlmnpsu])/g,
|
|
|
|
stop : false,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
self.stop = function() {
|
|
|
|
self.parseState.stop = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.parse = function() {
|
|
|
|
// :TODO: ensure this conforms to ANSI-BBS / CTerm / bansi.txt for movement/etc.
|
|
|
|
var pos;
|
|
|
|
var match;
|
|
|
|
var opCode;
|
|
|
|
var args;
|
|
|
|
var re = self.parseState.re;
|
|
|
|
var buffer = self.parseState.buffer;
|
|
|
|
|
|
|
|
self.parseState.stop = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(self.parseState.stop) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = re.lastIndex;
|
|
|
|
match = re.exec(buffer);
|
|
|
|
|
|
|
|
if(null !== match) {
|
|
|
|
if(match.index > pos) {
|
|
|
|
parseMCI(buffer.slice(pos, match.index));
|
|
|
|
}
|
|
|
|
|
|
|
|
opCode = match[2];
|
2016-04-29 05:05:01 +00:00
|
|
|
args = match[1].split(';').map(v => parseInt(v, 10)); // convert to array of ints
|
2015-05-03 23:35:55 +00:00
|
|
|
|
|
|
|
escape(opCode, args);
|
|
|
|
|
|
|
|
self.emit('chunk', match[0]);
|
|
|
|
}
|
|
|
|
} while(0 !== re.lastIndex);
|
|
|
|
|
|
|
|
if(pos < buffer.length) {
|
2015-07-25 22:10:12 +00:00
|
|
|
var lastBit = buffer.slice(pos);
|
2015-09-27 21:35:24 +00:00
|
|
|
|
|
|
|
// :TODO: check for various ending LF's, not just DOS \r\n
|
|
|
|
if('\r\n' === lastBit.slice(-2).toString()) {
|
|
|
|
switch(self.trailingLF) {
|
|
|
|
case 'default' :
|
|
|
|
//
|
|
|
|
// Default is to *not* omit the trailing LF
|
|
|
|
// if we're going to end on termHeight
|
|
|
|
//
|
|
|
|
if(this.termHeight === self.row) {
|
|
|
|
lastBit = lastBit.slice(0, -2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'omit' :
|
|
|
|
case 'no' :
|
|
|
|
case false :
|
|
|
|
lastBit = lastBit.slice(0, -2);
|
|
|
|
break;
|
|
|
|
}
|
2015-07-25 22:10:12 +00:00
|
|
|
}
|
2015-09-27 21:35:24 +00:00
|
|
|
|
2015-07-25 22:10:12 +00:00
|
|
|
parseMCI(lastBit)
|
2015-05-03 23:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('complete');
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2014-10-17 02:21:06 +00:00
|
|
|
self.parse = function(buffer, savedRe) {
|
|
|
|
// :TODO: ensure this conforms to ANSI-BBS / CTerm / bansi.txt for movement/etc.
|
2015-04-29 21:38:20 +00:00
|
|
|
// :TODO: move this to "constants" section @ top
|
2014-10-17 02:21:06 +00:00
|
|
|
var re = /(?:\x1b\x5b)([\?=;0-9]*?)([ABCDHJKfhlmnpsu])/g;
|
|
|
|
var pos = 0;
|
|
|
|
var match;
|
|
|
|
var opCode;
|
|
|
|
var args;
|
|
|
|
|
|
|
|
// ignore anything past EOF marker, if any
|
|
|
|
buffer = buffer.split(String.fromCharCode(0x1a), 1)[0];
|
|
|
|
|
|
|
|
do {
|
|
|
|
pos = re.lastIndex;
|
|
|
|
match = re.exec(buffer);
|
|
|
|
|
|
|
|
if(null !== match) {
|
|
|
|
if(match.index > pos) {
|
|
|
|
parseMCI(buffer.slice(pos, match.index));
|
|
|
|
}
|
|
|
|
|
|
|
|
opCode = match[2];
|
|
|
|
args = getArgArray(match[1].split(';'));
|
|
|
|
|
|
|
|
escape(opCode, args);
|
|
|
|
|
|
|
|
self.emit('chunk', match[0]);
|
|
|
|
}
|
|
|
|
|
2015-05-03 23:35:55 +00:00
|
|
|
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
} while(0 !== re.lastIndex);
|
|
|
|
|
|
|
|
if(pos < buffer.length) {
|
|
|
|
parseMCI(buffer.slice(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('complete');
|
|
|
|
};
|
2015-05-03 23:35:55 +00:00
|
|
|
*/
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
function escape(opCode, args) {
|
|
|
|
var arg;
|
|
|
|
var i;
|
|
|
|
var len;
|
|
|
|
|
|
|
|
switch(opCode) {
|
|
|
|
// cursor up
|
|
|
|
case 'A' :
|
|
|
|
arg = args[0] || 1;
|
|
|
|
self.moveCursor(0, -arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// cursor down
|
|
|
|
case 'B' :
|
|
|
|
arg = args[0] || 1;
|
|
|
|
self.moveCursor(0, arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// cursor forward/right
|
|
|
|
case 'C' :
|
|
|
|
arg = args[0] || 1;
|
|
|
|
self.moveCursor(arg, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// cursor back/left
|
|
|
|
case 'D' :
|
|
|
|
arg = args[0] || 1;
|
|
|
|
self.moveCursor(-arg, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f' : // horiz & vertical
|
|
|
|
case 'H' : // cursor position
|
|
|
|
self.row = args[0] || 1;
|
|
|
|
self.column = args[1] || 1;
|
|
|
|
self.rowUpdated();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// save position
|
|
|
|
case 's' :
|
|
|
|
self.saveCursorPosition();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// restore position
|
|
|
|
case 'u' :
|
|
|
|
self.restoreCursorPosition();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// set graphic rendition
|
|
|
|
case 'm' :
|
|
|
|
for(i = 0, len = args.length; i < len; ++i) {
|
|
|
|
arg = args[i];
|
2015-04-29 04:42:22 +00:00
|
|
|
|
|
|
|
if(ANSIEscapeParser.foregroundColors[arg]) {
|
2015-04-29 21:38:20 +00:00
|
|
|
self.graphicRendition.fg = arg;
|
2015-04-29 04:42:22 +00:00
|
|
|
} else if(ANSIEscapeParser.backgroundColors[arg]) {
|
2015-04-29 21:38:20 +00:00
|
|
|
self.graphicRendition.bg = arg;
|
2015-04-29 04:42:22 +00:00
|
|
|
} else if(ANSIEscapeParser.styles[arg]) {
|
2015-04-30 20:39:03 +00:00
|
|
|
switch(arg) {
|
|
|
|
case 0 :
|
|
|
|
// clear out everything
|
|
|
|
delete self.graphicRendition.intensity;
|
|
|
|
delete self.graphicRendition.underline;
|
|
|
|
delete self.graphicRendition.blink;
|
|
|
|
delete self.graphicRendition.negative;
|
|
|
|
delete self.graphicRendition.invisible;
|
|
|
|
|
|
|
|
self.graphicRendition.fg = 39;
|
|
|
|
self.graphicRendition.bg = 49;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1 :
|
|
|
|
case 2 :
|
|
|
|
case 22 :
|
|
|
|
self.graphicRendition.intensity = arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4 :
|
|
|
|
case 24 :
|
|
|
|
self.graphicRendition.underline = arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5 :
|
|
|
|
case 6 :
|
|
|
|
case 25 :
|
|
|
|
self.graphicRendition.blink = arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7 :
|
|
|
|
case 27 :
|
|
|
|
self.graphicRendition.negative = arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8 :
|
|
|
|
case 28 :
|
|
|
|
self.graphicRendition.invisible = arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
console.log('Unknown attribute: ' + arg); // :TODO: Log properly
|
|
|
|
break;
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-29 21:38:20 +00:00
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// erase display/screen
|
|
|
|
case 'J' :
|
|
|
|
// :TODO: Handle others
|
|
|
|
if(2 === args[0]) {
|
|
|
|
self.clearScreen();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 04:42:22 +00:00
|
|
|
util.inherits(ANSIEscapeParser, events.EventEmitter);
|
|
|
|
|
|
|
|
ANSIEscapeParser.foregroundColors = {
|
|
|
|
30 : 'black',
|
|
|
|
31 : 'red',
|
|
|
|
32 : 'green',
|
|
|
|
33 : 'yellow',
|
|
|
|
34 : 'blue',
|
|
|
|
35 : 'magenta',
|
|
|
|
36 : 'cyan',
|
|
|
|
37 : 'white',
|
2015-04-30 20:39:03 +00:00
|
|
|
39 : 'default', // same as white for most implementations
|
|
|
|
|
2015-04-29 04:42:22 +00:00
|
|
|
90 : 'grey'
|
|
|
|
};
|
|
|
|
Object.freeze(ANSIEscapeParser.foregroundColors);
|
|
|
|
|
|
|
|
ANSIEscapeParser.backgroundColors = {
|
|
|
|
40 : 'black',
|
|
|
|
41 : 'red',
|
|
|
|
42 : 'green',
|
|
|
|
43 : 'yellow',
|
|
|
|
44 : 'blue',
|
|
|
|
45 : 'magenta',
|
|
|
|
46 : 'cyan',
|
2015-04-30 20:39:03 +00:00
|
|
|
47 : 'white',
|
|
|
|
49 : 'default', // same as black for most implementations
|
2015-04-29 04:42:22 +00:00
|
|
|
};
|
|
|
|
Object.freeze(ANSIEscapeParser.backgroundColors);
|
|
|
|
|
2015-04-30 20:39:03 +00:00
|
|
|
// :TODO: ensure these names all align with that of ansi_term.js
|
|
|
|
//
|
|
|
|
// See the following specs:
|
|
|
|
// * http://www.ansi-bbs.org/ansi-bbs-core-server.html
|
|
|
|
// * http://www.vt100.net/docs/vt510-rm/SGR
|
|
|
|
// * https://github.com/protomouse/synchronet/blob/master/src/conio/cterm.txt
|
|
|
|
//
|
|
|
|
// Note that these are intentionally not in order such that they
|
|
|
|
// can be grouped by concept here in code.
|
|
|
|
//
|
2015-04-29 04:42:22 +00:00
|
|
|
ANSIEscapeParser.styles = {
|
2015-04-30 20:39:03 +00:00
|
|
|
0 : 'default', // Everything disabled
|
|
|
|
|
|
|
|
1 : 'intensityBright', // aka bold
|
|
|
|
2 : 'intensityDim',
|
|
|
|
22 : 'intensityNormal',
|
|
|
|
|
|
|
|
4 : 'underlineOn', // Not supported by most BBS-like terminals
|
|
|
|
24 : 'underlineOff', // Not supported by most BBS-like terminals
|
|
|
|
|
|
|
|
5 : 'blinkSlow', // blinkSlow & blinkFast are generally treated the same
|
|
|
|
6 : 'blinkFast', // blinkSlow & blinkFast are generally treated the same
|
|
|
|
25 : 'blinkOff',
|
|
|
|
|
|
|
|
7 : 'negativeImageOn', // Generally not supported or treated as "reverse FG & BG"
|
|
|
|
27 : 'negativeImageOff', // Generally not supported or treated as "reverse FG & BG"
|
|
|
|
|
|
|
|
8 : 'invisibleOn', // FG set to BG
|
|
|
|
28 : 'invisibleOff', // Not supported by most BBS-like terminals
|
2015-04-29 04:42:22 +00:00
|
|
|
};
|
|
|
|
Object.freeze(ANSIEscapeParser.styles);
|