UNPKG

ansidec

Version:

Limited Unix ANSI escape sequences transformer for use in Browsers

21 lines 11.3 kB
/**@license * ▄████████ ███▄▄▄▄ ▄████████ ▄█ ████████▄ ▄████████ ▄████████ * ███ ███ ███▀▀▀██▄ ███ ███ ███ ███ ▀███ ███ ███ ███ ███ * ███ ███ ███ ███ ███ █▀ ███▌ ███ ███ ███ █▀ ███ █▀ * ███ ███ ███ ███ ███ ███▌ ███ ███ ▄███▄▄▄ ███ * ▀███████████ ███ ███ ▀███████████ ███▌ ███ ███ ▀▀███▀▀▀ ███ * ███ ███ ███ ███ ███ ███ ███ ███ ███ █▄ ███ █▄ * ███ ███ ███ ███ ▄█ ███ ███ ███ ▄███ ███ ███ ███ ███ * ███ █▀ ▀█ █▀ ▄████████▀ █▀ ████████▀ ██████████ ████████▀ * v. 0.3.4 * * Copyright (c) 2018-2019 Jakub T. Jankiewicz <https://jcubic.pl/me> * Released under the MIT license * * Contains: SAUCE parser from ansilove.js MIT license * * Based on jQuery Terminal's unix formatting * */ (function(F){var e=typeof window!=="undefined"?window:global;if(typeof define==="function"&&define.amd){define([],function(){F(window.TextEncoder)})}else if(typeof module==="object"&&module.exports){module.exports=F(require("util").TextEncoder)}else{e.ansi=F(window.TextEncoder)}})(function(i,t){var F="[^\\x08]|[\\r\\n]{2}|&[^;]+;";var d=new RegExp("^("+F+")?\\x08");var p=new RegExp("^(?:("+F+")?\\x08(_|\\1)|"+"(_)\\x08("+F+"))");var b=/^(\r\n|\n\r|\r|\n)/;var n=/[^\r\n]+\r\x1B\[K/g;function r(r,F){var c=[];var D;var g=0;var v=[];function e(F,n){var e="";var t=0;var i;g=0;function r(F,e,t){if(F<n){var i=F+e.length;if(i<n){D=Math.max(0,D+t.length-e.length)}else{D+=t.length-(n-F)}}}for(var a=0;a<F.length;++a){var s=F.substring(a);var f=s.match(d);var l=c[0];if(f){if(f[1]){i=a-f[1].length+t;c.push({index:i,string:f[1],overtyping:s.match(p)});r(i,f[0],"",1)}if(g<0){g=0}v=v.map(function(F){return F-1});v.push(i);return e+s.replace(d,"")}else if(s.match(b)){if(c.length){var u=c;c=[];u.reverse().forEach(function(F){if(a>F.index){if(--g<=0){r(F.index,"",F.string,2);e+=F.string}}else{c.unshift(F)}})}var A=s.match(b);e+=A[1];a+=A[1].length-1}else{if(v.length){var h=v[0];if(a===h){v.shift()}if(a>=h){g++}}if(c.length){if(a>l.index&&l.overtyping){c.shift();r(l.index,"",l.string);t++;if(l.string===F[a]){e+=F[a]+"￱";continue}else if(l.string==="_"||F[a]==="_"){var o;if(l.string==="_"){o=F[a]}else{o=l.string}e+=o+"￲";continue}}}e+=F[a]}}return e}var t=false;F=F.replace(n,"");while(F.match(/\x08/)||c.length){F=e(F,D);if(t){break}if(!F.match(/\x08/)){t=true}}function i(F,i,n){var e=new RegExp("((:?."+i+")+)","g");return F.replace(e,function(F,e){var t=new RegExp(i,"g");return r(n,null,null,e.replace(t,""))})}if(typeof r==="function"){F=i(F,"￱",{bold:true});F=i(F,"￲",{underline:true})}return F}var x={normal:{black:"#000",red:"#A00",green:"#008400",yellow:"#A50",blue:"#00A",magenta:"#A0A",cyan:"#0AA",white:"#AAA"},faited:{black:"#000",red:"#640000",green:"#006100",yellow:"#737300",blue:"#000087",magenta:"#650065",cyan:"#008787",white:"#818181"},bold:{black:"#444",red:"#F55",green:"#44D544",yellow:"#FF5",blue:"#55F",magenta:"#F5F",cyan:"#5FF",white:"#FFF"},palette:["#000000","#AA0000","#00AA00","#AA5500","#0000AA","#AA00AA","#00AAAA","#AAAAAA","#555555","#FF5555","#55FF55","#FFFF55","#5555FF","#FF55FF","#55FFFF","#FFFFFF","#000000","#00005F","#000087","#0000AF","#0000D7","#0000FF","#005F00","#005F5F","#005F87","#005FAF","#005FD7","#005FFF","#008700","#00875F","#008787","#0087AF","#0087D7","#0087FF","#00AF00","#00AF5F","#00AF87","#00AFAF","#00AFD7","#00AFFF","#00D700","#00D75F","#00D787","#00D7AF","#00D7D7","#00D7FF","#00FF00","#00FF5F","#00FF87","#00FFAF","#00FFD7","#00FFFF","#5F0000","#5F005F","#5F0087","#5F00AF","#5F00D7","#5F00FF","#5F5F00","#5F5F5F","#5F5F87","#5F5FAF","#5F5FD7","#5F5FFF","#5F8700","#5F875F","#5F8787","#5F87AF","#5F87D7","#5F87FF","#5FAF00","#5FAF5F","#5FAF87","#5FAFAF","#5FAFD7","#5FAFFF","#5FD700","#5FD75F","#5FD787","#5FD7AF","#5FD7D7","#5FD7FF","#5FFF00","#5FFF5F","#5FFF87","#5FFFAF","#5FFFD7","#5FFFFF","#870000","#87005F","#870087","#8700AF","#8700D7","#8700FF","#875F00","#875F5F","#875F87","#875FAF","#875FD7","#875FFF","#878700","#87875F","#878787","#8787AF","#8787D7","#8787FF","#87AF00","#87AF5F","#87AF87","#87AFAF","#87AFD7","#87AFFF","#87D700","#87D75F","#87D787","#87D7AF","#87D7D7","#87D7FF","#87FF00","#87FF5F","#87FF87","#87FFAF","#87FFD7","#87FFFF","#AF0000","#AF005F","#AF0087","#AF00AF","#AF00D7","#AF00FF","#AF5F00","#AF5F5F","#AF5F87","#AF5FAF","#AF5FD7","#AF5FFF","#AF8700","#AF875F","#AF8787","#AF87AF","#AF87D7","#AF87FF","#AFAF00","#AFAF5F","#AFAF87","#AFAFAF","#AFAFD7","#AFAFFF","#AFD700","#AFD75F","#AFD787","#AFD7AF","#AFD7D7","#AFD7FF","#AFFF00","#AFFF5F","#AFFF87","#AFFFAF","#AFFFD7","#AFFFFF","#D70000","#D7005F","#D70087","#D700AF","#D700D7","#D700FF","#D75F00","#D75F5F","#D75F87","#D75FAF","#D75FD7","#D75FFF","#D78700","#D7875F","#D78787","#D787AF","#D787D7","#D787FF","#D7AF00","#D7AF5F","#D7AF87","#D7AFAF","#D7AFD7","#D7AFFF","#D7D700","#D7D75F","#D7D787","#D7D7AF","#D7D7D7","#D7D7FF","#D7FF00","#D7FF5F","#D7FF87","#D7FFAF","#D7FFD7","#D7FFFF","#FF0000","#FF005F","#FF0087","#FF00AF","#FF00D7","#FF00FF","#FF5F00","#FF5F5F","#FF5F87","#FF5FAF","#FF5FD7","#FF5FFF","#FF8700","#FF875F","#FF8787","#FF87AF","#FF87D7","#FF87FF","#FFAF00","#FFAF5F","#FFAF87","#FFAFAF","#FFAFD7","#FFAFFF","#FFD700","#FFD75F","#FFD787","#FFD7AF","#FFD7D7","#FFD7FF","#FFFF00","#FFFF5F","#FFFF87","#FFFFAF","#FFFFD7","#FFFFFF","#080808","#121212","#1C1C1C","#262626","#303030","#3A3A3A","#444444","#4E4E4E","#585858","#626262","#6C6C6C","#767676","#808080","#8A8A8A","#949494","#9E9E9E","#A8A8A8","#B2B2B2","#BCBCBC","#C6C6C6","#D0D0D0","#DADADA","#E4E4E4","#EEEEEE"]};var a=function(){var m={30:"black",31:"red",32:"green",33:"yellow",34:"blue",35:"magenta",36:"cyan",37:"white",39:"inherit"};var y={40:"black",41:"red",42:"green",43:"yellow",44:"blue",45:"magenta",46:"cyan",47:"white",49:"transparent"};function c(F,e){var t=F.split(";");var i;var n=[];var r="";var a="";var s=-1;var f=false;var l=false;var u=false;var A=x.palette;function h(F){switch(F){case 0:Object.keys(e).forEach(function(F){delete e[F]});e.bold=false;e.faited=false;break;case 1:n.bold=e.bold=true;e.faited=false;break;case 4:n.underline=e.underline=true;break;case 3:n.italic=e.italic=true;break;case 5:if(f||l){u=true}break;case 38:f=true;break;case 48:l=true;break;case 2:if(f||l){s=0}else{e.faited=true;e.bold=false}break;case 7:e.reverse=true;break;default:if(t[1]!=="5"){if(m[F]){r=m[F]}if(y[F]){a=y[F]}}}}function o(){if(f){if(!r){r="#"}if(r.length<7){r+=("0"+i.toString(16)).slice(-2)}}if(l){if(!a){a="#"}if(a.length<7){a+=("0"+i.toString(16)).slice(-2)}}if(s===2){s=-1}else{s++}}function c(){return u&&(l&&!a||f&&!r)}function D(){if(f&&A[i]&&!r){r=A[i]}if(l&&A[i]&&!a){a=A[i]}u=false}for(var g in t){if(t.hasOwnProperty(g)){i=parseInt(t[g],10);if(s>-1){o()}else if(c()){D()}else{h(i)}}}if(e.reverse){if(r||a){var v=a;a=r;r=v}else{r="black";a="white"}}r=r||e.color;a=a||e.background;e.background=a;e.color=r;var d,p,b;if(e.bold){d=x.bold}else if(e.faited){d=x.faited}else{d=x.normal}if(typeof r!=="undefined"){if(r.match(/^#/)){p=r}else if(r==="inherit"){p=r}else{p=d[r]}}if(typeof a!=="undefined"){if(a.match(/^#/)){b=a}else if(a==="transparent"){b=a}else{b=x.normal[a]}}var w=[n,p,b];return w}return function F(i,e){var t={};var s=/(\x1B\[[0-9;]*[A-Za-z])/g;var n=/(.*)\r?\n\x1b\[1A\x1b\[([0-9]+)C/g;e=e.replace(n,function(F,e,t){t=parseInt(t,10);var i=e.split(s).filter(Boolean);var n=[];for(var r=0;r<i.length;++r){if(i[r].match(s)){n.push(i[r])}else{var a=i[r].length;if(a>t){n.push(i[r].substring(0,t));break}else{n.push(i[r])}t-=a}}return n.join("")});e=e.replace(/\x1b\[([0-9]+)C/g,function(F,e){return new Array(+e+1).join(" ")});var r=e.split(s);if(r.length===1){return e}var a=[];if(r.length>3){var f=r.slice(0,3).join("");if(f.match(/^\[0*m$/)){r=r.slice(3)}}var l,u,A=false;for(var h=0;h<r.length;++h){u=r[h].match(/^\x1B\[([0-9;]*)([A-Za-z])$/);if(u){switch(u[2]){case"m":l=c(u[1],t);if(A){if(+u[1]===0){A=false;a.push(false)}else{a.push(l)}}else if(+u[1]!==0){a.push(l);A=true}break}}else{a.push(r[h].replace(/\x1b\[[0-9;]*/g,""))}}var o;return a.reduce(function(F,e){if(typeof e==="string"){if(o&&e){var t=o.concat([e]);if(typeof i==="function"){return F+i.apply(null,t)}}o=null;return F+e}else{o=e;return F}},"")}}();function s(e,F){if(F===t){return function(F){return s(e,F)}}return a(e,r(e,F))}function e(F){return s(function(F,e,t,i){var n=[];if(e){n.push("color:"+e)}if(t){n.push("background:"+t)}if(F.bold){n.push("font-weight:bold")}if(F.italic){n.push("font-style:italic")}if(F.underline){F.push("text-decoration:underline")}return'<span style="'+n.join(";")+'">'+i+"</span>"},F)}function f(t){var i,n,F,e;n=new Uint8Array([83,65,85,67,69]);F=new Uint8Array([67,79,77,78,84]);this.get=function(){if(i>=t.length){throw new Error("Unexpected end of file reached.")}return t[i++]};this.get16=function(){var F;F=this.get();return F+(this.get()<<8)};this.get32=function(){var F;F=this.get();F+=this.get()<<8;F+=this.get()<<16;return F+(this.get()<<24)};this.getC=function(){return String.fromCharCode(this.get())};this.getS=function(F){var e;e="";while(F-- >0){e+=this.getC()}return e.replace(/[\x00\s]+$/,"")};this.lookahead=function(F){if(n===F){}var e;for(e=0;e<F.length;++e){if(i+e===t.length||t[i+e]!==F[e]){break}}return e===F.length};this.read=function(F){var e;e=i;F=F||this.size-i;while(++i<this.size){if(--F===0){break}}return t.subarray(e,i)};this.seek=function(F){i=F};this.peek=function(F){F=F||0;return t[i+F]};this.getPos=function(){return i};this.eof=function(){return i===this.size};i=t.length-128;if(this.lookahead(n)){this.sauce={};this.getS(5);this.sauce.version=this.getS(2);this.sauce.title=this.getS(35);this.sauce.author=this.getS(20);this.sauce.group=this.getS(20);this.sauce.date=this.getS(8);this.sauce.fileSize=this.get32();this.sauce.dataType=this.get();this.sauce.fileType=this.get();this.sauce.tInfo=[];this.sauce.tInfo.push(this.get16());this.sauce.tInfo.push(this.get16());this.sauce.tInfo.push(this.get16());this.sauce.tInfo.push(this.get16());this.sauce.comments=[];e=this.get();this.sauce.flags=this.get();this.sauce.zstring=this.getS(22);if(e>0){i=t.length-128-e*64-5;if(this.lookahead(F)){this.getS(5);while(e-- >0){this.sauce.comments.push(this.getS(64))}}}}i=0;if(this.sauce){if(this.sauce.fileSize>0&&this.sauce.fileSize<t.length){this.size=this.sauce.fileSize}else{this.size=t.length-128}}else{this.size=t.length}}function l(F){var e;if(typeof F==="string"){e=new Uint8Array(new i("utf8").encode(F))}else{e=F}var t=new f(e);return t.sauce}return{version:"0.3.4",meta:l,format:s,html:e,colors:x}}); //# sourceMappingURL=ascidec.min.js.map