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',
@ -267,8 +271,8 @@ var FONT_ALIAS_TO_SYNCTERM_MAP = {
'amiga_p0t-noodle' : 'pot_noodle', 'amiga_p0t-noodle' : 'pot_noodle',
'mo_soul' : 'mo_soul', 'mo_soul' : 'mo_soul',
'mosoul' : 'mo_soul', 'mosoul' : 'mo_soul',
'mO\'sOul' : 'mo_soul', 'mO\'sOul' : 'mo_soul',
'amiga_microknight' : 'microknight', 'amiga_microknight' : 'microknight',
'amiga_microknight+' : 'microknight_plus', 'amiga_microknight+' : 'microknight_plus',
@ -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,20 +300,20 @@ 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,
'blinking underline' : 3, 'blinking underline' : 3,
'steady underline' : 4, 'steady underline' : 4,
'blinking bar' : 5, 'blinking bar' : 5,
'steady bar' : 6, 'steady bar' : 6,
}; };
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`;
}; };
}); });
@ -347,28 +351,19 @@ function sgr() {
if(arguments.length <= 0) { if(arguments.length <= 0) {
return ''; return '';
} }
var result = '';
// :TODO: this method needs a lot of cleanup! let result = [];
const args = Array.isArray(arguments[0]) ? arguments[0] : arguments;
var args = Array.isArray(arguments[0]) ? arguments[0] : arguments; args.forEach(arg => {
for(var i = 0; i < args.length; i++) { if(_.isString(arg) && arg in SGRValues) {
if(typeof args[i] === 'string') { result.push(SGRValues[arg]);
if(args[i] in SGRValues) { } else if(_.isNumber(arg)) {
if(result.length > 0) { result.push(arg);
result += ';';
}
result += SGRValues[args[i]];
}
} else if(typeof args[i] === 'number') {
if(result.length > 0) {
result += ';';
}
result += args[i];
} }
} });
return ESC_CSI + result + 'm';
return `${ESC_CSI}${result.join(';')}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,11 +409,11 @@ 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() {
return sgr(['normal', 'reset']); return sgr( [ 'normal', 'reset' ] );
} }
function goHome() { function goHome() {
@ -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,