@remotex-labs/xansi
Version:
A lightweight ANSI utility library for styling terminal output
4 lines (3 loc) • 6.64 kB
JavaScript
;var d=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var P=(i,e)=>{for(var n in e)d(i,n,{get:e[n],enumerable:!0})},W=(i,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of I(e))!k.call(i,t)&&t!==n&&d(i,t,{get:()=>e[t],enumerable:!(r=T(e,t))||r.enumerable});return i};var _=i=>W(d({},"__esModule",{value:!0}),i);var N={};P(N,{ANSI:()=>c,ShadowRenderer:()=>y,moveCursor:()=>C,stripAnsi:()=>w,writeRaw:()=>h,xterm:()=>E});module.exports=_(N);function h(i){process.stdout.write?process.stdout.write(i):console.log(i)}function C(i,e=0){return`\x1B[${i};${e}H`}function w(i){return i.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 v={dim:[2,22],bold:[1,22],reset:[0,0],hidden:[8,28],inverse:[7,27]},R={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]},A={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 x={...v,...R,...A},b="\x1B[",m="m";function $(i,e){let n=i.length;if(n===0)return e;if(n===1)return`${b}${i[0][0]}${m}${e}${b}${i[0][1]}${m}`;let r=new Array(n),t=new Array(n);for(let o=0;o<n;o++)t[o]=`${b}${i[o][0]}${m}`,r[n-o-1]=`${b}${i[o][1]}${m}`;return t.concat(e,r).join("")}function p(i,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=i==="fg"?38:48,o=i==="fg"?39:49;return[`${t};2;${e};${n};${r}`,o]}function S(i){let e=i.replace(/^#/,"").toLowerCase();if(!/^([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/.test(e))throw new Error(`Invalid hex color format: "${i}". Expected 3 or 6 hex digits.`);if(e.length===3){let o=parseInt(e[0]+e[0],16),l=parseInt(e[1]+e[1],16),s=parseInt(e[2]+e[2],16);return[o,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(i=[]){let e=(...r)=>{if(Array.isArray(r[0])&&"raw"in r[0]){let[t,...o]=r,l=t.reduce((s,g,u)=>s+g+(u<o.length?String(o[u]??""):""),"");return $(i,l)}return $(i,r.join(""))},n={rgb:(r,t,o)=>a([...i,p("fg",r,t,o)]),bgRgb:(r,t,o)=>a([...i,p("bg",r,t,o)]),hex:r=>a([...i,p("fg",...S(r))]),bgHex:r=>a([...i,p("bg",...S(r))])};return new Proxy(e,{get(r,t){if(typeof t!="string")throw new Error(`Invalid property: ${String(t)}`);return t in x?a([...i,x[t]]):t in n?n[t]:Reflect.get(r,t)}})}var E=a();var M={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 H(i,e,n,r){if((i==="38"||i==="48")&&n[e+1]==="2"){let o=n.slice(e+2,e+5);if(o.length===3){let l=`${i};2;${o.join(";")}`,s=i==="38"?"39":"49";return r.set(l,s),e+4}}let t=M[i];if(t===null)if(i==="0")r.clear();else for(let[o,l]of r)(o===i||l===i)&&r.delete(o);else t&&r.set(i,t);return e}function L(i){let e=/\x1b\[[0-9;]*m/g,n=i.match(new RegExp(`${e.source}|.`,"g"))||[],r=new Map,t=[];for(let o of n){if(!o.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}${o}${g}`);continue}let l=o.slice(2,-1).split(";");for(let s=0;s<l.length;s++)s=H(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;h(e)}writeText(e,n,r,t=!1){if(e<0||n>=this.terminalWidth)return;t&&(this.contentBuffer[e]=[]),r=r.split(`
`)[0];let o=this.contentBuffer[e]??=[],l=w(r),s=Math.min(this.terminalWidth-n,l.length),g=L(r);for(let u=0;u<s;u++){let f=n+u,B=g[u];o[f]||(o[f]={char:"",dirty:!0}),o[f].char!==B&&(o[f].char=B,o[f].dirty=!0)}o.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 o=n;o<r;o++,t.screenRow++)t.contentLine=this.contentBuffer[o],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 o=t.screenRow;o<=this.terminalHeight;o++)t.output+=this.moveCursor(o,0),t.output+=c.CLEAR_LINE}t.output+=this.moveCursor(this.viewBuffer.length,this.terminalWidth),h(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)}};0&&(module.exports={ANSI,ShadowRenderer,moveCursor,stripAnsi,writeRaw,xterm});
//# sourceMappingURL=index.js.map