Various ES6, TODO and code cleanup

This commit is contained in:
Bryan Ashby 2016-06-26 21:23:59 -06:00
parent 3955d5539f
commit b6cada6f3c
1 changed files with 57 additions and 53 deletions

View File

@ -12,9 +12,13 @@
// * http://www.inwap.com/pdp10/ansicode.txt // * http://www.inwap.com/pdp10/ansicode.txt
// //
const assert = require('assert'); // ENiGMA½
const miscUtil = require('./misc_util.js'); const miscUtil = require('./misc_util.js');
// deps
const assert = require('assert');
const _ = require('lodash');
exports.getFGColorValue = getFGColorValue; exports.getFGColorValue = getFGColorValue;
exports.getBGColorValue = getBGColorValue; exports.getBGColorValue = getBGColorValue;
exports.sgr = sgr; exports.sgr = sgr;
@ -35,9 +39,9 @@ exports.setEmulatedBaudRate = setEmulatedBaudRate;
// See also // See also
// https://github.com/TooTallNate/ansi.js/blob/master/lib/ansi.js // https://github.com/TooTallNate/ansi.js/blob/master/lib/ansi.js
var ESC_CSI = '\u001b['; const ESC_CSI = '\u001b[';
var CONTROL = { const CONTROL = {
up : 'A', up : 'A',
down : 'B', down : 'B',
@ -124,7 +128,7 @@ var CONTROL = {
// Select Graphics Rendition // Select Graphics Rendition
// See http://cvs.synchro.net/cgi-bin/viewcvs.cgi/*checkout*/src/conio/cterm.txt // See http://cvs.synchro.net/cgi-bin/viewcvs.cgi/*checkout*/src/conio/cterm.txt
// //
var SGRValues = { const SGRValues = {
reset : 0, reset : 0,
bold : 1, bold : 1,
dim : 2, dim : 2,
@ -180,7 +184,7 @@ function getBGColorValue(name) {
// //
// See https://github.com/protomouse/synchronet/blob/master/src/conio/cterm.txt // See https://github.com/protomouse/synchronet/blob/master/src/conio/cterm.txt
// //
var SYNCTERM_FONT_AND_ENCODING_TABLE = [ const SYNCTERM_FONT_AND_ENCODING_TABLE = [
'cp437', 'cp437',
'cp1251', 'cp1251',
'koi8_r', 'koi8_r',
@ -233,7 +237,7 @@ var SYNCTERM_FONT_AND_ENCODING_TABLE = [
// This table contains lowercased entries with any spaces // This table contains lowercased entries with any spaces
// replaced with '_' for lookup purposes. // replaced with '_' for lookup purposes.
// //
var FONT_ALIAS_TO_SYNCTERM_MAP = { const FONT_ALIAS_TO_SYNCTERM_MAP = {
'cp437' : 'cp437', 'cp437' : 'cp437',
'ibm_vga' : 'cp437', 'ibm_vga' : 'cp437',
'ibmpc' : 'cp437', 'ibmpc' : 'cp437',
@ -280,13 +284,13 @@ var FONT_ALIAS_TO_SYNCTERM_MAP = {
}; };
function setSyncTERMFont(name, fontPage) { function setSyncTERMFont(name, fontPage) {
var p1 = miscUtil.valueWithDefault(fontPage, 0); const p1 = miscUtil.valueWithDefault(fontPage, 0);
assert(p1 >= 0 && p1 <= 3); assert(p1 >= 0 && p1 <= 3);
var p2 = SYNCTERM_FONT_AND_ENCODING_TABLE.indexOf(name); const p2 = SYNCTERM_FONT_AND_ENCODING_TABLE.indexOf(name);
if(p2 > -1) { if(p2 > -1) {
return ESC_CSI + p1 + ';' + p2 + ' D'; return `${ESC_CSI}${p1};${p2} D`;
} }
return ''; return '';
@ -296,7 +300,7 @@ function getSyncTERMFontFromAlias(alias) {
return FONT_ALIAS_TO_SYNCTERM_MAP[alias.toLowerCase().replace(/ /g, '_')]; return FONT_ALIAS_TO_SYNCTERM_MAP[alias.toLowerCase().replace(/ /g, '_')];
} }
var DEC_CURSOR_STYLE = { const DEC_CURSOR_STYLE = {
'blinking block' : 0, 'blinking block' : 0,
'default' : 1, 'default' : 1,
'steady block' : 2, 'steady block' : 2,
@ -307,9 +311,9 @@ var DEC_CURSOR_STYLE = {
}; };
function setCursorStyle(cursorStyle) { function setCursorStyle(cursorStyle) {
var ps = DEC_CURSOR_STYLE[cursorStyle]; const ps = DEC_CURSOR_STYLE[cursorStyle];
if(ps) { if(ps) {
return ESC_CSI + ps + ' q'; return `${ESC_CSI}${ps} q`;
} }
return ''; return '';
@ -317,24 +321,24 @@ function setCursorStyle(cursorStyle) {
// Create methods such as up(), nextLine(),... // Create methods such as up(), nextLine(),...
Object.keys(CONTROL).forEach(function onControlName(name) { Object.keys(CONTROL).forEach(function onControlName(name) {
var code = CONTROL[name]; const code = CONTROL[name];
exports[name] = function() { exports[name] = function() {
var c = code; let c = code;
if(arguments.length > 0) { if(arguments.length > 0) {
// arguments are array like -- we want an array // arguments are array like -- we want an array
c = Array.prototype.slice.call(arguments).map(Math.round).join(';') + code; c = Array.prototype.slice.call(arguments).map(Math.round).join(';') + code;
} }
return ESC_CSI + c; return `${ESC_CSI}${c}`;
}; };
}); });
// Create various color methods such as white(), yellowBG(), reset(), ... // Create various color methods such as white(), yellowBG(), reset(), ...
Object.keys(SGRValues).forEach(function onSgrName(name) { Object.keys(SGRValues).forEach( name => {
var code = SGRValues[name]; const code = SGRValues[name];
exports[name] = function() { exports[name] = function() {
return ESC_CSI + code + 'm'; return `${ESC_CSI}${code}m`;
}; };
}); });
@ -348,27 +352,18 @@ function sgr() {
return ''; return '';
} }
var result = ''; let result = [];
const args = Array.isArray(arguments[0]) ? arguments[0] : arguments;
// :TODO: this method needs a lot of cleanup! args.forEach(arg => {
if(_.isString(arg) && arg in SGRValues) {
result.push(SGRValues[arg]);
} else if(_.isNumber(arg)) {
result.push(arg);
}
});
var args = Array.isArray(arguments[0]) ? arguments[0] : arguments; return `${ESC_CSI}${result.join(';')}m`;
for(var i = 0; i < args.length; i++) {
if(typeof args[i] === 'string') {
if(args[i] in SGRValues) {
if(result.length > 0) {
result += ';';
}
result += SGRValues[args[i]];
}
} else if(typeof args[i] === 'number') {
if(result.length > 0) {
result += ';';
}
result += args[i];
}
}
return ESC_CSI + result + 'm';
} }
// //
@ -376,10 +371,10 @@ function sgr() {
// to a ANSI SGR sequence. // to a ANSI SGR sequence.
// //
function getSGRFromGraphicRendition(graphicRendition, initialReset) { function getSGRFromGraphicRendition(graphicRendition, initialReset) {
var sgrSeq = []; let sgrSeq = [];
let styleCount = 0;
var styleCount = 0; [ 'intensity', 'underline', 'blink', 'negative', 'invisible' ].forEach( s => {
[ 'intensity', 'underline', 'blink', 'negative', 'invisible' ].forEach(function style(s) {
if(graphicRendition[s]) { if(graphicRendition[s]) {
sgrSeq.push(graphicRendition[s]); sgrSeq.push(graphicRendition[s]);
++styleCount; ++styleCount;
@ -414,7 +409,7 @@ function clearScreen() {
} }
function resetScreen() { function resetScreen() {
return exports.reset() + exports.eraseData(2) + exports.goHome(); return `${exports.reset()}${exports.eraseData(2)}${exports.goHome()}`;
} }
function normal() { function normal() {
@ -452,14 +447,23 @@ function deleteLine(count) {
*/ */
// //
// See http://www.termsys.demon.co.uk/vtANSI_BBS.htm // Disable auto line wraping @ termWidth
//
// See:
// http://stjarnhimlen.se/snippets/vt100.txt
// https://github.com/protomouse/synchronet/blob/master/src/conio/cterm.txt
//
// WARNING:
// * Not honored by all clients
// * If it is honored, ANSI's that rely on this (e.g. do not have \r\n endings
// and use term width -- generally 80 columns -- will display garbled!
// //
function disableVT100LineWrapping() { function disableVT100LineWrapping() {
return ESC_CSI + '7l'; return `${ESC_CSI}?7l`;
} }
function setEmulatedBaudRate(rate) { function setEmulatedBaudRate(rate) {
var speed = { const speed = {
unlimited : 0, unlimited : 0,
off : 0, off : 0,
0 : 0, 0 : 0,