@visulima/ansi 1.0.13 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,359 @@
1
+ 'use strict';
2
+
3
+ const process$2 = require('node:process');
4
+
5
+ const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
6
+
7
+ const process__default = /*#__PURE__*/_interopDefaultCompat(process$2);
8
+
9
+ var __defProp$2 = Object.defineProperty;
10
+ var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true });
11
+ const copyProperty = /* @__PURE__ */ __name$2((to, from, property, ignoreNonConfigurable) => {
12
+ if (property === "length" || property === "prototype") {
13
+ return;
14
+ }
15
+ if (property === "arguments" || property === "caller") {
16
+ return;
17
+ }
18
+ const toDescriptor = Object.getOwnPropertyDescriptor(to, property);
19
+ const fromDescriptor = Object.getOwnPropertyDescriptor(from, property);
20
+ if (!canCopyProperty(toDescriptor, fromDescriptor) && ignoreNonConfigurable) {
21
+ return;
22
+ }
23
+ Object.defineProperty(to, property, fromDescriptor);
24
+ }, "copyProperty");
25
+ const canCopyProperty = /* @__PURE__ */ __name$2(function(toDescriptor, fromDescriptor) {
26
+ return toDescriptor === undefined || toDescriptor.configurable || toDescriptor.writable === fromDescriptor.writable && toDescriptor.enumerable === fromDescriptor.enumerable && toDescriptor.configurable === fromDescriptor.configurable && (toDescriptor.writable || toDescriptor.value === fromDescriptor.value);
27
+ }, "canCopyProperty");
28
+ const changePrototype = /* @__PURE__ */ __name$2((to, from) => {
29
+ const fromPrototype = Object.getPrototypeOf(from);
30
+ if (fromPrototype === Object.getPrototypeOf(to)) {
31
+ return;
32
+ }
33
+ Object.setPrototypeOf(to, fromPrototype);
34
+ }, "changePrototype");
35
+ const wrappedToString = /* @__PURE__ */ __name$2((withName, fromBody) => `/* Wrapped ${withName}*/
36
+ ${fromBody}`, "wrappedToString");
37
+ const toStringDescriptor = Object.getOwnPropertyDescriptor(Function.prototype, "toString");
38
+ const toStringName = Object.getOwnPropertyDescriptor(Function.prototype.toString, "name");
39
+ const changeToString = /* @__PURE__ */ __name$2((to, from, name) => {
40
+ const withName = name === "" ? "" : `with ${name.trim()}() `;
41
+ const newToString = wrappedToString.bind(null, withName, from.toString());
42
+ Object.defineProperty(newToString, "name", toStringName);
43
+ const { writable, enumerable, configurable } = toStringDescriptor;
44
+ Object.defineProperty(to, "toString", { value: newToString, writable, enumerable, configurable });
45
+ }, "changeToString");
46
+ function mimicFunction(to, from, { ignoreNonConfigurable = false } = {}) {
47
+ const { name } = to;
48
+ for (const property of Reflect.ownKeys(from)) {
49
+ copyProperty(to, from, property, ignoreNonConfigurable);
50
+ }
51
+ changePrototype(to, from);
52
+ changeToString(to, from, name);
53
+ return to;
54
+ }
55
+ __name$2(mimicFunction, "mimicFunction");
56
+
57
+ var __defProp$1 = Object.defineProperty;
58
+ var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true });
59
+ const calledFunctions = /* @__PURE__ */ new WeakMap();
60
+ const onetime = /* @__PURE__ */ __name$1((function_, options = {}) => {
61
+ if (typeof function_ !== "function") {
62
+ throw new TypeError("Expected a function");
63
+ }
64
+ let returnValue;
65
+ let callCount = 0;
66
+ const functionName = function_.displayName || function_.name || "<anonymous>";
67
+ const onetime2 = /* @__PURE__ */ __name$1(function(...arguments_) {
68
+ calledFunctions.set(onetime2, ++callCount);
69
+ if (callCount === 1) {
70
+ returnValue = function_.apply(this, arguments_);
71
+ function_ = undefined;
72
+ } else if (options.throw === true) {
73
+ throw new Error(`Function \`${functionName}\` can only be called once`);
74
+ }
75
+ return returnValue;
76
+ }, "onetime");
77
+ mimicFunction(onetime2, function_);
78
+ calledFunctions.set(onetime2, callCount);
79
+ return onetime2;
80
+ }, "onetime");
81
+ onetime.callCount = (function_) => {
82
+ if (!calledFunctions.has(function_)) {
83
+ throw new Error(`The given function \`${function_.name}\` is not wrapped by the \`onetime\` package`);
84
+ }
85
+ return calledFunctions.get(function_);
86
+ };
87
+
88
+ const signals = [];
89
+ signals.push("SIGHUP", "SIGINT", "SIGTERM");
90
+ if (process.platform !== "win32") {
91
+ signals.push(
92
+ "SIGALRM",
93
+ "SIGABRT",
94
+ "SIGVTALRM",
95
+ "SIGXCPU",
96
+ "SIGXFSZ",
97
+ "SIGUSR2",
98
+ "SIGTRAP",
99
+ "SIGSYS",
100
+ "SIGQUIT",
101
+ "SIGIOT"
102
+ // should detect profiler and enable/disable accordingly.
103
+ // see #21
104
+ // 'SIGPROF'
105
+ );
106
+ }
107
+ if (process.platform === "linux") {
108
+ signals.push("SIGIO", "SIGPOLL", "SIGPWR", "SIGSTKFLT");
109
+ }
110
+
111
+ var __defProp = Object.defineProperty;
112
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
113
+ const processOk = /* @__PURE__ */ __name((process2) => !!process2 && true && typeof process2.removeListener === "function" && typeof process2.emit === "function" && typeof process2.reallyExit === "function" && typeof process2.listeners === "function" && typeof process2.kill === "function" && typeof process2.pid === "number" && typeof process2.on === "function", "processOk");
114
+ const kExitEmitter = Symbol.for("signal-exit emitter");
115
+ const global = globalThis;
116
+ const ObjectDefineProperty = Object.defineProperty.bind(Object);
117
+ class Emitter {
118
+ static {
119
+ __name(this, "Emitter");
120
+ }
121
+ emitted = {
122
+ afterExit: false,
123
+ exit: false
124
+ };
125
+ listeners = {
126
+ afterExit: [],
127
+ exit: []
128
+ };
129
+ count = 0;
130
+ id = Math.random();
131
+ constructor() {
132
+ if (global[kExitEmitter]) {
133
+ return global[kExitEmitter];
134
+ }
135
+ ObjectDefineProperty(global, kExitEmitter, {
136
+ value: this,
137
+ writable: false,
138
+ enumerable: false,
139
+ configurable: false
140
+ });
141
+ }
142
+ on(ev, fn) {
143
+ this.listeners[ev].push(fn);
144
+ }
145
+ removeListener(ev, fn) {
146
+ const list = this.listeners[ev];
147
+ const i = list.indexOf(fn);
148
+ if (i === -1) {
149
+ return;
150
+ }
151
+ if (i === 0 && list.length === 1) {
152
+ list.length = 0;
153
+ } else {
154
+ list.splice(i, 1);
155
+ }
156
+ }
157
+ emit(ev, code, signal) {
158
+ if (this.emitted[ev]) {
159
+ return false;
160
+ }
161
+ this.emitted[ev] = true;
162
+ let ret = false;
163
+ for (const fn of this.listeners[ev]) {
164
+ ret = fn(code, signal) === true || ret;
165
+ }
166
+ if (ev === "exit") {
167
+ ret = this.emit("afterExit", code, signal) || ret;
168
+ }
169
+ return ret;
170
+ }
171
+ }
172
+ class SignalExitBase {
173
+ static {
174
+ __name(this, "SignalExitBase");
175
+ }
176
+ }
177
+ const signalExitWrap = /* @__PURE__ */ __name((handler) => {
178
+ return {
179
+ onExit(cb, opts) {
180
+ return handler.onExit(cb, opts);
181
+ },
182
+ load() {
183
+ return handler.load();
184
+ },
185
+ unload() {
186
+ return handler.unload();
187
+ }
188
+ };
189
+ }, "signalExitWrap");
190
+ class SignalExitFallback extends SignalExitBase {
191
+ static {
192
+ __name(this, "SignalExitFallback");
193
+ }
194
+ onExit() {
195
+ return () => {
196
+ };
197
+ }
198
+ load() {
199
+ }
200
+ unload() {
201
+ }
202
+ }
203
+ class SignalExit extends SignalExitBase {
204
+ static {
205
+ __name(this, "SignalExit");
206
+ }
207
+ // "SIGHUP" throws an `ENOSYS` error on Windows,
208
+ // so use a supported signal instead
209
+ /* c8 ignore start */
210
+ #hupSig = process$1.platform === "win32" ? "SIGINT" : "SIGHUP";
211
+ /* c8 ignore stop */
212
+ #emitter = new Emitter();
213
+ #process;
214
+ #originalProcessEmit;
215
+ #originalProcessReallyExit;
216
+ #sigListeners = {};
217
+ #loaded = false;
218
+ constructor(process2) {
219
+ super();
220
+ this.#process = process2;
221
+ this.#sigListeners = {};
222
+ for (const sig of signals) {
223
+ this.#sigListeners[sig] = () => {
224
+ const listeners = this.#process.listeners(sig);
225
+ let { count } = this.#emitter;
226
+ const p = process2;
227
+ if (typeof p.__signal_exit_emitter__ === "object" && typeof p.__signal_exit_emitter__.count === "number") {
228
+ count += p.__signal_exit_emitter__.count;
229
+ }
230
+ if (listeners.length === count) {
231
+ this.unload();
232
+ const ret = this.#emitter.emit("exit", null, sig);
233
+ const s = sig === "SIGHUP" ? this.#hupSig : sig;
234
+ if (!ret)
235
+ process2.kill(process2.pid, s);
236
+ }
237
+ };
238
+ }
239
+ this.#originalProcessReallyExit = process2.reallyExit;
240
+ this.#originalProcessEmit = process2.emit;
241
+ }
242
+ onExit(cb, opts) {
243
+ if (!processOk(this.#process)) {
244
+ return () => {
245
+ };
246
+ }
247
+ if (this.#loaded === false) {
248
+ this.load();
249
+ }
250
+ const ev = opts?.alwaysLast ? "afterExit" : "exit";
251
+ this.#emitter.on(ev, cb);
252
+ return () => {
253
+ this.#emitter.removeListener(ev, cb);
254
+ if (this.#emitter.listeners["exit"].length === 0 && this.#emitter.listeners["afterExit"].length === 0) {
255
+ this.unload();
256
+ }
257
+ };
258
+ }
259
+ load() {
260
+ if (this.#loaded) {
261
+ return;
262
+ }
263
+ this.#loaded = true;
264
+ this.#emitter.count += 1;
265
+ for (const sig of signals) {
266
+ try {
267
+ const fn = this.#sigListeners[sig];
268
+ if (fn)
269
+ this.#process.on(sig, fn);
270
+ } catch (_) {
271
+ }
272
+ }
273
+ this.#process.emit = (ev, ...a) => {
274
+ return this.#processEmit(ev, ...a);
275
+ };
276
+ this.#process.reallyExit = (code) => {
277
+ return this.#processReallyExit(code);
278
+ };
279
+ }
280
+ unload() {
281
+ if (!this.#loaded) {
282
+ return;
283
+ }
284
+ this.#loaded = false;
285
+ signals.forEach((sig) => {
286
+ const listener = this.#sigListeners[sig];
287
+ if (!listener) {
288
+ throw new Error("Listener not defined for signal: " + sig);
289
+ }
290
+ try {
291
+ this.#process.removeListener(sig, listener);
292
+ } catch (_) {
293
+ }
294
+ });
295
+ this.#process.emit = this.#originalProcessEmit;
296
+ this.#process.reallyExit = this.#originalProcessReallyExit;
297
+ this.#emitter.count -= 1;
298
+ }
299
+ #processReallyExit(code) {
300
+ if (!processOk(this.#process)) {
301
+ return 0;
302
+ }
303
+ this.#process.exitCode = code || 0;
304
+ this.#emitter.emit("exit", this.#process.exitCode, null);
305
+ return this.#originalProcessReallyExit.call(this.#process, this.#process.exitCode);
306
+ }
307
+ #processEmit(ev, ...args) {
308
+ const og = this.#originalProcessEmit;
309
+ if (ev === "exit" && processOk(this.#process)) {
310
+ if (typeof args[0] === "number") {
311
+ this.#process.exitCode = args[0];
312
+ }
313
+ const ret = og.call(this.#process, ev, ...args);
314
+ this.#emitter.emit("exit", this.#process.exitCode, null);
315
+ return ret;
316
+ } else {
317
+ return og.call(this.#process, ev, ...args);
318
+ }
319
+ }
320
+ }
321
+ const process$1 = globalThis.process;
322
+ const {
323
+ /**
324
+ * Called when the process is exiting, whether via signal, explicit
325
+ * exit, or running out of stuff to do.
326
+ *
327
+ * If the global process object is not suitable for instrumentation,
328
+ * then this will be a no-op.
329
+ *
330
+ * Returns a function that may be used to unload signal-exit.
331
+ */
332
+ onExit,
333
+ /**
334
+ * Load the listeners. Likely you never need to call this, unless
335
+ * doing a rather deep integration with signal-exit functionality.
336
+ * Mostly exposed for the benefit of testing.
337
+ *
338
+ * @internal
339
+ */
340
+ load,
341
+ /**
342
+ * Unload the listeners. Likely you never need to call this, unless
343
+ * doing a rather deep integration with signal-exit functionality.
344
+ * Mostly exposed for the benefit of testing.
345
+ *
346
+ * @internal
347
+ */
348
+ unload
349
+ } = signalExitWrap(processOk(process$1) ? new SignalExit(process$1) : new SignalExitFallback());
350
+
351
+ const terminal = process__default.stderr.isTTY ? process__default.stderr : process__default.stdout.isTTY ? process__default.stdout : undefined;
352
+ const restoreCursor = terminal ? onetime(() => {
353
+ onExit(() => {
354
+ terminal.write("\x1B[?25h");
355
+ }, { alwaysLast: true });
356
+ }) : () => {
357
+ };
358
+
359
+ module.exports = restoreCursor;
@@ -0,0 +1,353 @@
1
+ import process$2 from 'node:process';
2
+
3
+ var __defProp$2 = Object.defineProperty;
4
+ var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true });
5
+ const copyProperty = /* @__PURE__ */ __name$2((to, from, property, ignoreNonConfigurable) => {
6
+ if (property === "length" || property === "prototype") {
7
+ return;
8
+ }
9
+ if (property === "arguments" || property === "caller") {
10
+ return;
11
+ }
12
+ const toDescriptor = Object.getOwnPropertyDescriptor(to, property);
13
+ const fromDescriptor = Object.getOwnPropertyDescriptor(from, property);
14
+ if (!canCopyProperty(toDescriptor, fromDescriptor) && ignoreNonConfigurable) {
15
+ return;
16
+ }
17
+ Object.defineProperty(to, property, fromDescriptor);
18
+ }, "copyProperty");
19
+ const canCopyProperty = /* @__PURE__ */ __name$2(function(toDescriptor, fromDescriptor) {
20
+ return toDescriptor === undefined || toDescriptor.configurable || toDescriptor.writable === fromDescriptor.writable && toDescriptor.enumerable === fromDescriptor.enumerable && toDescriptor.configurable === fromDescriptor.configurable && (toDescriptor.writable || toDescriptor.value === fromDescriptor.value);
21
+ }, "canCopyProperty");
22
+ const changePrototype = /* @__PURE__ */ __name$2((to, from) => {
23
+ const fromPrototype = Object.getPrototypeOf(from);
24
+ if (fromPrototype === Object.getPrototypeOf(to)) {
25
+ return;
26
+ }
27
+ Object.setPrototypeOf(to, fromPrototype);
28
+ }, "changePrototype");
29
+ const wrappedToString = /* @__PURE__ */ __name$2((withName, fromBody) => `/* Wrapped ${withName}*/
30
+ ${fromBody}`, "wrappedToString");
31
+ const toStringDescriptor = Object.getOwnPropertyDescriptor(Function.prototype, "toString");
32
+ const toStringName = Object.getOwnPropertyDescriptor(Function.prototype.toString, "name");
33
+ const changeToString = /* @__PURE__ */ __name$2((to, from, name) => {
34
+ const withName = name === "" ? "" : `with ${name.trim()}() `;
35
+ const newToString = wrappedToString.bind(null, withName, from.toString());
36
+ Object.defineProperty(newToString, "name", toStringName);
37
+ const { writable, enumerable, configurable } = toStringDescriptor;
38
+ Object.defineProperty(to, "toString", { value: newToString, writable, enumerable, configurable });
39
+ }, "changeToString");
40
+ function mimicFunction(to, from, { ignoreNonConfigurable = false } = {}) {
41
+ const { name } = to;
42
+ for (const property of Reflect.ownKeys(from)) {
43
+ copyProperty(to, from, property, ignoreNonConfigurable);
44
+ }
45
+ changePrototype(to, from);
46
+ changeToString(to, from, name);
47
+ return to;
48
+ }
49
+ __name$2(mimicFunction, "mimicFunction");
50
+
51
+ var __defProp$1 = Object.defineProperty;
52
+ var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true });
53
+ const calledFunctions = /* @__PURE__ */ new WeakMap();
54
+ const onetime = /* @__PURE__ */ __name$1((function_, options = {}) => {
55
+ if (typeof function_ !== "function") {
56
+ throw new TypeError("Expected a function");
57
+ }
58
+ let returnValue;
59
+ let callCount = 0;
60
+ const functionName = function_.displayName || function_.name || "<anonymous>";
61
+ const onetime2 = /* @__PURE__ */ __name$1(function(...arguments_) {
62
+ calledFunctions.set(onetime2, ++callCount);
63
+ if (callCount === 1) {
64
+ returnValue = function_.apply(this, arguments_);
65
+ function_ = undefined;
66
+ } else if (options.throw === true) {
67
+ throw new Error(`Function \`${functionName}\` can only be called once`);
68
+ }
69
+ return returnValue;
70
+ }, "onetime");
71
+ mimicFunction(onetime2, function_);
72
+ calledFunctions.set(onetime2, callCount);
73
+ return onetime2;
74
+ }, "onetime");
75
+ onetime.callCount = (function_) => {
76
+ if (!calledFunctions.has(function_)) {
77
+ throw new Error(`The given function \`${function_.name}\` is not wrapped by the \`onetime\` package`);
78
+ }
79
+ return calledFunctions.get(function_);
80
+ };
81
+
82
+ const signals = [];
83
+ signals.push("SIGHUP", "SIGINT", "SIGTERM");
84
+ if (process.platform !== "win32") {
85
+ signals.push(
86
+ "SIGALRM",
87
+ "SIGABRT",
88
+ "SIGVTALRM",
89
+ "SIGXCPU",
90
+ "SIGXFSZ",
91
+ "SIGUSR2",
92
+ "SIGTRAP",
93
+ "SIGSYS",
94
+ "SIGQUIT",
95
+ "SIGIOT"
96
+ // should detect profiler and enable/disable accordingly.
97
+ // see #21
98
+ // 'SIGPROF'
99
+ );
100
+ }
101
+ if (process.platform === "linux") {
102
+ signals.push("SIGIO", "SIGPOLL", "SIGPWR", "SIGSTKFLT");
103
+ }
104
+
105
+ var __defProp = Object.defineProperty;
106
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
107
+ const processOk = /* @__PURE__ */ __name((process2) => !!process2 && true && typeof process2.removeListener === "function" && typeof process2.emit === "function" && typeof process2.reallyExit === "function" && typeof process2.listeners === "function" && typeof process2.kill === "function" && typeof process2.pid === "number" && typeof process2.on === "function", "processOk");
108
+ const kExitEmitter = Symbol.for("signal-exit emitter");
109
+ const global = globalThis;
110
+ const ObjectDefineProperty = Object.defineProperty.bind(Object);
111
+ class Emitter {
112
+ static {
113
+ __name(this, "Emitter");
114
+ }
115
+ emitted = {
116
+ afterExit: false,
117
+ exit: false
118
+ };
119
+ listeners = {
120
+ afterExit: [],
121
+ exit: []
122
+ };
123
+ count = 0;
124
+ id = Math.random();
125
+ constructor() {
126
+ if (global[kExitEmitter]) {
127
+ return global[kExitEmitter];
128
+ }
129
+ ObjectDefineProperty(global, kExitEmitter, {
130
+ value: this,
131
+ writable: false,
132
+ enumerable: false,
133
+ configurable: false
134
+ });
135
+ }
136
+ on(ev, fn) {
137
+ this.listeners[ev].push(fn);
138
+ }
139
+ removeListener(ev, fn) {
140
+ const list = this.listeners[ev];
141
+ const i = list.indexOf(fn);
142
+ if (i === -1) {
143
+ return;
144
+ }
145
+ if (i === 0 && list.length === 1) {
146
+ list.length = 0;
147
+ } else {
148
+ list.splice(i, 1);
149
+ }
150
+ }
151
+ emit(ev, code, signal) {
152
+ if (this.emitted[ev]) {
153
+ return false;
154
+ }
155
+ this.emitted[ev] = true;
156
+ let ret = false;
157
+ for (const fn of this.listeners[ev]) {
158
+ ret = fn(code, signal) === true || ret;
159
+ }
160
+ if (ev === "exit") {
161
+ ret = this.emit("afterExit", code, signal) || ret;
162
+ }
163
+ return ret;
164
+ }
165
+ }
166
+ class SignalExitBase {
167
+ static {
168
+ __name(this, "SignalExitBase");
169
+ }
170
+ }
171
+ const signalExitWrap = /* @__PURE__ */ __name((handler) => {
172
+ return {
173
+ onExit(cb, opts) {
174
+ return handler.onExit(cb, opts);
175
+ },
176
+ load() {
177
+ return handler.load();
178
+ },
179
+ unload() {
180
+ return handler.unload();
181
+ }
182
+ };
183
+ }, "signalExitWrap");
184
+ class SignalExitFallback extends SignalExitBase {
185
+ static {
186
+ __name(this, "SignalExitFallback");
187
+ }
188
+ onExit() {
189
+ return () => {
190
+ };
191
+ }
192
+ load() {
193
+ }
194
+ unload() {
195
+ }
196
+ }
197
+ class SignalExit extends SignalExitBase {
198
+ static {
199
+ __name(this, "SignalExit");
200
+ }
201
+ // "SIGHUP" throws an `ENOSYS` error on Windows,
202
+ // so use a supported signal instead
203
+ /* c8 ignore start */
204
+ #hupSig = process$1.platform === "win32" ? "SIGINT" : "SIGHUP";
205
+ /* c8 ignore stop */
206
+ #emitter = new Emitter();
207
+ #process;
208
+ #originalProcessEmit;
209
+ #originalProcessReallyExit;
210
+ #sigListeners = {};
211
+ #loaded = false;
212
+ constructor(process2) {
213
+ super();
214
+ this.#process = process2;
215
+ this.#sigListeners = {};
216
+ for (const sig of signals) {
217
+ this.#sigListeners[sig] = () => {
218
+ const listeners = this.#process.listeners(sig);
219
+ let { count } = this.#emitter;
220
+ const p = process2;
221
+ if (typeof p.__signal_exit_emitter__ === "object" && typeof p.__signal_exit_emitter__.count === "number") {
222
+ count += p.__signal_exit_emitter__.count;
223
+ }
224
+ if (listeners.length === count) {
225
+ this.unload();
226
+ const ret = this.#emitter.emit("exit", null, sig);
227
+ const s = sig === "SIGHUP" ? this.#hupSig : sig;
228
+ if (!ret)
229
+ process2.kill(process2.pid, s);
230
+ }
231
+ };
232
+ }
233
+ this.#originalProcessReallyExit = process2.reallyExit;
234
+ this.#originalProcessEmit = process2.emit;
235
+ }
236
+ onExit(cb, opts) {
237
+ if (!processOk(this.#process)) {
238
+ return () => {
239
+ };
240
+ }
241
+ if (this.#loaded === false) {
242
+ this.load();
243
+ }
244
+ const ev = opts?.alwaysLast ? "afterExit" : "exit";
245
+ this.#emitter.on(ev, cb);
246
+ return () => {
247
+ this.#emitter.removeListener(ev, cb);
248
+ if (this.#emitter.listeners["exit"].length === 0 && this.#emitter.listeners["afterExit"].length === 0) {
249
+ this.unload();
250
+ }
251
+ };
252
+ }
253
+ load() {
254
+ if (this.#loaded) {
255
+ return;
256
+ }
257
+ this.#loaded = true;
258
+ this.#emitter.count += 1;
259
+ for (const sig of signals) {
260
+ try {
261
+ const fn = this.#sigListeners[sig];
262
+ if (fn)
263
+ this.#process.on(sig, fn);
264
+ } catch (_) {
265
+ }
266
+ }
267
+ this.#process.emit = (ev, ...a) => {
268
+ return this.#processEmit(ev, ...a);
269
+ };
270
+ this.#process.reallyExit = (code) => {
271
+ return this.#processReallyExit(code);
272
+ };
273
+ }
274
+ unload() {
275
+ if (!this.#loaded) {
276
+ return;
277
+ }
278
+ this.#loaded = false;
279
+ signals.forEach((sig) => {
280
+ const listener = this.#sigListeners[sig];
281
+ if (!listener) {
282
+ throw new Error("Listener not defined for signal: " + sig);
283
+ }
284
+ try {
285
+ this.#process.removeListener(sig, listener);
286
+ } catch (_) {
287
+ }
288
+ });
289
+ this.#process.emit = this.#originalProcessEmit;
290
+ this.#process.reallyExit = this.#originalProcessReallyExit;
291
+ this.#emitter.count -= 1;
292
+ }
293
+ #processReallyExit(code) {
294
+ if (!processOk(this.#process)) {
295
+ return 0;
296
+ }
297
+ this.#process.exitCode = code || 0;
298
+ this.#emitter.emit("exit", this.#process.exitCode, null);
299
+ return this.#originalProcessReallyExit.call(this.#process, this.#process.exitCode);
300
+ }
301
+ #processEmit(ev, ...args) {
302
+ const og = this.#originalProcessEmit;
303
+ if (ev === "exit" && processOk(this.#process)) {
304
+ if (typeof args[0] === "number") {
305
+ this.#process.exitCode = args[0];
306
+ }
307
+ const ret = og.call(this.#process, ev, ...args);
308
+ this.#emitter.emit("exit", this.#process.exitCode, null);
309
+ return ret;
310
+ } else {
311
+ return og.call(this.#process, ev, ...args);
312
+ }
313
+ }
314
+ }
315
+ const process$1 = globalThis.process;
316
+ const {
317
+ /**
318
+ * Called when the process is exiting, whether via signal, explicit
319
+ * exit, or running out of stuff to do.
320
+ *
321
+ * If the global process object is not suitable for instrumentation,
322
+ * then this will be a no-op.
323
+ *
324
+ * Returns a function that may be used to unload signal-exit.
325
+ */
326
+ onExit,
327
+ /**
328
+ * Load the listeners. Likely you never need to call this, unless
329
+ * doing a rather deep integration with signal-exit functionality.
330
+ * Mostly exposed for the benefit of testing.
331
+ *
332
+ * @internal
333
+ */
334
+ load,
335
+ /**
336
+ * Unload the listeners. Likely you never need to call this, unless
337
+ * doing a rather deep integration with signal-exit functionality.
338
+ * Mostly exposed for the benefit of testing.
339
+ *
340
+ * @internal
341
+ */
342
+ unload
343
+ } = signalExitWrap(processOk(process$1) ? new SignalExit(process$1) : new SignalExitFallback());
344
+
345
+ const terminal = process$2.stderr.isTTY ? process$2.stderr : process$2.stdout.isTTY ? process$2.stdout : undefined;
346
+ const restoreCursor = terminal ? onetime(() => {
347
+ onExit(() => {
348
+ terminal.write("\x1B[?25h");
349
+ }, { alwaysLast: true });
350
+ }) : () => {
351
+ };
352
+
353
+ export { restoreCursor as default };