node-ansiparser
Version:
A parser for ANSI escape codes.
2 lines (1 loc) • 4.19 kB
JavaScript
!function(){"use strict";function t(t,s){for(var e=s-t,r=new Array(e);e--;)r[e]=--s;return r}function s(t,s,e,r,i){t[e<<8|s]=(0|r)<<4|(void 0===i?e:i)}function e(t,e,r,i,n){for(var a=0;a<e.length;a++)s(t,e[a],r,i,n)}function r(t){this.initial_state=0,this.current_state=0|this.initial_state,this.transitions=new Uint8Array(4095),this.transitions.set(a),this.osc="",this.params=[0],this.collected="",this.term=t||{};for(var s=["inst_p","inst_o","inst_x","inst_c","inst_e","inst_H","inst_P","inst_U","inst_E"],e=0;e<s.length;++e)s[e]in this.term||(this.term[s[e]]=function(){})}var i=t(32,127),n=t(0,24);n.push(25),n=n.concat(t(28,32));var a=function(){for(var r=new Uint8Array(4095),a=0;a<14;++a)for(var o=0;o<160;++o)r[a<<8|o]=16;for(e(r,i,0,2),a=0;a<14;++a)e(r,[24,26,153,154],a,3,0),e(r,t(128,144),a,3,0),e(r,t(144,152),a,3,0),s(r,156,a,0,0),s(r,27,a,11,1),s(r,157,a,4,8),e(r,[152,158,159],a,0,7),s(r,155,a,11,3),s(r,144,a,11,9);return e(r,n,0,3),e(r,n,1,3),s(r,127,1),e(r,n,8),e(r,n,3,3),s(r,127,3),e(r,n,4,3),s(r,127,4),e(r,n,6,3),e(r,n,5,3),s(r,127,5),e(r,n,2,3),s(r,127,2),s(r,93,1,4,8),e(r,i,8,5),s(r,127,8,5),e(r,[156,27,24,26,7],8,6,0),e(r,t(28,32),8,0),e(r,[88,94,95],1,0,7),e(r,i,7),e(r,n,7),s(r,127,7),s(r,156,7,0,0),s(r,91,1,11,3),e(r,t(64,127),3,7,0),e(r,t(48,58),3,8,4),s(r,59,3,8,4),e(r,[60,61,62,63],3,9,4),e(r,t(48,58),4,8),s(r,59,4,8),e(r,t(64,127),4,7,0),e(r,[58,60,61,62,63],4,0,6),e(r,t(32,64),6),s(r,127,6),e(r,t(64,127),6,0,0),s(r,58,3,0,6),e(r,t(32,48),3,9,5),e(r,t(32,48),5,9),e(r,t(48,64),5,0,6),e(r,t(64,127),5,7,0),e(r,t(32,48),4,9,5),e(r,t(32,48),1,9,2),e(r,t(32,48),2,9),e(r,t(48,127),2,10,0),e(r,t(48,80),1,10,0),e(r,[81,82,83,84,85,86,87,89,90,92],1,10,0),e(r,t(96,127),1,10,0),s(r,80,1,11,9),e(r,n,9),s(r,127,9),e(r,t(28,32),9),e(r,t(32,48),9,9,12),s(r,58,9,0,11),e(r,t(48,58),9,8,10),s(r,59,9,8,10),e(r,[60,61,62,63],9,9,10),e(r,n,11),e(r,t(32,128),11),e(r,t(28,32),11),e(r,n,10),s(r,127,10),e(r,t(28,32),10),e(r,t(48,58),10,8),s(r,59,10,8),e(r,[58,60,61,62,63],10,0,11),e(r,t(32,48),10,9,12),e(r,n,12),s(r,127,12),e(r,t(28,32),12),e(r,t(32,48),12,9),e(r,t(48,64),12,0,11),e(r,t(64,127),12,12,13),e(r,t(64,127),10,12,13),e(r,t(64,127),9,12,13),e(r,n,13,13),e(r,i,13,13),s(r,127,13),e(r,[27,156],13,14,0),r}();r.prototype.reset=function(){this.current_state=0|this.initial_state,this.osc="",this.params=[0],this.collected=""},r.prototype.parse=function(t){for(var s=0,e=0,r=!1,i=0|this.current_state,n=-1,a=-1,o=this.osc,c=this.collected,h=this.params,f=0,u=0|t.length;f<u;++f)if(s=0|t.charCodeAt(f),0===i&&s>31&&s<128)n=n+1?0|n:0|f;else{switch((e=0|(s<160?0|this.transitions[i<<8|s|0]:16))>>4|0){case 2:n=n+1?0|n:0|f;break;case 3:n+1&&(this.term.inst_p(t.substring(n,f)),n=-1),this.term.inst_x(String.fromCharCode(s));break;case 0:n+1?(this.term.inst_p(t.substring(n,f)),n=-1):a+1&&(this.term.inst_P(t.substring(a,f)),a=-1);break;case 1:if(s>159)switch(i){case 0:n=n+1?0|n:0|f;break;case 8:o+=String.fromCharCode(s),e|=8;break;case 6:e|=6;break;case 11:e|=11;break;case 13:a+1||(a=0|f),e|=13;break;default:r=!0}else r=!0;if(r){if(this.term.inst_E({pos:f,character:String.fromCharCode(s),state:i,print:n,dcs:a,osc:o,collect:c,params:h}))return;r=!1}break;case 7:this.term.inst_c(c,h,String.fromCharCode(s));break;case 8:59===s?h.push(0):h[h.length-1]=10*h[h.length-1]+s-48|0;break;case 9:c+=String.fromCharCode(s);break;case 10:this.term.inst_e(c,String.fromCharCode(s));break;case 11:n+1&&(this.term.inst_p(t.substring(n,f)),n=-1),o="",h=[0],c="",a=-1;break;case 12:this.term.inst_H(c,h,String.fromCharCode(s));break;case 13:a+1||(a=0|f);break;case 14:a+1&&this.term.inst_P(t.substring(a,f)),this.term.inst_U(),27===s&&(e|=1),o="",h=[0],c="",a=-1;break;case 4:~n&&(this.term.inst_p(t.substring(n,f)),n=-1),o="";break;case 5:o+=t.charAt(f);break;case 6:o&&24!==s&&26!==s&&this.term.inst_o(o),27===s&&(e|=1),o="",h=[0],c="",a=-1}i=15&e|0}!i&&n+1?this.term.inst_p(t.substring(n)):13===i&&a+1&&this.term.inst_P(t.substring(a)),this.osc=o,this.collected=c,this.params=h,this.current_state=0|i},"undefined"!=typeof module&&void 0!==module.exports?module.exports=r:"function"==typeof define&&define.amd?define([],function(){return r}):window.AnsiParser=r}();