Created
October 30, 2012 13:27
-
-
Save clochix/3980167 to your computer and use it in GitHub Desktop.
JavaScript padding tolerant to ANSI escape sequences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Display a string with padding | |
* | |
* @param {String} str String to pad | |
* @param {Integer} l padding length | |
* @param {Boolean} [r] true for right padding | |
* | |
* @return {String} | |
*/ | |
function pad(str, l, r) { | |
"use strict"; | |
var tmp = new Array(l).join(' '); | |
str = '' + str; | |
var strClean = str.replace(/\u001b\[[^m]+m/g, ''); | |
return r ? tmp.substr(0, l - strClean.length) + str.substr(0, l + str.length - strClean.length) : str.substr(0, l + str.length - strClean.length) + tmp.substr(0, l - strClean.length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment