github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/cmd/droned/assets/js/line_formatter.js (about)

     1  ;// Format ANSI to HTML
     2  
     3  if(typeof(Drone) === 'undefined') { Drone = {}; }
     4  
     5  (function() {
     6  	Drone.LineFormatter = function() {};
     7  
     8  	Drone.LineFormatter.prototype = {
     9  		regex: /\u001B\[([0-9]+;?)*[Km]/g,
    10  		styles: [],
    11  
    12  		format: function(s) {
    13  			// Check for newline and early exit?
    14  			s = s.replace(/</g, "&lt;");
    15  			s = s.replace(/>/g, "&gt;");
    16  
    17  			var output = "";
    18  			var current = 0;
    19  			while (m = this.regex.exec(s)) {
    20  				var part = s.substring(current, m.index);
    21  				current = this.regex.lastIndex;
    22  
    23  				var token = s.substr(m.index, this.regex.lastIndex - m.index);
    24  				var code = token.substr(2, token.length-2);
    25  
    26  				var pre = "";
    27  				var post = "";
    28  
    29  				switch (code) {
    30  					case 'm':
    31  					case '0m':
    32  						var len = this.styles.length;
    33  						for (var i=0; i < len; i++) {
    34  							this.styles.pop();
    35  							post += "</span>"
    36  						}
    37  						break;
    38  					case '30;42m': pre = '<span style="color:black;background:lime">'; break;
    39  					case '36m':
    40  					case '36;1m': pre = '<span style="color:cyan;">'; break;
    41  					case '31m':
    42  					case '31;31m': pre = '<span style="color:red;">'; break;
    43  					case '33m':
    44  					case '33;33m': pre = '<span style="color:yellow;">'; break;
    45  					case '32m':
    46  					case '0;32m': pre = '<span style="color:lime;">'; break;
    47  					case '90m': pre = '<span style="color:gray;">'; break;
    48  					case 'K':
    49  					case '0K':
    50  					case '1K':
    51  					case '2K': break;
    52  				}
    53  
    54  				if (pre !== "") {
    55  					this.styles.push(pre);
    56  				}
    57  
    58  				output += part + pre + post;
    59  			}
    60  
    61  			var part = s.substring(current, s.length);
    62  			output += part;
    63  			return output;
    64  		}
    65  	};
    66  })();