UNPKG

@colin_t/lezer-tree-visualizer

Version:
2 lines (1 loc) 5.45 kB
const r={Black:0,DarkRed:1,DarkGreen:2,DarkYellow:3,DarkBlue:4,DarkMagenta:5,DarkCyan:6,LightGray:7,DarkGray:8,Red:9,Green:10,Yellow:11,Blue:12,Magenta:13,BrightCyan:14,White:15,vsc:{Black:{rgb:[0,0,0]},DarkRed:{rgb:[205,49,49]},DarkGreen:{rgb:[13,188,121]},DarkYellow:{rgb:[229,229,16]},DarkBlue:{rgb:[36,114,200]},DarkMagenta:{rgb:[188,63,188]},DarkCyan:{rgb:[17,168,205]},LightGray:{rgb:[229,229,229]},DarkGray:{rgb:[102,102,102]},Red:{rgb:[241,76,76]},Green:{rgb:[35,209,139]},Yellow:{rgb:[245,245,67]},Blue:{rgb:[59,142,234]},Magenta:{rgb:[214,112,214]},BrightCyan:{rgb:[41,184,219]},White:{rgb:[229,229,229]}},toRGB(r){switch(typeof r){case"number":return e[r];case"object":if(r instanceof Array){let o=r[1];if(o<16)return e[o];if(o>=232){const r=t[o-232];return[r,r,r]}return o-=16,[l[Math.floor(o/36)],l[Math.floor(o/6)%6],l[o%6]]}return r.rgb}},to3bit:r=>[r%8,r>=8],fastEquals(r,e){const o=typeof r;return o===typeof e&&("number"===o?r===e:null===r&&null===e||null!==r&&null!==e&&(r instanceof Array?e instanceof Array&&r[1]===e[1]:!(e instanceof Array)&&(r.rgb[0]===e.rgb[0]&&r.rgb[1]===e.rgb[1]&&r.rgb[2]===e.rgb[2])))}},e=[[0,0,0],[128,0,0],[0,128,0],[128,128,0],[0,0,128],[128,0,128],[0,128,128],[192,192,192],[128,128,128],[255,0,0],[0,255,0],[255,255,0],[0,0,255],[255,0,255],[0,255,255],[255,255,255]],o=r=>{const e=new Uint8Array(r);let o;for(o=0;o<r;o++)e[o]=Math.round(o*(255/(r-1)));return e},t=o(24),l=o(6);class s{constructor(){this.buffer="",this.color={fg:null,bg:null,update_fg:!1,update_bg:!1}}pushColor(){}push(r){this.pushColor(),this.buffer+=r,this.color.update_fg=!1,this.color.update_bg=!1}fg(e){r.fastEquals(this.color.fg,e)||(this.color.fg=e,this.color.update_fg=!0)}bg(e){r.fastEquals(this.color.bg,e)||(this.color.bg=e,this.color.update_bg=!0)}resetColor(r=!0,e=!0){r&&null!==this.color.fg!==this.color.update_fg&&(this.color.fg=null,this.color.update_fg=!0),e&&null!==this.color.bg!==this.color.update_bg&&(this.color.bg=null,this.color.update_bg=!0)}print(){console.log(this.buffer)}clear(){this.buffer="",this.color.update_bg=!0,this.color.update_fg=!0}toString(){return this.buffer}}function n(e,o){if("number"==typeof e){const[t,l]=r.to3bit(e);return t+(l?90:30)+(o?10:0)+""}return e instanceof Array?(o?"48;":"38;")+e.join(";"):(o?"48;2;":"38;2;")+e.rgb.join(";")}class i extends s{push(r){const e=r.split("\n");let o;for(o=0;o<e.length-1;o++)super.push(e[o]),null===this.color.bg?this.buffer+="\n":(this.buffer+="\n",this.color.update_fg=!0,this.color.update_bg=!0);super.push(e[o])}pushColor(){const{update_fg:r,update_bg:e}=this.color;if(!r&&!e)return;const o=[];r&&null!==this.color.fg&&o.push(n(this.color.fg,!1)),e&&null!==this.color.bg&&o.push(n(this.color.bg,!0)),null===this.color.fg&&null===this.color.bg?o.push(""):(r&&null===this.color.fg&&o.push("39"),e&&null===this.color.bg&&o.push("49")),this.buffer+="[",this.buffer+=o.join(";"),this.buffer+="m"}print(){console.log(this.buffer+"")}}function u(e,o){return(o?"background:":"color:")+"rgb("+r.toRGB(e).join(",")+");"}class c extends s{constructor(){super(...arguments),this.styles=[]}pushColor(){const r=[];null!==this.color.fg&&r.push(u(this.color.fg,!1)),null!==this.color.bg&&r.push(u(this.color.bg,!0)),this.buffer+="%c",this.styles.push(r.join(";"))}print(){console.log(this.buffer,...this.styles)}clear(){super.clear(),this.styles.length=0}}const a="object"==typeof process?i:c,[f,h,g,b]=["│ ","├╴","└╴"," "],p={name:r.BrightCyan,source:r.DarkYellow,tree:r.DarkGray,blockQuotes:r.LightGray,blockReturn:r.DarkGray,colon:r.LightGray,ellipsis:r.LightGray},d={name:r.vsc.BrightCyan,source:r.vsc.DarkYellow,tree:r.vsc.DarkGray,blockQuotes:r.vsc.LightGray,blockReturn:r.vsc.DarkGray,colon:r.vsc.LightGray,ellipsis:r.vsc.LightGray},y="object"==typeof process?p:d,k={Hidden:0,Shown:1,Collapsed:2};function D(r,e,o={}){const t=o.writer||(o.colors??1?new a:new s);let l,n=o.filter||(()=>1),i=o.theme||y;if(o.lineByLine){let o;for(o of G(r,e,i,n)){for(l of o)l[1]&&(-1===l[1]?t.resetColor(!0,!1):t.fg(l[1])),t.push(l[0]);t.print(),t.clear()}}else{for(l of function*(r,e){let o,t;if(o=r.next(),!o.done)for(t of o.value)yield t;for(;o=r.next(),!o.done;)for(t of(yield e,o.value))yield t}(G(r,e,i,n),["\n",void 0]))l[1]&&(-1===l[1]?t.resetColor(!0,!1):t.fg(l[1])),t.push(l[0]);t.print()}}function*G(r,e,o,t){const l=r.node.name;switch(t(r.node)){case 2:yield v([l,o.name],[": ",o.colon],["…",o.ellipsis]);case 0:return}if(r.firstChild()){let s,n;yield v([l,o.name],[":",o.colon]);do{for(s of(n=r.node.nextSibling,_(G(r,e,o,t),[n?h:g,o.tree],[n?f:b,o.tree])))yield s}while(r.nextSibling());r.parent()}else{const t=e.substring(r.from,r.to);if(t===l)yield v([l,o.source]);else if(t.indexOf("\n")<0)yield v([l,o.name],[": ",o.colon],[t,o.source]);else{yield v([l,o.name],[": ",o.colon],["'''",o.blockQuotes]);let r=0,e=t.split(/\r?\n/g),s=e.length-1;for(;r<s;r++)yield v([b+e[r],o.source],["⮐",o.blockReturn]);s>=0&&(yield v([b+e[r],o.source])),yield v([" '''",o.blockQuotes])}}}function*v(...r){let e=0,o=r.length;for(;e<o;e++)yield r[e]}function*_(r,e,o){let t=r.next();for(t.done||(yield m(e,t.value));t=r.next(),!t.done;)yield m(o,t.value);return t.value}function*m(r,e){let o;for(yield r;o=e.next(),!o.done;)yield o.value;return o.value}export{r as Color,a as ColorWriter,i as OsColorWriter,k as VisibilityModifier,c as WebColorWriter,s as Writer,p as defaultOsTheme,y as defaultTheme,d as defaultWebTheme,D as visualize};