@visulima/ansi
Version:
ANSI escape codes for some terminal swag.
3 lines (2 loc) • 4.64 kB
JavaScript
import{createRequire as F}from"node:module";const x=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,a=x,b=(e,t,i,n)=>{if(i==="length"||i==="prototype"||i==="arguments"||i==="caller")return;const r=Object.getOwnPropertyDescriptor(e,i),s=Object.getOwnPropertyDescriptor(t,i);!S(r,s)&&n||Object.defineProperty(e,i,s)},S=function(e,t){return e===void 0||e.configurable||e.writable===t.writable&&e.enumerable===t.enumerable&&e.configurable===t.configurable&&(e.writable||e.value===t.value)},w=(e,t)=>{const i=Object.getPrototypeOf(t);i!==Object.getPrototypeOf(e)&&Object.setPrototypeOf(e,i)},E=(e,t)=>`/* Wrapped ${e}*/
${t}`,P=Object.getOwnPropertyDescriptor(Function.prototype,"toString"),I=Object.getOwnPropertyDescriptor(Function.prototype.toString,"name"),O=(e,t,i)=>{const n=i===""?"":`with ${i.trim()}() `,r=E.bind(null,n,t.toString());Object.defineProperty(r,"name",I);const{writable:s,enumerable:l,configurable:f}=P;Object.defineProperty(e,"toString",{value:r,writable:s,enumerable:l,configurable:f})};function _(e,t,{ignoreNonConfigurable:i=!1}={}){const{name:n}=e;for(const r of Reflect.ownKeys(t))b(e,t,r,i);return w(e,t),O(e,t,n),e}const u=new WeakMap,g=(e,t={})=>{if(typeof e!="function")throw new TypeError("Expected a function");let i,n=0;const r=e.displayName||e.name||"<anonymous>",s=function(...l){if(u.set(s,++n),n===1)i=e.apply(this,l),e=void 0;else if(t.throw===!0)throw new Error(`Function \`${r}\` can only be called once`);return i};return _(s,e),u.set(s,n),s};g.callCount=e=>{if(!u.has(e))throw new Error(`The given function \`${e.name}\` is not wrapped by the \`onetime\` package`);return u.get(e)};const o=[];o.push("SIGHUP","SIGINT","SIGTERM");process.platform!=="win32"&&o.push("SIGALRM","SIGABRT","SIGVTALRM","SIGXCPU","SIGXFSZ","SIGUSR2","SIGTRAP","SIGSYS","SIGQUIT","SIGIOT");process.platform==="linux"&&o.push("SIGIO","SIGPOLL","SIGPWR","SIGSTKFLT");const c=e=>!!e&&!0&&typeof e.removeListener=="function"&&typeof e.emit=="function"&&typeof e.reallyExit=="function"&&typeof e.listeners=="function"&&typeof e.kill=="function"&&typeof e.pid=="number"&&typeof e.on=="function",h=Symbol.for("signal-exit emitter"),p=globalThis,T=Object.defineProperty.bind(Object);class G{emitted={afterExit:!1,exit:!1};listeners={afterExit:[],exit:[]};count=0;id=Math.random();constructor(){if(p[h])return p[h];T(p,h,{value:this,writable:!1,enumerable:!1,configurable:!1})}on(t,i){this.listeners[t].push(i)}removeListener(t,i){const n=this.listeners[t],r=n.indexOf(i);r!==-1&&(r===0&&n.length===1?n.length=0:n.splice(r,1))}emit(t,i,n){if(this.emitted[t])return!1;this.emitted[t]=!0;let r=!1;for(const s of this.listeners[t])r=s(i,n)===!0||r;return t==="exit"&&(r=this.emit("afterExit",i,n)||r),r}}class y{}const j=e=>({onExit(t,i){return e.onExit(t,i)},load(){return e.load()},unload(){return e.unload()}});class L extends y{onExit(){return()=>{}}load(){}unload(){}}class R extends y{#o=m.platform==="win32"?"SIGINT":"SIGHUP";#e=new G;#t;#r;#s;#n={};#i=!1;constructor(t){super(),this.#t=t,this.#n={};for(const i of o)this.#n[i]=()=>{const n=this.#t.listeners(i);let{count:r}=this.#e;const s=t;if(typeof s.__signal_exit_emitter__=="object"&&typeof s.__signal_exit_emitter__.count=="number"&&(r+=s.__signal_exit_emitter__.count),n.length===r){this.unload();const l=this.#e.emit("exit",null,i),f=i==="SIGHUP"?this.#o:i;l||t.kill(t.pid,f)}};this.#s=t.reallyExit,this.#r=t.emit}onExit(t,i){if(!c(this.#t))return()=>{};this.#i===!1&&this.load();const n=i?.alwaysLast?"afterExit":"exit";return this.#e.on(n,t),()=>{this.#e.removeListener(n,t),this.#e.listeners.exit.length===0&&this.#e.listeners.afterExit.length===0&&this.unload()}}load(){if(!this.#i){this.#i=!0,this.#e.count+=1;for(const t of o)try{const i=this.#n[t];i&&this.#t.on(t,i)}catch{}this.#t.emit=(t,...i)=>this.#a(t,...i),this.#t.reallyExit=t=>this.#l(t)}}unload(){this.#i&&(this.#i=!1,o.forEach(t=>{const i=this.#n[t];if(!i)throw new Error("Listener not defined for signal: "+t);try{this.#t.removeListener(t,i)}catch{}}),this.#t.emit=this.#r,this.#t.reallyExit=this.#s,this.#e.count-=1)}#l(t){return c(this.#t)?(this.#t.exitCode=t||0,this.#e.emit("exit",this.#t.exitCode,null),this.#s.call(this.#t,this.#t.exitCode)):0}#a(t,...i){const n=this.#r;if(t==="exit"&&c(this.#t)){typeof i[0]=="number"&&(this.#t.exitCode=i[0]);const r=n.call(this.#t,t,...i);return this.#e.emit("exit",this.#t.exitCode,null),r}else return n.call(this.#t,t,...i)}}const m=globalThis.process,{onExit:C}=j(c(m)?new R(m):new L),d=a.stderr.isTTY?a.stderr:a.stdout.isTTY?a.stdout:void 0,k=d?g(()=>{C(()=>{d.write("\x1B[?25h")},{alwaysLast:!0})}):()=>{};export{k as default};