@remotex-labs/xansi
Version:
A lightweight ANSI utility library for styling terminal output
4 lines (3 loc) • 6.11 kB
JavaScript
function p(o){process.stdout.write?process.stdout.write(o):console.log(o)}function C(o,e=0){return`\x1B[${o};${e}H`}function w(o){return o.replace(/\x1b\[[0-9;]*m/g,"")}var c={CLEAR_LINE:"\x1B[K",HIDE_CURSOR:"\x1B[?25l",SHOW_CURSOR:"\x1B[?25h",SAVE_CURSOR:"\x1B[s",CLEAR_SCREEN:"\x1B[2J\x1B[H",RESTORE_CURSOR:"\x1B[u",CLEAR_SCREEN_DOWN:"\x1B[J",RESET_TERMINAL:"\x1Bc"};var B={dim:[2,22],bold:[1,22],reset:[0,0],hidden:[8,28],inverse:[7,27]},v={red:[31,39],gray:[90,39],blue:[34,39],cyan:[36,39],black:[30,39],white:[37,39],green:[32,39],yellow:[33,39],magenta:[35,39],redBright:[91,39],blueBright:[94,39],cyanBright:[96,39],whiteBright:[97,39],greenBright:[92,39],blackBright:[90,39],yellowBright:[93,39],magentaBright:[95,39],darkGray:["38;5;238",39],lightGray:["38;5;252",39],lightCoral:["38;5;203",39],oliveGreen:["38;5;149",39],deepOrange:["38;5;166",39],brightPink:["38;5;197",39],lightOrange:["38;5;215",39],burntOrange:["38;5;208",39],lightYellow:["38;5;230",39],canaryYellow:["38;5;227",39],lightGoldenrodYellow:["38;5;221",39]},R={bgRed:[41,49],bgBlue:[44,49],bgCyan:[46,49],bgGray:[100,49],bgBlack:[40,49],bgGreen:[42,49],bgWhite:[47,49],bgYellow:[43,49],bgMagenta:[45,49],bgRedBright:[101,49],bgBlueBright:[104,49],bgCyanBright:[106,49],bgBlackBright:[100,49],bgWhiteBright:[107,49],bgGreenBright:[102,49],bgYellowBright:[103,49],bgMagentaBright:[105,49]};var A={...B,...v,...R},h="\x1B[",b="m";function x(o,e){let n=o.length;if(n===0)return e;if(n===1)return`${h}${o[0][0]}${b}${e}${h}${o[0][1]}${b}`;let r=new Array(n),t=new Array(n);for(let i=0;i<n;i++)t[i]=`${h}${o[i][0]}${b}`,r[n-i-1]=`${h}${o[i][1]}${b}`;return t.concat(e,r).join("")}function m(o,e,n,r){if(typeof e!="number"||typeof n!="number"||typeof r!="number")throw new Error(`RGB values must be numbers, received: r=${typeof e}, g=${typeof n}, b=${typeof r}`);let t=o==="fg"?38:48,i=o==="fg"?39:49;return[`${t};2;${e};${n};${r}`,i]}function $(o){let e=o.replace(/^#/,"").toLowerCase();if(!/^([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(e))throw new Error(`Invalid hex color format: "${o}". Expected 3 or 6 hex digits.`);if(e.length===3){let i=parseInt(e[0]+e[0],16),l=parseInt(e[1]+e[1],16),s=parseInt(e[2]+e[2],16);return[i,l,s]}let n=parseInt(e.slice(0,2),16),r=parseInt(e.slice(2,4),16),t=parseInt(e.slice(4,6),16);return[n,r,t]}function a(o=[]){let e=(...r)=>{if(Array.isArray(r[0])&&"raw"in r[0]){let[t,...i]=r,l=t.reduce((s,g,u)=>s+g+(u<i.length?String(i[u]??""):""),"");return x(o,l)}return x(o,r.join(""))},n={rgb:(r,t,i)=>a([...o,m("fg",r,t,i)]),bgRgb:(r,t,i)=>a([...o,m("bg",r,t,i)]),hex:r=>a([...o,m("fg",...$(r))]),bgHex:r=>a([...o,m("bg",...$(r))])};return new Proxy(e,{get(r,t){if(typeof t!="string")throw new Error(`Invalid property: ${String(t)}`);return t in A?a([...o,A[t]]):t in n?n[t]:Reflect.get(r,t)}})}var E=a();var L={1:"22",3:"23",4:"24",5:"25",7:"27",8:"28",9:"29",30:"39",31:"39",32:"39",33:"39",34:"39",35:"39",36:"39",37:"39",40:"49",41:"49",42:"49",43:"49",44:"49",45:"49",46:"49",47:"49",0:null,22:null,23:null,24:null,25:null,27:null,28:null,29:null,39:null,49:null};function T(o,e,n,r){if((o==="38"||o==="48")&&n[e+1]==="2"){let i=n.slice(e+2,e+5);if(i.length===3){let l=`${o};2;${i.join(";")}`,s=o==="38"?"39":"49";return r.set(l,s),e+4}}let t=L[o];if(t===null)if(o==="0")r.clear();else for(let[i,l]of r)(i===o||l===o)&&r.delete(i);else t&&r.set(o,t);return e}function S(o){let e=/\x1b\[[0-9;]*m/g,n=o.match(new RegExp(`${e.source}|.`,"g"))||[],r=new Map,t=[];for(let i of n){if(!i.startsWith("\x1B[")){let s=[...r.keys()].map(u=>`\x1B[${u}m`).join(""),g=[...new Set(r.values())].map(u=>`\x1B[${u}m`).join("");t.push(`${s}${i}${g}`);continue}let l=i.slice(2,-1).split(";");for(let s=0;s<l.length;s++)s=T(l[s],s,l,r)}return t}var y=class{constructor(e,n,r,t){this.terminalHeight=e;this.terminalWidth=n;this.topPosition=r;this.leftPosition=t}scrollPosition=0;viewBuffer=[];contentBuffer=[];get top(){return this.topPosition}get left(){return this.leftPosition}get width(){return this.terminalWidth}get height(){return this.terminalHeight}get scroll(){return this.scrollPosition}set top(e){this.topPosition=e}set left(e){this.leftPosition=e}set width(e){this.terminalWidth=e}set height(e){this.terminalHeight=e}set scroll(e){let n=e<0?this.scrollPosition+e:e;n>=this.contentBuffer.length||(this.scrollPosition=n,this.render())}clear(){this.clearScreen(),this.viewBuffer=[],this.contentBuffer=[]}clearScreen(){let e="";for(let n=0;n<this.viewBuffer.length;n++)e+=this.moveCursor(n,0),e+=c.CLEAR_LINE;p(e)}writeText(e,n,r,t=!1){if(e<0||n>=this.terminalWidth)return;t&&(this.contentBuffer[e]=[]),r=r.split(`
`)[0];let i=this.contentBuffer[e]??=[],l=w(r),s=Math.min(this.terminalWidth-n,l.length),g=S(r);for(let u=0;u<s;u++){let f=n+u,d=g[u];i[f]||(i[f]={char:"",dirty:!0}),i[f].char!==d&&(i[f].char=d,i[f].dirty=!0)}i.length=Math.min(Math.max(n+s,this.terminalWidth),this.terminalWidth)}render(e=!1){let n=Math.min(this.scrollPosition,this.contentBuffer.length),r=Math.min(n+this.terminalHeight,this.contentBuffer.length);if(n>=r)return;let t={force:e,output:"",viewLine:[],screenRow:1,contentLine:[]};for(let i=n;i<r;i++,t.screenRow++)t.contentLine=this.contentBuffer[i],t.contentLine&&(t.viewLine=this.viewBuffer[t.screenRow]??=[],this.renderLine(t),t.viewLine.length>t.contentLine.length&&(t.output+=c.CLEAR_LINE,t.viewLine.length=t.contentLine.length));if(r>=this.contentBuffer.length){this.viewBuffer.length=t.screenRow;for(let i=t.screenRow;i<=this.terminalHeight;i++)t.output+=this.moveCursor(i,0),t.output+=c.CLEAR_LINE}t.output+=this.moveCursor(this.viewBuffer.length,this.terminalWidth),p(t.output)}renderLine(e){let n=!0;for(let r=0;r<e.contentLine.length;r++){let t=e.contentLine[r];if(t&&!t.dirty&&t.char===e.viewLine[r]&&!e.force){n=!0;continue}if(n&&(e.output+=this.moveCursor(e.screenRow,r+1),n=!1),!t){e.viewLine[r]=" ",e.output+=" ";continue}e.viewLine[r]=t.char,e.output+=t.char,t.dirty=!1}}moveCursor(e,n=0){return C(e+this.topPosition,n+this.leftPosition)}};export{c as ANSI,y as ShadowRenderer,C as moveCursor,w as stripAnsi,p as writeRaw,E as xterm};
//# sourceMappingURL=index.js.map