Skip to content

Instantly share code, notes, and snippets.

@qubyte
Last active December 14, 2015 03:18
Show Gist options
  • Save qubyte/5019456 to your computer and use it in GitHub Desktop.
Save qubyte/5019456 to your computer and use it in GitHub Desktop.
Ever wanted to wrap your node.js console output in some visual chrome? This function automates it for you!

Description

Ever wanted to wrap your node.js console output in some visual chrome? This function automates it for you! It indents your text by one space, and centres the chrome above and below. Even for multiline strings the chrome length will be properly calculated using ninja disco rocket boots (or Math.max.apply on the array if the rocket boots are on loan). As a bonus, the third parameter takes a character to put at the start of your string, like an arrow or something.

The function

/**
 * Wrap a sting with visual chrome above and below. Automatic length.
 *
 * @param  {String} basic      A basic string to wrap. No whitespace padding needed.
 * @param  {String} chromeChar A character to use for the chrome.
 * @param  {String} special    A special character to come before basic.
 * @return {String}            Resultant string.
 */

function chromify(basic, chromeChar, special) {
    basic = special && special.length ? special + ' ' + basic : basic;

    var maxLength = Math.max.apply(null, basic.split('\n').map(function (subString) {
        return subString.length;
    }));

    var chrome = Array(maxLength + 3).join(chromeChar);
    return '\n' +  chrome + '\n ' + basic.replace(/\n/g, '\n ') + '\n' + chrome + '\n';
}

Example

The following

console.log(chromify('Hello world!', '=', '>'));

results in

================
 > Hello world!
================

A multiline comment is also properly handled.

var e = new Error('ouch...');

console.log(chromify(e.stack, '#', 'x'))

results in something like

########################################################
 x Error: ouch...
     at repl:1:6
     at REPLServer.self.eval (repl.js:109:21)
     at Interface.<anonymous> (repl.js:248:12)
     at Interface.EventEmitter.emit (events.js:96:17)
     at Interface._onLine (readline.js:200:10)
     at Interface._line (readline.js:518:8)
     at Interface._ttyWrite (readline.js:736:14)
     at ReadStream.onkeypress (readline.js:97:10)
     at ReadStream.EventEmitter.emit (events.js:126:20)
     at emitKey (readline.js:1058:12)
     at ReadStream.onData (readline.js:807:7)
     at ReadStream.EventEmitter.emit (events.js:96:17)
########################################################

Licence

Copyright (c) 2013 Mark Stanley Everitt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment