cli-high
Version:
tiny cli highlighter
5 lines (4 loc) • 987 B
JavaScript
;const sugarHigh=require("sugar-high"),xycolors=require("xycolors");function highlight(c,h={showLineNumbers:!1}){const a=sugarHigh.tokenize(c),l=[];let o=1;const e=[];function g(r){const n=h.showLineNumbers?`${xycolors.dim(`${o}`.padEnd(2," "))} `:"";return o++,`${n}${r}`}function i(r){l.push(g(r.map(([n,t])=>{switch(n){case 0:return xycolors.pinkStylize(t);case 1:return xycolors.grayStylize(t);case 2:return xycolors.grayStylize(t);case 3:return xycolors.yellowStylize(t);case 4:return xycolors.pinkStylize(t);case 5:return xycolors.magentaStylize(t);case 6:return xycolors.whiteSecondaryStylize(t);case 7:return xycolors.grayStylize(t);case 8:return xycolors.dim(t);default:return t}}).join("")))}for(const r of a){const[n,t]=r;if(n!==9)if(t.includes(`
`)){const u=t.split(`
`);for(let s=0;s<u.length;s++)e.push([n,u[s]]),s<u.length-1&&(i(e),e.length=0)}else e.push(r);else e.push([n,""]),i(e),e.length=0}return e.length>0&&i(e),l.join(`
`)}exports.highlight=highlight;