| 392 | z3 = /* @__PURE__ */ new Map([["k", "up"], ["j", "down"], ["h", "left"], ["l", "right"]]); |
| 393 | lD2 = /* @__PURE__ */ new Set(["up", "down", "left", "right", "space", "enter"]); |
| 394 | x3 = class { |
| 395 | constructor({ render: u3, input: F5 = import_node_process.stdin, output: e3 = import_node_process.stdout, ...s2 }, C5 = true) { |
| 396 | a(this, "input"), a(this, "output"), a(this, "rl"), a(this, "opts"), a(this, "_track", false), a(this, "_render"), a(this, "_cursor", 0), a(this, "state", "initial"), a(this, "value"), a(this, "error", ""), a(this, "subscribers", /* @__PURE__ */ new Map()), a(this, "_prevFrame", ""), this.opts = s2, this.onKeypress = this.onKeypress.bind(this), this.close = this.close.bind(this), this.render = this.render.bind(this), this._render = u3.bind(this), this._track = C5, this.input = F5, this.output = e3; |
| 397 | } |
| 398 | prompt() { |
| 399 | const u3 = new import_node_tty.WriteStream(0); |
| 400 | return u3._write = (F5, e3, s2) => { |
| 401 | this._track && (this.value = this.rl.line.replace(/\t/g, ""), this._cursor = this.rl.cursor, this.emit("value", this.value)), s2(); |
| 402 | }, this.input.pipe(u3), this.rl = import_node_readline.default.createInterface({ input: this.input, output: u3, tabSize: 2, prompt: "", escapeCodeTimeout: 50 }), import_node_readline.default.emitKeypressEvents(this.input, this.rl), this.rl.prompt(), this.opts.initialValue !== void 0 && this._track && this.rl.write(this.opts.initialValue), this.input.on("keypress", this.onKeypress), v3(this.input, true), this.output.on("resize", this.render), this.render(), new Promise((F5, e3) => { |
| 403 | this.once("submit", () => { |
| 404 | this.output.write(import_sisteransi.cursor.show), this.output.off("resize", this.render), v3(this.input, false), F5(this.value); |
| 405 | }), this.once("cancel", () => { |
| 406 | this.output.write(import_sisteransi.cursor.show), this.output.off("resize", this.render), v3(this.input, false), F5(V4); |
| 407 | }); |
| 408 | }); |
| 409 | } |
| 410 | on(u3, F5) { |
| 411 | const e3 = this.subscribers.get(u3) ?? []; |
| 412 | e3.push({ cb: F5 }), this.subscribers.set(u3, e3); |
| 413 | } |
| 414 | once(u3, F5) { |
| 415 | const e3 = this.subscribers.get(u3) ?? []; |
| 416 | e3.push({ cb: F5, once: true }), this.subscribers.set(u3, e3); |
| 417 | } |
| 418 | emit(u3, ...F5) { |
| 419 | const e3 = this.subscribers.get(u3) ?? [], s2 = []; |
| 420 | for (const C5 of e3) C5.cb(...F5), C5.once && s2.push(() => e3.splice(e3.indexOf(C5), 1)); |
| 421 | for (const C5 of s2) C5(); |
| 422 | } |
| 423 | unsubscribe() { |
| 424 | this.subscribers.clear(); |
| 425 | } |
| 426 | onKeypress(u3, F5) { |
| 427 | if (this.state === "error" && (this.state = "active"), F5?.name && !this._track && z3.has(F5.name) && this.emit("cursor", z3.get(F5.name)), F5?.name && lD2.has(F5.name) && this.emit("cursor", F5.name), u3 && (u3.toLowerCase() === "y" || u3.toLowerCase() === "n") && this.emit("confirm", u3.toLowerCase() === "y"), u3 === " " && this.opts.placeholder && (this.value || (this.rl.write(this.opts.placeholder), this.emit("value", this.opts.placeholder))), u3 && this.emit("key", u3.toLowerCase()), F5?.name === "return") { |
| 428 | if (this.opts.validate) { |
| 429 | const e3 = this.opts.validate(this.value); |
| 430 | e3 && (this.error = e3, this.state = "error", this.rl.write(this.value)); |
| 431 | } |
| 432 | this.state !== "error" && (this.state = "submit"); |
| 433 | } |
| 434 | u3 === "" && (this.state = "cancel"), (this.state === "submit" || this.state === "cancel") && this.emit("finalize"), this.render(), (this.state === "submit" || this.state === "cancel") && this.close(); |
| 435 | } |
| 436 | close() { |
| 437 | this.input.unpipe(), this.input.removeListener("keypress", this.onKeypress), this.output.write(` |
| 438 | `), v3(this.input, false), this.rl.close(), this.emit(`${this.state}`, this.value), this.unsubscribe(); |
| 439 | } |
| 440 | restoreCursor() { |
| 441 | const u3 = R4(this._prevFrame, process.stdout.columns, { hard: true }).split(` |
| 442 | `).length - 1; |
| 443 | this.output.write(import_sisteransi.cursor.move(-999, u3 * -1)); |
| 444 | } |
| 445 | render() { |
| 446 | const u3 = R4(this._render(this) ?? "", process.stdout.columns, { hard: true }); |
| 447 | if (u3 !== this._prevFrame) { |
| 448 | if (this.state === "initial") this.output.write(import_sisteransi.cursor.hide); |
| 449 | else { |
| 450 | const F5 = aD2(this._prevFrame, u3); |
| 451 | if (this.restoreCursor(), F5 && F5?.length === 1) { |
nothing calls this directly
no outgoing calls
no test coverage detected