From c87c0d69b7c41b436d7e771e63620b0c6d320a5c Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Tue, 30 Jun 2015 13:04:58 -0600 Subject: [PATCH] * Some work on color codes module --- core/ansi_term.js | 52 ---------------------------------- core/color_codes.js | 68 +++++++++++++++++++++++++++++++++++++++++++++ core/connect.js | 7 +++-- 3 files changed, 73 insertions(+), 54 deletions(-) create mode 100644 core/color_codes.js diff --git a/core/ansi_term.js b/core/ansi_term.js index 4dd7846d..338bb74c 100644 --- a/core/ansi_term.js +++ b/core/ansi_term.js @@ -29,7 +29,6 @@ exports.disableVT100LineWrapping = disableVT100LineWrapping; exports.setSyncTERMFont = setSyncTERMFont; exports.getSyncTERMFontFromAlias = getSyncTERMFontFromAlias; exports.setCursorStyle = setCursorStyle; -exports.fromPipeCode = fromPipeCode; // @@ -394,54 +393,3 @@ function goHome() { function disableVT100LineWrapping() { return ESC_CSI + '7l'; } - -// Also add: -// * fromRenegade(): |<0-23> -// * fromCelerity(): | -// * fromPCBoard(): (@X@) -// * fromWildcat(): (@@ (same as PCBoard without 'X' prefix) -// * fromWWIV(): <0-7> -// * fromSyncronet(): -// See http://wiki.synchro.net/custom:colors -function fromPipeCode(s) { - if(-1 == s.indexOf('|')) { - return s; // no pipe codes present - } - - var result = ''; - var re = /\|(\d{2,3}|\|)/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)) { - val = 0; - } - - assert(val >= 0 && val <= 256); - - var attr = ''; - if(7 == val) { - attr = sgr('normal'); - } else if (val < 7 || val >= 16) { - attr = sgr(['normal', val]); - } else if (val <= 15) { - attr = 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; -} \ No newline at end of file diff --git a/core/color_codes.js b/core/color_codes.js new file mode 100644 index 00000000..4e5a46f5 --- /dev/null +++ b/core/color_codes.js @@ -0,0 +1,68 @@ +/* jslint node: true */ +'use strict'; + +var ansi = require('./ansi_term.js'); + +var assert = require('assert'); + +exports.pipeToAnsi = exports.renegadeToAnsi = renegadeToAnsi; +exports.stripPipeCodes = exports.stripRenegadeCodes = stripRenegadeCodes; + +// :TODO: Not really happy with the module name of "color_codes". Would like something better + + + +// Also add: +// * fromCelerity(): | +// * fromPCBoard(): (@X) +// * fromWildcat(): (@@ (same as PCBoard without 'X' prefix and '@' suffix) +// * fromWWIV(): <0-7> +// * fromSyncronet(): +// See http://wiki.synchro.net/custom:colors +function renegadeToAnsi(s) { + if(-1 == s.indexOf('|')) { + return s; // no pipe codes present + } + + var result = ''; + var re = /\|(\d{2,3}|\|)/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)) { + val = 0; + } + + assert(val >= 0 && val <= 256); + + 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 stripRenegadeCodes(s) { + return s.replace(/\|[\d]{2,3}/g, ''); +} + diff --git a/core/connect.js b/core/connect.js index 074dfabd..3e8a5ee6 100644 --- a/core/connect.js +++ b/core/connect.js @@ -2,11 +2,13 @@ 'use strict'; var ansi = require('./ansi_term.js'); -//var artwork = require('./art.js'); +var colorCodes = require('./color_codes.js'); var theme = require('./theme.js'); var moduleUtil = require('./module_util.js'); var Log = require('./logger.js').log; var Config = require('./config.js').config; + + var packageJson = require('../package.json'); var assert = require('assert'); @@ -77,7 +79,8 @@ function prepareTerminal(term) { function displayBanner(term) { // :TODO: add URL(s) to banner - term.write(ansi.fromPipeCode(util.format('' + + //term.write(ansi.fromPipeCode(util.format('' + + term.write(colorCodes.pipeToAnsi(util.format( '|33Conected to |32EN|33|01i|00|32|22GMA|32|01½|00 |33BBS version|31|01 %s\n' + '|00|33Copyright (c) 2014-2015 Bryan Ashby\n' + '|00', packageJson.version)));