2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const miscUtil = require('./misc_util.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const Log = require('./logger.js').log;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const events = require('events');
|
|
|
|
const util = require('util');
|
|
|
|
const _ = require('lodash');
|
2015-04-29 21:38:20 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.ANSIEscapeParser = ANSIEscapeParser;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
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) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.column = 1;
|
|
|
|
this.graphicRendition = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
this.parseState = {
|
2022-06-05 20:04:25 +00:00
|
|
|
re: /(?:\x1b\x5b)([?=;0-9]*?)([ABCDHJKfhlmnpsu])/g, // eslint-disable-line no-control-regex
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
options = miscUtil.valueWithDefault(options, {
|
2022-06-05 20:04:25 +00:00
|
|
|
mciReplaceChar: '',
|
|
|
|
termHeight: 25,
|
|
|
|
termWidth: 80,
|
|
|
|
trailingLF: 'default', // default|omit|no|yes, ...
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.mciReplaceChar = miscUtil.valueWithDefault(options.mciReplaceChar, '');
|
|
|
|
this.termHeight = miscUtil.valueWithDefault(options.termHeight, 25);
|
|
|
|
this.termWidth = miscUtil.valueWithDefault(options.termWidth, 80);
|
2023-08-25 19:24:47 +00:00
|
|
|
this.breakWidth = this.termWidth;
|
2023-08-25 23:34:45 +00:00
|
|
|
// toNumber takes care of null, undefined etc as well.
|
|
|
|
let artWidth = _.toNumber(options.artWidth);
|
|
|
|
if(!(_.isNaN(artWidth)) && artWidth > 0 && artWidth < this.breakWidth) {
|
2023-08-25 19:24:47 +00:00
|
|
|
this.breakWidth = options.artWidth;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
this.trailingLF = miscUtil.valueWithDefault(options.trailingLF, 'default');
|
2022-03-28 23:51:43 +00:00
|
|
|
|
|
|
|
this.row = Math.min(options?.startRow ?? 1, this.termHeight);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.moveCursor = function (cols, rows) {
|
2018-06-23 03:26:46 +00:00
|
|
|
self.column += cols;
|
2022-06-05 20:04:25 +00:00
|
|
|
self.row += rows;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
self.column = Math.max(self.column, 1);
|
2022-06-05 20:04:25 +00:00
|
|
|
self.column = Math.min(self.column, self.termWidth); // can't move past term width
|
|
|
|
self.row = Math.max(self.row, 1);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.positionUpdated();
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.saveCursorPosition = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.savedPosition = {
|
2022-06-05 20:04:25 +00:00
|
|
|
row: self.row,
|
|
|
|
column: self.column,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.restoreCursorPosition = function () {
|
|
|
|
self.row = self.savedPosition.row;
|
2018-06-23 03:26:46 +00:00
|
|
|
self.column = self.savedPosition.column;
|
2018-06-22 05:15:04 +00:00
|
|
|
delete self.savedPosition;
|
|
|
|
|
|
|
|
self.positionUpdated();
|
2018-06-23 03:26:46 +00:00
|
|
|
// self.rowUpdated();
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.clearScreen = function () {
|
2022-03-28 23:51:43 +00:00
|
|
|
self.column = 1;
|
2022-06-05 20:04:25 +00:00
|
|
|
self.row = 1;
|
2018-06-22 05:15:04 +00:00
|
|
|
self.emit('clear screen');
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.positionUpdated = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.emit('position update', self.row, self.column);
|
|
|
|
};
|
2016-12-07 01:58:56 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
function literal(text) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const len = text.length;
|
|
|
|
let pos = 0;
|
|
|
|
let start = 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
let charCode;
|
2020-05-16 23:37:51 +00:00
|
|
|
let lastCharCode;
|
2016-12-07 01:58:56 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
while (pos < len) {
|
2018-06-23 03:26:46 +00:00
|
|
|
charCode = text.charCodeAt(pos) & 0xff; // 8bit clean
|
2014-11-13 06:16:47 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (charCode) {
|
|
|
|
case CR:
|
2023-08-25 19:24:47 +00:00
|
|
|
self.emit('literal', text.slice(start, pos + 1));
|
|
|
|
start = pos + 1;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
self.column = 1;
|
|
|
|
|
|
|
|
self.positionUpdated();
|
|
|
|
break;
|
2014-10-30 04:23:44 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case LF:
|
2020-05-16 23:37:51 +00:00
|
|
|
// Handle ANSI saved with UNIX-style LF's only
|
|
|
|
// vs the CRLF pairs
|
|
|
|
if (lastCharCode !== CR) {
|
|
|
|
self.column = 1;
|
|
|
|
}
|
|
|
|
|
2023-08-25 19:24:47 +00:00
|
|
|
self.emit('literal', text.slice(start, pos + 1));
|
|
|
|
start = pos + 1;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.row += 1;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
self.positionUpdated();
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
2023-08-25 19:24:47 +00:00
|
|
|
if (self.column === self.breakWidth) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.emit('literal', text.slice(start, pos + 1));
|
|
|
|
start = pos + 1;
|
|
|
|
|
2023-08-25 23:34:45 +00:00
|
|
|
// If we hit breakWidth before termWidth then we need to force the terminal to go to the next line.
|
2023-08-25 19:24:47 +00:00
|
|
|
if(self.column < self.termWidth) {
|
|
|
|
self.emit('literal', '\r\n');
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
self.column = 1;
|
2022-06-05 20:04:25 +00:00
|
|
|
self.row += 1;
|
2018-06-22 05:15:04 +00:00
|
|
|
self.positionUpdated();
|
|
|
|
} else {
|
|
|
|
self.column += 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++pos;
|
2020-05-16 23:37:51 +00:00
|
|
|
lastCharCode = charCode;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Finalize this chunk
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2023-08-25 19:24:47 +00:00
|
|
|
if (self.column > self.breakWidth) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.column = 1;
|
2022-06-05 20:04:25 +00:00
|
|
|
self.row += 1;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.positionUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
const rem = text.slice(start);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (rem) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.emit('literal', rem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseMCI(buffer) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: move this to "constants" seciton @ top
|
2018-06-22 05:15:04 +00:00
|
|
|
var mciRe = /%([A-Z]{2})([0-9]{1,2})?(?:\(([0-9A-Za-z,]+)\))*/g;
|
|
|
|
var pos = 0;
|
|
|
|
var match;
|
|
|
|
var mciCode;
|
|
|
|
var args;
|
|
|
|
var id;
|
|
|
|
|
|
|
|
do {
|
2022-06-05 20:04:25 +00:00
|
|
|
pos = mciRe.lastIndex;
|
|
|
|
match = mciRe.exec(buffer);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (null !== match) {
|
|
|
|
if (match.index > pos) {
|
2018-06-22 05:15:04 +00:00
|
|
|
literal(buffer.slice(pos, match.index));
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
mciCode = match[1];
|
2022-06-05 20:04:25 +00:00
|
|
|
id = match[2] || null;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (match[3]) {
|
2018-06-22 05:15:04 +00:00
|
|
|
args = match[3].split(',');
|
|
|
|
} else {
|
|
|
|
args = [];
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// if MCI codes are changing, save off the current color
|
2018-06-22 05:15:04 +00:00
|
|
|
var fullMciCode = mciCode + (id || '');
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.lastMciCode !== fullMciCode) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.lastMciCode = fullMciCode;
|
|
|
|
|
|
|
|
self.graphicRenditionForErase = _.clone(self.graphicRendition);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('mci', {
|
2022-06-05 20:04:25 +00:00
|
|
|
position: [self.row, self.column],
|
|
|
|
mci: mciCode,
|
|
|
|
id: id ? parseInt(id, 10) : null,
|
|
|
|
args: args,
|
|
|
|
SGR: ansi.getSGRFromGraphicRendition(self.graphicRendition, true),
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.mciReplaceChar.length > 0) {
|
|
|
|
const sgrCtrl = ansi.getSGRFromGraphicRendition(
|
|
|
|
self.graphicRenditionForErase
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.emit(
|
|
|
|
'control',
|
|
|
|
sgrCtrl,
|
|
|
|
'm',
|
|
|
|
sgrCtrl.slice(2).split(/[;m]/).slice(0, 3)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
literal(new Array(match[0].length + 1).join(self.mciReplaceChar));
|
|
|
|
} else {
|
|
|
|
literal(match[0]);
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
} while (0 !== mciRe.lastIndex);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (pos < buffer.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
literal(buffer.slice(pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.reset = function (input) {
|
2022-03-29 03:20:55 +00:00
|
|
|
self.column = 1;
|
|
|
|
self.row = Math.min(options?.startRow ?? 1, self.termHeight);
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
self.parseState = {
|
2018-06-23 03:26:46 +00:00
|
|
|
// ignore anything past EOF marker, if any
|
2022-06-05 20:04:25 +00:00
|
|
|
buffer: input.split(String.fromCharCode(0x1a), 1)[0],
|
|
|
|
re: /(?:\x1b\x5b)([?=;0-9]*?)([ABCDHJKfhlmnpsu])/g, // eslint-disable-line no-control-regex
|
|
|
|
stop: false,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.stop = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.parseState.stop = true;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.parse = function (input) {
|
|
|
|
if (input) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.reset(input);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: ensure this conforms to ANSI-BBS / CTerm / bansi.txt for movement/etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
var pos;
|
|
|
|
var match;
|
|
|
|
var opCode;
|
|
|
|
var args;
|
2022-06-05 20:04:25 +00:00
|
|
|
var re = self.parseState.re;
|
|
|
|
var buffer = self.parseState.buffer;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
self.parseState.stop = false;
|
|
|
|
|
|
|
|
do {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.parseState.stop) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
pos = re.lastIndex;
|
|
|
|
match = re.exec(buffer);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (null !== match) {
|
|
|
|
if (match.index > pos) {
|
2018-06-22 05:15:04 +00:00
|
|
|
parseMCI(buffer.slice(pos, match.index));
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
opCode = match[2];
|
|
|
|
args = match[1].split(';').map(v => parseInt(v, 10)); // convert to array of ints
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
escape(opCode, args);
|
|
|
|
|
|
|
|
//self.emit('chunk', match[0]);
|
|
|
|
self.emit('control', match[0], opCode, args);
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
} while (0 !== re.lastIndex);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (pos < buffer.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var lastBit = buffer.slice(pos);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: check for various ending LF's, not just DOS \r\n
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('\r\n' === lastBit.slice(-2).toString()) {
|
|
|
|
switch (self.trailingLF) {
|
|
|
|
case 'default':
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Default is to *not* omit the trailing LF
|
|
|
|
// if we're going to end on termHeight
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.termHeight === self.row) {
|
2018-06-22 05:15:04 +00:00
|
|
|
lastBit = lastBit.slice(0, -2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'omit':
|
|
|
|
case 'no':
|
|
|
|
case false:
|
2018-06-22 05:15:04 +00:00
|
|
|
lastBit = lastBit.slice(0, -2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parseMCI(lastBit);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('complete');
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
self.parse = function(buffer, savedRe) {
|
|
|
|
// :TODO: ensure this conforms to ANSI-BBS / CTerm / bansi.txt for movement/etc.
|
|
|
|
// :TODO: move this to "constants" section @ top
|
|
|
|
var re = /(?:\x1b\x5b)([\?=;0-9]*?)([ABCDHJKfhlmnpsu])/g;
|
|
|
|
var pos = 0;
|
|
|
|
var match;
|
|
|
|
var opCode;
|
|
|
|
var args;
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ignore anything past EOF marker, if any
|
|
|
|
buffer = buffer.split(String.fromCharCode(0x1a), 1)[0];
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
do {
|
|
|
|
pos = re.lastIndex;
|
|
|
|
match = re.exec(buffer);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
if(null !== match) {
|
|
|
|
if(match.index > pos) {
|
|
|
|
parseMCI(buffer.slice(pos, match.index));
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
opCode = match[2];
|
|
|
|
args = getArgArray(match[1].split(';'));
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
escape(opCode, args);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
self.emit('chunk', match[0]);
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-05-03 23:35:55 +00:00
|
|
|
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
} while(0 !== re.lastIndex);
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
if(pos < buffer.length) {
|
|
|
|
parseMCI(buffer.slice(pos));
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
self.emit('complete');
|
|
|
|
};
|
|
|
|
*/
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
function escape(opCode, args) {
|
|
|
|
let arg;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (opCode) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// cursor up
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'A':
|
2018-06-22 05:15:04 +00:00
|
|
|
//arg = args[0] || 1;
|
|
|
|
arg = isNaN(args[0]) ? 1 : args[0];
|
|
|
|
self.moveCursor(0, -arg);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// cursor down
|
|
|
|
case 'B':
|
2018-06-22 05:15:04 +00:00
|
|
|
//arg = args[0] || 1;
|
|
|
|
arg = isNaN(args[0]) ? 1 : args[0];
|
|
|
|
self.moveCursor(0, arg);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// cursor forward/right
|
|
|
|
case 'C':
|
2018-06-22 05:15:04 +00:00
|
|
|
//arg = args[0] || 1;
|
|
|
|
arg = isNaN(args[0]) ? 1 : args[0];
|
|
|
|
self.moveCursor(arg, 0);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// cursor back/left
|
|
|
|
case 'D':
|
2018-06-22 05:15:04 +00:00
|
|
|
//arg = args[0] || 1;
|
|
|
|
arg = isNaN(args[0]) ? 1 : args[0];
|
|
|
|
self.moveCursor(-arg, 0);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'f': // horiz & vertical
|
|
|
|
case 'H': // cursor position
|
2018-06-23 03:26:46 +00:00
|
|
|
//self.row = args[0] || 1;
|
|
|
|
//self.column = args[1] || 1;
|
2022-06-05 20:04:25 +00:00
|
|
|
self.row = isNaN(args[0]) ? 1 : args[0];
|
2018-06-23 03:26:46 +00:00
|
|
|
self.column = isNaN(args[1]) ? 1 : args[1];
|
2018-06-22 05:15:04 +00:00
|
|
|
//self.rowUpdated();
|
|
|
|
self.positionUpdated();
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// save position
|
|
|
|
case 's':
|
2018-06-22 05:15:04 +00:00
|
|
|
self.saveCursorPosition();
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// restore position
|
|
|
|
case 'u':
|
2018-06-22 05:15:04 +00:00
|
|
|
self.restoreCursorPosition();
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// set graphic rendition
|
|
|
|
case 'm':
|
2018-06-22 05:15:04 +00:00
|
|
|
self.graphicRendition.reset = false;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
for (let i = 0, len = args.length; i < len; ++i) {
|
2018-06-22 05:15:04 +00:00
|
|
|
arg = args[i];
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (ANSIEscapeParser.foregroundColors[arg]) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.graphicRendition.fg = arg;
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (ANSIEscapeParser.backgroundColors[arg]) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.graphicRendition.bg = arg;
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (ANSIEscapeParser.styles[arg]) {
|
|
|
|
switch (arg) {
|
|
|
|
case 0:
|
2018-06-23 03:26:46 +00:00
|
|
|
// clear out everything
|
2018-06-22 05:15:04 +00:00
|
|
|
delete self.graphicRendition.intensity;
|
|
|
|
delete self.graphicRendition.underline;
|
|
|
|
delete self.graphicRendition.blink;
|
|
|
|
delete self.graphicRendition.negative;
|
|
|
|
delete self.graphicRendition.invisible;
|
|
|
|
|
|
|
|
delete self.graphicRendition.fg;
|
|
|
|
delete self.graphicRendition.bg;
|
|
|
|
|
|
|
|
self.graphicRendition.reset = true;
|
|
|
|
//self.graphicRendition.fg = 39;
|
|
|
|
//self.graphicRendition.bg = 49;
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 22:
|
2018-06-22 05:15:04 +00:00
|
|
|
self.graphicRendition.intensity = arg;
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 4:
|
|
|
|
case 24:
|
2018-06-22 05:15:04 +00:00
|
|
|
self.graphicRendition.underline = arg;
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 25:
|
2018-06-22 05:15:04 +00:00
|
|
|
self.graphicRendition.blink = arg;
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 7:
|
|
|
|
case 27:
|
2018-06-22 05:15:04 +00:00
|
|
|
self.graphicRendition.negative = arg;
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 8:
|
|
|
|
case 28:
|
2018-06-22 05:15:04 +00:00
|
|
|
self.graphicRendition.invisible = arg;
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
|
|
|
Log.trace(
|
|
|
|
{ attribute: arg },
|
|
|
|
'Unknown attribute while parsing ANSI'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('sgr update', self.graphicRendition);
|
2022-06-05 20:04:25 +00:00
|
|
|
break; // m
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// :TODO: s, u, K
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// erase display/screen
|
|
|
|
case 'J':
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Handle other 'J' types!
|
2022-06-05 20:04:25 +00:00
|
|
|
if (2 === args[0]) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.clearScreen();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 04:42:22 +00:00
|
|
|
util.inherits(ANSIEscapeParser, events.EventEmitter);
|
|
|
|
|
|
|
|
ANSIEscapeParser.foregroundColors = {
|
2022-06-05 20:04:25 +00:00
|
|
|
30: 'black',
|
|
|
|
31: 'red',
|
|
|
|
32: 'green',
|
|
|
|
33: 'yellow',
|
|
|
|
34: 'blue',
|
|
|
|
35: 'magenta',
|
|
|
|
36: 'cyan',
|
|
|
|
37: 'white',
|
|
|
|
39: 'default', // same as white for most implementations
|
|
|
|
|
|
|
|
90: 'grey',
|
2015-04-29 04:42:22 +00:00
|
|
|
};
|
|
|
|
Object.freeze(ANSIEscapeParser.foregroundColors);
|
|
|
|
|
|
|
|
ANSIEscapeParser.backgroundColors = {
|
2022-06-05 20:04:25 +00:00
|
|
|
40: 'black',
|
|
|
|
41: 'red',
|
|
|
|
42: 'green',
|
|
|
|
43: 'yellow',
|
|
|
|
44: 'blue',
|
|
|
|
45: 'magenta',
|
|
|
|
46: 'cyan',
|
|
|
|
47: 'white',
|
|
|
|
49: 'default', // same as black for most implementations
|
2015-04-29 04:42:22 +00:00
|
|
|
};
|
|
|
|
Object.freeze(ANSIEscapeParser.backgroundColors);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: ensure these names all align with that of ansi_term.js
|
2015-04-30 20:39:03 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// 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
|
2015-04-30 20:39:03 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Note that these are intentionally not in order such that they
|
|
|
|
// can be grouped by concept here in code.
|
2015-04-30 20:39:03 +00:00
|
|
|
//
|
2015-04-29 04:42:22 +00:00
|
|
|
ANSIEscapeParser.styles = {
|
2022-06-05 20:04:25 +00:00
|
|
|
0: 'default', // Everything disabled
|
2015-04-30 20:39:03 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
1: 'intensityBright', // aka bold
|
|
|
|
2: 'intensityDim',
|
|
|
|
22: 'intensityNormal',
|
2015-04-30 20:39:03 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
4: 'underlineOn', // Not supported by most BBS-like terminals
|
|
|
|
24: 'underlineOff', // Not supported by most BBS-like terminals
|
2015-04-30 20:39:03 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
5: 'blinkSlow', // blinkSlow & blinkFast are generally treated the same
|
|
|
|
6: 'blinkFast', // blinkSlow & blinkFast are generally treated the same
|
|
|
|
25: 'blinkOff',
|
2015-04-30 20:39:03 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
7: 'negativeImageOn', // Generally not supported or treated as "reverse FG & BG"
|
|
|
|
27: 'negativeImageOff', // Generally not supported or treated as "reverse FG & BG"
|
2015-04-30 20:39:03 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
8: 'invisibleOn', // FG set to BG
|
|
|
|
28: 'invisibleOff', // Not supported by most BBS-like terminals
|
2015-04-29 04:42:22 +00:00
|
|
|
};
|
2016-05-24 02:45:18 +00:00
|
|
|
Object.freeze(ANSIEscapeParser.styles);
|