/* Socket Lib - Built with esbuild */
var m=Object.create;var d=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var C=(e,t)=>{for(var i in t)d(e,i,{get:t[i],enumerable:!0})},c=(e,t,i,p)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of v(t))!T.call(e,o)&&o!==i&&d(e,o,{get:()=>t[o],enumerable:!(p=S(t,o))||p.enumerable});return e};var h=(e,t,i)=>(i=e!=null?m(M(e)):{},c(t||!e||!e.__esModule?d(i,"default",{value:e,enumerable:!0}):i,e)),B=e=>c(d({},"__esModule",{value:!0}),e);var R={};C(R,{attachOutputMask:()=>x,createKeyboardHandler:()=>w,createOutputMask:()=>b,runWithMask:()=>E});module.exports=B(R);var g=require("node:child_process"),O=h(require("node:readline")),f=require("../spinner.js"),l=require("./clear.js"),a=require("./stdout.js");function b(e={}){const{showOutput:t=!1}=e;return{isSpinning:!t,outputBuffer:[],stderrCapture:"",stdoutCapture:"",verbose:t}}function w(e,t,i={}){const{message:p="Running\u2026",toggleText:o="to see full output"}=i;return(s,n)=>{if(n?.ctrl&&n.name==="o")if(e.verbose=!e.verbose,e.verbose)e.isSpinning&&(f.spinner.stop(),e.isSpinning=!1),(0,l.clearLine)(),e.outputBuffer.length>0&&(console.log("--- Output (ctrl+o to hide) ---"),e.outputBuffer.forEach(r=>{(0,a.write)(r)}));else{if(e.outputBuffer.length>0){const r=e.outputBuffer.join("").split(`
`).length+1;for(let u=0;u<r;u+=1)process.stdout.write("\x1B[1A\x1B[2K")}(0,l.clearLine)(),e.outputBuffer=[],e.isSpinning||(f.spinner.start(`${p} (ctrl+o ${o})`),e.isSpinning=!0)}else if(n?.ctrl&&n.name==="c")throw t.kill("SIGTERM"),process.stdin.isTTY&&process.stdin.setRawMode(!1),new Error("Process cancelled by user")}}function x(e,t={}){return new Promise((i,p)=>{const{message:o="Running\u2026"}=t,s=b(t);if(s.isSpinning&&process.stdout.isTTY&&f.spinner.start(`${o} (ctrl+o ${t.toggleText||"to see full output"})`),process.stdin.isTTY){O.default.emitKeypressEvents(process.stdin),process.stdin.setRawMode(!0);const n=w(s,e,t);process.stdin.on("keypress",n),e.on("exit",()=>{process.stdin.isTTY&&(process.stdin.setRawMode(!1),process.stdin.removeListener("keypress",n))})}e.stdout&&e.stdout.on("data",n=>{const r=n.toString();if(s.stdoutCapture+=r,!(t.filterOutput&&!t.filterOutput(r,"stdout")))if(s.verbose)(0,a.write)(r);else{s.outputBuffer.push(r);const u=s.outputBuffer.join("").split(`
`);u.length>1e3&&(s.outputBuffer=[u.slice(-1e3).join(`
`)])}}),e.stderr&&e.stderr.on("data",n=>{const r=n.toString();s.stderrCapture+=r,!(t.filterOutput&&!t.filterOutput(r,"stderr"))&&(s.verbose?process.stderr.write(r):s.outputBuffer.push(r))}),e.on("exit",n=>{process.stdin.isTTY&&process.stdin.setRawMode(!1);let r=n||0;if(t.overrideExitCode){const u=t.overrideExitCode(r,s.stdoutCapture,s.stderrCapture);u!==void 0&&(r=u)}s.isSpinning&&(r===0?f.spinner.successAndStop(`${o} completed`):(f.spinner.failAndStop(`${o} failed`),s.outputBuffer.length>0&&!s.verbose&&(console.log(`
--- Output ---`),s.outputBuffer.forEach(u=>{(0,a.write)(u)})))),i(r)}),e.on("error",n=>{process.stdin.isTTY&&process.stdin.setRawMode(!1),s.isSpinning&&f.spinner.failAndStop(`${o} error`),p(n)})})}async function E(e,t=[],i={}){const{message:p="Running\u2026",showOutput:o=!1,toggleText:s="to see output",...n}=i,r=(0,g.spawn)(e,t,{stdio:["inherit","pipe","pipe"],...n});return await x(r,{message:p,showOutput:o,toggleText:s})}0&&(module.exports={attachOutputMask,createKeyboardHandler,createOutputMask,runWithMask});
//# sourceMappingURL=mask.js.map
