Permalink
Browse files

[api] Basic version, can deal with simple colors

  • Loading branch information...
1 parent 68a7e42 commit 44bebdd1c0c4dfecc301eeb8b44959a06a865beb @mmalecki committed Nov 27, 2011
Showing with 29 additions and 0 deletions.
  1. +29 −0 lib/ansispan.js
View
@@ -0,0 +1,29 @@
+var ansispan = module.exports = function (str) {
+ Object.keys(ansispan.foregroundColors).forEach(function (ansi) {
+ var span = '<span style="color: ' + ansispan.foregroundColors[ansi] + '">';
+
+ //
+ // `\033[Xm` == `\033[0;Xm` sets foreground color to `X`.
+ //
+
+ str = str.replace(
+ new RegExp('\033\\[' + ansi + 'm', 'g'),
+ span
+ ).replace(
+ new RegExp('\033\\[0;' + ansi + 'm', 'g'),
+ span
+ );
+ });
+ return str.replace(/\033\[39m/g, '</span>');
+};
+
+ansispan.foregroundColors = {
+ '30': 'black',
+ '31': 'red',
+ '32': 'green',
+ '33': 'yellow',
+ '35': 'purple',
+ '36': 'cyan',
+ '37': 'white'
+};
+

0 comments on commit 44bebdd

Please sign in to comment.