@colin_t/lezer-tree-visualizer
Version:
Visualizes a lezer tree to view it in a console
2 lines (1 loc) • 5.55 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e={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(e){switch(typeof e){case"number":return r[e];case"object":if(e instanceof Array){let o=e[1];if(o<16)return r[o];if(o>=232){const e=t[o-232];return[e,e,e]}return o-=16,[l[Math.floor(o/36)],l[Math.floor(o/6)%6],l[o%6]]}return e.rgb}},to3bit:e=>[e%8,e>=8],fastEquals(e,r){const o=typeof e;return o===typeof r&&("number"===o?e===r:null===e&&null===r||null!==e&&null!==r&&(e instanceof Array?r instanceof Array&&e[1]===r[1]:!(r instanceof Array)&&(e.rgb[0]===r.rgb[0]&&e.rgb[1]===r.rgb[1]&&e.rgb[2]===r.rgb[2])))}},r=[[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=e=>{const r=new Uint8Array(e);let o;for(o=0;o<e;o++)r[o]=Math.round(o*(255/(e-1)));return r},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(e){this.pushColor(),this.buffer+=e,this.color.update_fg=!1,this.color.update_bg=!1}fg(r){e.fastEquals(this.color.fg,r)||(this.color.fg=r,this.color.update_fg=!0)}bg(r){e.fastEquals(this.color.bg,r)||(this.color.bg=r,this.color.update_bg=!0)}resetColor(e=!0,r=!0){e&&null!==this.color.fg!==this.color.update_fg&&(this.color.fg=null,this.color.update_fg=!0),r&&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(r,o){if("number"==typeof r){const[t,l]=e.to3bit(r);return t+(l?90:30)+(o?10:0)+""}return r instanceof Array?(o?"48;":"38;")+r.join(";"):(o?"48;2;":"38;2;")+r.rgb.join(";")}class i extends s{push(e){const r=e.split("\n");let o;for(o=0;o<r.length-1;o++)super.push(r[o]),null===this.color.bg?this.buffer+="\n":(this.buffer+="[m\n",this.color.update_fg=!0,this.color.update_bg=!0);super.push(r[o])}pushColor(){const{update_fg:e,update_bg:r}=this.color;if(!e&&!r)return;const o=[];e&&null!==this.color.fg&&o.push(n(this.color.fg,!1)),r&&null!==this.color.bg&&o.push(n(this.color.bg,!0)),null===this.color.fg&&null===this.color.bg?o.push(""):(e&&null===this.color.fg&&o.push("39"),r&&null===this.color.bg&&o.push("49")),this.buffer+="[",this.buffer+=o.join(";"),this.buffer+="m"}print(){console.log(this.buffer+"[m")}}function u(r,o){return(o?"background:":"color:")+"rgb("+e.toRGB(r).join(",")+");"}class c extends s{constructor(){super(...arguments),this.styles=[]}pushColor(){const e=[];null!==this.color.fg&&e.push(u(this.color.fg,!1)),null!==this.color.bg&&e.push(u(this.color.bg,!0)),this.buffer+="%c",this.styles.push(e.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:e.BrightCyan,source:e.DarkYellow,tree:e.DarkGray,blockQuotes:e.LightGray,blockReturn:e.DarkGray,colon:e.LightGray,ellipsis:e.LightGray},d={name:e.vsc.BrightCyan,source:e.vsc.DarkYellow,tree:e.vsc.DarkGray,blockQuotes:e.vsc.LightGray,blockReturn:e.vsc.DarkGray,colon:e.vsc.LightGray,ellipsis:e.vsc.LightGray},y="object"==typeof process?p:d;function*k(e,r,o,t){const l=e.node.name;switch(t(e.node)){case 2:yield x([l,o.name],[": ",o.colon],["…",o.ellipsis]);case 0:return}if(e.firstChild()){let s,n;yield x([l,o.name],[":",o.colon]);do{for(s of(n=e.node.nextSibling,D(k(e,r,o,t),[n?h:g,o.tree],[n?f:b,o.tree])))yield s}while(e.nextSibling());e.parent()}else{const t=r.substring(e.from,e.to);if(t===l)yield x([l,o.source]);else if(t.indexOf("\n")<0)yield x([l,o.name],[": ",o.colon],[t,o.source]);else{yield x([l,o.name],[": ",o.colon],["'''",o.blockQuotes]);let e=0,r=t.split(/\r?\n/g),s=r.length-1;for(;e<s;e++)yield x([b+r[e],o.source],["⮐",o.blockReturn]);s>=0&&(yield x([b+r[e],o.source])),yield x([" '''",o.blockQuotes])}}}function*x(...e){let r=0,o=e.length;for(;r<o;r++)yield e[r]}function*D(e,r,o){let t=e.next();for(t.done||(yield G(r,t.value));t=e.next(),!t.done;)yield G(o,t.value);return t.value}function*G(e,r){let o;for(yield e;o=r.next(),!o.done;)yield o.value;return o.value}exports.Color=e,exports.ColorWriter=a,exports.OsColorWriter=i,exports.VisibilityModifier={Hidden:0,Shown:1,Collapsed:2},exports.WebColorWriter=c,exports.Writer=s,exports.defaultOsTheme=p,exports.defaultTheme=y,exports.defaultWebTheme=d,exports.visualize=function(e,r,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 k(e,r,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*(e,r){let o,t;if(o=e.next(),!o.done)for(t of o.value)yield t;for(;o=e.next(),!o.done;)for(t of(yield r,o.value))yield t}(k(e,r,i,n),["\n",void 0]))l[1]&&(-1===l[1]?t.resetColor(!0,!1):t.fg(l[1])),t.push(l[0]);t.print()}};