WIP add in some basic movement MCI codes & color/pipe code cleanup

This commit is contained in:
Bryan Ashby 2018-09-14 20:34:39 -06:00
parent e6055e0f11
commit 9bb3557509
4 changed files with 44 additions and 98 deletions

View File

@ -3,7 +3,6 @@
// ENiGMA½ // ENiGMA½
var Log = require('./logger.js').log; var Log = require('./logger.js').log;
var enigmaToAnsi = require('./color_codes.js').enigmaToAnsi;
var renegadeToAnsi = require('./color_codes.js').renegadeToAnsi; var renegadeToAnsi = require('./color_codes.js').renegadeToAnsi;
var iconv = require('iconv-lite'); var iconv = require('iconv-lite');
@ -174,15 +173,8 @@ ClientTerminal.prototype.rawWrite = function(s, cb) {
} }
}; };
ClientTerminal.prototype.pipeWrite = function(s, spec, cb) { ClientTerminal.prototype.pipeWrite = function(s, cb) {
spec = spec || 'renegade'; this.write(renegadeToAnsi(s, this), null, cb); // null = use default for |convertLineFeeds|
var conv = {
enigma : enigmaToAnsi,
renegade : renegadeToAnsi,
}[spec] || renegadeToAnsi;
this.write(conv(s, this), null, cb); // null = use default for |convertLineFeeds|
}; };
ClientTerminal.prototype.encode = function(s, convertLineFeeds) { ClientTerminal.prototype.encode = function(s, convertLineFeeds) {

View File

@ -1,13 +1,12 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
var ansi = require('./ansi_term.js'); const ANSI = require('./ansi_term.js');
var getPredefinedMCIValue = require('./predefined_mci.js').getPredefinedMCIValue; const { getPredefinedMCIValue } = require('./predefined_mci.js');
var assert = require('assert'); // deps
var _ = require('lodash'); const _ = require('lodash');
exports.enigmaToAnsi = enigmaToAnsi;
exports.stripPipeCodes = exports.stripEnigmaCodes = stripEnigmaCodes; exports.stripPipeCodes = exports.stripEnigmaCodes = stripEnigmaCodes;
exports.pipeStrLen = exports.enigmaStrLen = enigmaStrLen; exports.pipeStrLen = exports.enigmaStrLen = enigmaStrLen;
exports.pipeToAnsi = exports.renegadeToAnsi = renegadeToAnsi; exports.pipeToAnsi = exports.renegadeToAnsi = renegadeToAnsi;
@ -15,70 +14,6 @@ exports.controlCodesToAnsi = controlCodesToAnsi;
// :TODO: Not really happy with the module name of "color_codes". Would like something better // :TODO: Not really happy with the module name of "color_codes". Would like something better
// Also add:
// * fromCelerity(): |<case sensitive letter>
// * fromPCBoard(): (@X<bg><fg>)
// * fromWildcat(): (@<bg><fg>@ (same as PCBoard without 'X' prefix and '@' suffix)
// * fromWWIV(): <ctrl-c><0-7>
// * fromSyncronet(): <ctrl-a><colorCode>
// See http://wiki.synchro.net/custom:colors
// :TODO: rid of enigmaToAnsi() -- never really use. Instead, create bbsToAnsi() that supports renegade, PCB, WWIV, etc...
function enigmaToAnsi(s, client) {
if(-1 == s.indexOf('|')) {
return s; // no pipe codes present
}
var result = '';
var re = /\|([A-Z\d]{2}|\|)/g;
var m;
var lastIndex = 0;
while((m = re.exec(s))) {
var val = m[1];
if('|' == val) {
result += '|';
continue;
}
// convert to number
val = parseInt(val, 10);
if(isNaN(val)) {
//
// ENiGMA MCI code? Only available if |client|
// is supplied.
//
val = getPredefinedMCIValue(client, m[1]) || ('|' + m[1]); // value itself or literal
}
if(_.isString(val)) {
result += s.substr(lastIndex, m.index - lastIndex) + val;
} else {
assert(val >= 0 && val <= 47);
var attr = '';
if(7 == val) {
attr = ansi.sgr('normal');
} else if (val < 7 || val >= 16) {
attr = ansi.sgr(['normal', val]);
} else if (val <= 15) {
attr = ansi.sgr(['normal', val - 8, 'bold']);
}
result += s.substr(lastIndex, m.index - lastIndex) + attr;
}
lastIndex = re.lastIndex;
}
result = (0 === result.length ? s : result + s.substr(lastIndex));
return result;
}
function stripEnigmaCodes(s) { function stripEnigmaCodes(s) {
return s.replace(/\|[A-Z\d]{2}/g, ''); return s.replace(/\|[A-Z\d]{2}/g, '');
} }
@ -88,7 +23,7 @@ function enigmaStrLen(s) {
} }
function ansiSgrFromRenegadeColorCode(cc) { function ansiSgrFromRenegadeColorCode(cc) {
return ansi.sgr({ return ANSI.sgr({
0 : [ 'reset', 'black' ], 0 : [ 'reset', 'black' ],
1 : [ 'reset', 'blue' ], 1 : [ 'reset', 'blue' ],
2 : [ 'reset', 'green' ], 2 : [ 'reset', 'green' ],
@ -132,10 +67,11 @@ function renegadeToAnsi(s, client) {
return s; // no pipe codes present return s; // no pipe codes present
} }
var result = ''; let result = '';
var re = /\|([A-Z\d]{2}|\|)/g; const re = /\|([A-Z\d]{2}|\|)/g;
var m; //const re = /\|(?:(C[FBUD])([0-9]{1,2})|([A-Z\d]{2})|(\|))/g;
var lastIndex = 0; let m;
let lastIndex = 0;
while((m = re.exec(s))) { while((m = re.exec(s))) {
var val = m[1]; var val = m[1];
@ -192,7 +128,7 @@ function controlCodesToAnsi(s, client) {
while((m = RE.exec(s))) { while((m = RE.exec(s))) {
switch(m[0].charAt(0)) { switch(m[0].charAt(0)) {
case '|' : case '|' :
// Renegade or ENiGMA MCI // Renegade |##
v = parseInt(m[2], 10); v = parseInt(m[2], 10);
if(isNaN(v)) { if(isNaN(v)) {
@ -256,17 +192,18 @@ function controlCodesToAnsi(s, client) {
F : [ 'bold', 'whiteBG' ], F : [ 'bold', 'whiteBG' ],
}[v.charAt(1)] || [ 'normal' ]; }[v.charAt(1)] || [ 'normal' ];
v = ansi.sgr(fg.concat(bg)); v = ANSI.sgr(fg.concat(bg));
result += s.substr(lastIndex, m.index - lastIndex) + v; result += s.substr(lastIndex, m.index - lastIndex) + v;
break; break;
case '\x03' : case '\x03' :
// WWIV
v = parseInt(m[8], 10); v = parseInt(m[8], 10);
if(isNaN(v)) { if(isNaN(v)) {
v += m[0]; v += m[0];
} else { } else {
v = ansi.sgr({ v = ANSI.sgr({
0 : [ 'reset', 'black' ], 0 : [ 'reset', 'black' ],
1 : [ 'bold', 'cyan' ], 1 : [ 'bold', 'cyan' ],
2 : [ 'bold', 'yellow' ], 2 : [ 'bold', 'yellow' ],

View File

@ -37,6 +37,10 @@ MCIViewFactory.UserViewCodes = [
'XY', 'XY',
]; ];
MCIViewFactory.MovementCodes = [
'CF', 'CB', 'CU', 'CD',
];
MCIViewFactory.prototype.createFromMCI = function(mci) { MCIViewFactory.prototype.createFromMCI = function(mci) {
assert(mci.code); assert(mci.code);
assert(mci.id > 0); assert(mci.id > 0);
@ -192,14 +196,16 @@ MCIViewFactory.prototype.createFromMCI = function(mci) {
break; break;
default : default :
options.text = getPredefinedMCIValue(this.client, mci.code); if(!MCIViewFactory.MovementCodes.includes(mci.code)) {
if(_.isString(options.text)) { options.text = getPredefinedMCIValue(this.client, mci.code);
setWidth(0); if(_.isString(options.text)) {
setWidth(0);
setOption(1, 'textStyle'); setOption(1, 'textStyle');
setOption(2, 'justify'); setOption(2, 'justify');
view = new TextView(options); view = new TextView(options);
}
} }
break; break;
} }

View File

@ -4,12 +4,15 @@
// ENiGMA½ // ENiGMA½
const Config = require('./config.js').get; const Config = require('./config.js').get;
const Log = require('./logger.js').log; const Log = require('./logger.js').log;
const getMessageAreaByTag = require('./message_area.js').getMessageAreaByTag; const {
const getMessageConferenceByTag = require('./message_area.js').getMessageConferenceByTag; getMessageAreaByTag,
getMessageConferenceByTag
} = require('./message_area.js');
const clientConnections = require('./client_connections.js'); const clientConnections = require('./client_connections.js');
const StatLog = require('./stat_log.js'); const StatLog = require('./stat_log.js');
const FileBaseFilters = require('./file_base_filter.js'); const FileBaseFilters = require('./file_base_filter.js');
const formatByteSize = require('./string_util.js').formatByteSize; const { formatByteSize } = require('./string_util.js');
const ANSI = require('./ansi_term.js');
// deps // deps
const packageJson = require('../package.json'); const packageJson = require('../package.json');
@ -227,9 +230,17 @@ const PREDEFINED_MCI_GENERATORS = {
// Special handling for XY // Special handling for XY
// //
XY : function xyHack() { return; /* nothing */ }, XY : function xyHack() { return; /* nothing */ },
//
// Various movement by N
//
CF : function cursorForwardBy(client, n = 1) { return ANSI.forward(n); },
CB : function cursorBackBy(client, n = 1) { return ANSI.back(n); },
CU : function cursorUpBy(client, n = 1) { return ANSI.up(n); },
CD : function cursorDownBy(client, n = 1) { return ANSI.down(n); },
}; };
function getPredefinedMCIValue(client, code) { function getPredefinedMCIValue(client, code, extra) {
if(!client || !code) { if(!client || !code) {
return; return;
@ -240,7 +251,7 @@ function getPredefinedMCIValue(client, code) {
if(generator) { if(generator) {
let value; let value;
try { try {
value = generator(client); value = generator(client, extra);
} catch(e) { } catch(e) {
Log.error( { code : code, exception : e.message }, 'Exception caught generating predefined MCI value' ); Log.error( { code : code, exception : e.message }, 'Exception caught generating predefined MCI value' );
} }