@socketsecurity/lib 5.19.0 → 5.19.1
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.
- package/CHANGELOG.md +14 -0
- package/dist/constants/socket.js +1 -1
- package/dist/external/@inquirer/checkbox.js +5 -0
- package/dist/external/@inquirer/confirm.js +5 -0
- package/dist/external/@inquirer/input.js +5 -0
- package/dist/external/@inquirer/password.js +5 -0
- package/dist/external/@inquirer/search.js +5 -0
- package/dist/external/@inquirer/select.js +5 -0
- package/dist/external/@npmcli/package-json.js +3968 -9
- package/dist/external/debug.js +328 -162
- package/dist/external/external-pack.js +2749 -28
- package/dist/external/npm-pack.js +44319 -24912
- package/dist/external/zod.js +7558 -160
- package/dist/stdio/clear.d.ts +163 -0
- package/dist/stdio/clear.js +96 -0
- package/dist/stdio/progress.d.ts +152 -0
- package/dist/stdio/progress.js +217 -0
- package/dist/stdio/prompts.d.ts +196 -0
- package/dist/stdio/prompts.js +177 -0
- package/package.json +20 -2
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Terminal clearing and cursor utilities.
|
|
3
|
+
* Provides functions for clearing lines, screens, and managing cursor position.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Clear the current line in the terminal.
|
|
7
|
+
* Uses native TTY methods when available, falls back to ANSI escape codes.
|
|
8
|
+
* Uses native TTY methods when available and falls back to `\r\x1b[K` ANSI
|
|
9
|
+
* escapes on non-TTY streams.
|
|
10
|
+
*
|
|
11
|
+
* ANSI Sequences:
|
|
12
|
+
* - `\r`: Carriage return (move to line start)
|
|
13
|
+
* - `\x1b[K`: Clear from cursor to end of line
|
|
14
|
+
*
|
|
15
|
+
* @param stream - Output stream to clear (defaults to `process.stdout`)
|
|
16
|
+
* @default stream process.stdout
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* clearLine() // Clear current line on stdout
|
|
21
|
+
* clearLine(process.stderr) // Clear on stderr
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function clearLine(stream?: NodeJS.WriteStream): void;
|
|
25
|
+
/**
|
|
26
|
+
* Clear multiple lines above the current cursor position.
|
|
27
|
+
* Useful for clearing multi-line output like progress bars or status messages.
|
|
28
|
+
*
|
|
29
|
+
* ANSI Sequences:
|
|
30
|
+
* - `\x1b[1A`: Move cursor up one line
|
|
31
|
+
* - `\x1b[2K`: Erase entire line
|
|
32
|
+
*
|
|
33
|
+
* @param count - Number of lines to clear
|
|
34
|
+
* @param stream - Output stream to clear
|
|
35
|
+
* @default stream process.stdout
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* console.log('Line 1')
|
|
40
|
+
* console.log('Line 2')
|
|
41
|
+
* console.log('Line 3')
|
|
42
|
+
* clearLines(2) // Clears lines 2 and 3
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function clearLines(count: number, stream?: NodeJS.WriteStream): void;
|
|
46
|
+
/**
|
|
47
|
+
* Clear the entire screen and reset cursor to top-left.
|
|
48
|
+
* Only works in TTY environments.
|
|
49
|
+
*
|
|
50
|
+
* ANSI Sequence:
|
|
51
|
+
* - `\x1bc`: Full reset (clear screen and move cursor home)
|
|
52
|
+
*
|
|
53
|
+
* @param stream - Output stream to clear
|
|
54
|
+
* @default stream process.stdout
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* clearScreen() // Clear entire terminal
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function clearScreen(stream?: NodeJS.WriteStream): void;
|
|
62
|
+
/**
|
|
63
|
+
* Clear the visible terminal screen.
|
|
64
|
+
* Alias for `clearScreen()`.
|
|
65
|
+
*
|
|
66
|
+
* @param stream - Output stream to clear
|
|
67
|
+
* @default stream process.stdout
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* clearVisible() // Same as clearScreen()
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function clearVisible(stream?: NodeJS.WriteStream): void;
|
|
75
|
+
/**
|
|
76
|
+
* Move cursor to the beginning of the current line.
|
|
77
|
+
* Uses native TTY methods when available, falls back to carriage return.
|
|
78
|
+
*
|
|
79
|
+
* @param stream - Output stream to manipulate
|
|
80
|
+
* @default stream process.stdout
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* process.stdout.write('Some text...')
|
|
85
|
+
* cursorToStart()
|
|
86
|
+
* process.stdout.write('New text') // Overwrites from start
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare function cursorToStart(stream?: NodeJS.WriteStream): void;
|
|
90
|
+
/**
|
|
91
|
+
* Hide the terminal cursor.
|
|
92
|
+
* Useful for cleaner output during animations or progress indicators.
|
|
93
|
+
*
|
|
94
|
+
* ANSI Sequence:
|
|
95
|
+
* - `\x1b[?25l`: DECTCEM hide cursor
|
|
96
|
+
*
|
|
97
|
+
* @param stream - Output stream to manipulate
|
|
98
|
+
* @default stream process.stdout
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* hideCursor()
|
|
103
|
+
* // ... show animation
|
|
104
|
+
* showCursor()
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function hideCursor(stream?: NodeJS.WriteStream): void;
|
|
108
|
+
/**
|
|
109
|
+
* Restore cursor to previously saved position.
|
|
110
|
+
* Must be called after `saveCursor()`.
|
|
111
|
+
*
|
|
112
|
+
* ANSI Sequence:
|
|
113
|
+
* - `\x1b8`: DECRC restore cursor
|
|
114
|
+
*
|
|
115
|
+
* @param stream - Output stream to manipulate
|
|
116
|
+
* @default stream process.stdout
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* saveCursor()
|
|
121
|
+
* console.log('Temporary text')
|
|
122
|
+
* restoreCursor()
|
|
123
|
+
* console.log('Back at saved position')
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare function restoreCursor(stream?: NodeJS.WriteStream): void;
|
|
127
|
+
/**
|
|
128
|
+
* Save the current cursor position.
|
|
129
|
+
* Can be restored later with `restoreCursor()`.
|
|
130
|
+
*
|
|
131
|
+
* ANSI Sequence:
|
|
132
|
+
* - `\x1b7`: DECSC save cursor
|
|
133
|
+
*
|
|
134
|
+
* @param stream - Output stream to manipulate
|
|
135
|
+
* @default stream process.stdout
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* saveCursor()
|
|
140
|
+
* console.log('Temporary text')
|
|
141
|
+
* restoreCursor()
|
|
142
|
+
* console.log('Back at saved position')
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export declare function saveCursor(stream?: NodeJS.WriteStream): void;
|
|
146
|
+
/**
|
|
147
|
+
* Show the terminal cursor.
|
|
148
|
+
* Should be called after `hideCursor()` to restore normal cursor visibility.
|
|
149
|
+
*
|
|
150
|
+
* ANSI Sequence:
|
|
151
|
+
* - `\x1b[?25h`: DECTCEM show cursor
|
|
152
|
+
*
|
|
153
|
+
* @param stream - Output stream to manipulate
|
|
154
|
+
* @default stream process.stdout
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* hideCursor()
|
|
159
|
+
* // ... show animation
|
|
160
|
+
* showCursor()
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export declare function showCursor(stream?: NodeJS.WriteStream): void;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Socket Lib - Built with esbuild */
|
|
3
|
+
"use strict";
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __export = (target, all) => {
|
|
11
|
+
for (var name in all)
|
|
12
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
28
|
+
mod
|
|
29
|
+
));
|
|
30
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
31
|
+
var clear_exports = {};
|
|
32
|
+
__export(clear_exports, {
|
|
33
|
+
clearLine: () => clearLine,
|
|
34
|
+
clearLines: () => clearLines,
|
|
35
|
+
clearScreen: () => clearScreen,
|
|
36
|
+
clearVisible: () => clearVisible,
|
|
37
|
+
cursorToStart: () => cursorToStart,
|
|
38
|
+
hideCursor: () => hideCursor,
|
|
39
|
+
restoreCursor: () => restoreCursor,
|
|
40
|
+
saveCursor: () => saveCursor,
|
|
41
|
+
showCursor: () => showCursor
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(clear_exports);
|
|
44
|
+
var import_node_process = __toESM(require("node:process"));
|
|
45
|
+
function clearLine(stream = import_node_process.default.stdout) {
|
|
46
|
+
if (stream.isTTY) {
|
|
47
|
+
stream.cursorTo(0);
|
|
48
|
+
stream.clearLine(0);
|
|
49
|
+
} else {
|
|
50
|
+
stream.write("\r\x1B[K");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function clearLines(count, stream = import_node_process.default.stdout) {
|
|
54
|
+
for (let i = 0; i < count; i++) {
|
|
55
|
+
stream.write("\x1B[1A\x1B[2K");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function clearScreen(stream = import_node_process.default.stdout) {
|
|
59
|
+
if (stream.isTTY) {
|
|
60
|
+
stream.write("\x1Bc");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function clearVisible(stream = import_node_process.default.stdout) {
|
|
64
|
+
clearScreen(stream);
|
|
65
|
+
}
|
|
66
|
+
function cursorToStart(stream = import_node_process.default.stdout) {
|
|
67
|
+
if (stream.isTTY) {
|
|
68
|
+
stream.cursorTo(0);
|
|
69
|
+
} else {
|
|
70
|
+
stream.write("\r");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function hideCursor(stream = import_node_process.default.stdout) {
|
|
74
|
+
stream.write("\x1B[?25l");
|
|
75
|
+
}
|
|
76
|
+
function restoreCursor(stream = import_node_process.default.stdout) {
|
|
77
|
+
stream.write("\x1B8");
|
|
78
|
+
}
|
|
79
|
+
function saveCursor(stream = import_node_process.default.stdout) {
|
|
80
|
+
stream.write("\x1B7");
|
|
81
|
+
}
|
|
82
|
+
function showCursor(stream = import_node_process.default.stdout) {
|
|
83
|
+
stream.write("\x1B[?25h");
|
|
84
|
+
}
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
clearLine,
|
|
88
|
+
clearLines,
|
|
89
|
+
clearScreen,
|
|
90
|
+
clearVisible,
|
|
91
|
+
cursorToStart,
|
|
92
|
+
hideCursor,
|
|
93
|
+
restoreCursor,
|
|
94
|
+
saveCursor,
|
|
95
|
+
showCursor
|
|
96
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Progress bar utilities for CLI applications.
|
|
3
|
+
* Provides various progress indicators including bars, percentages, and spinners.
|
|
4
|
+
*/
|
|
5
|
+
export interface ProgressBarOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Width of the progress bar in characters.
|
|
8
|
+
* @default 40
|
|
9
|
+
*/
|
|
10
|
+
width?: number | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Format template for progress bar display.
|
|
13
|
+
* Available tokens: `:bar`, `:percent`, `:current`, `:total`, `:elapsed`, `:eta`.
|
|
14
|
+
* Custom tokens can be passed via the `tokens` parameter in `update()` or `tick()`.
|
|
15
|
+
* @default ':bar :percent :current/:total'
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* format: ':bar :percent :current/:total :eta'
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
format?: string | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Character(s) to use for completed portion of bar.
|
|
24
|
+
* @default '█'
|
|
25
|
+
*/
|
|
26
|
+
complete?: string | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Character(s) to use for incomplete portion of bar.
|
|
29
|
+
* @default '░'
|
|
30
|
+
*/
|
|
31
|
+
incomplete?: string | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Character(s) to use for the head of the progress bar.
|
|
34
|
+
* @default ''
|
|
35
|
+
*/
|
|
36
|
+
head?: string | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Clear the progress bar when complete.
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
clear?: boolean | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Minimum time between renders in milliseconds.
|
|
44
|
+
* ~60fps = 16ms throttle.
|
|
45
|
+
* @default 16
|
|
46
|
+
*/
|
|
47
|
+
renderThrottle?: number | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Stream to write progress bar output to.
|
|
50
|
+
* @default process.stderr
|
|
51
|
+
*/
|
|
52
|
+
stream?: NodeJS.WriteStream | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Color to apply to the completed portion of the bar.
|
|
55
|
+
* @default 'cyan'
|
|
56
|
+
*/
|
|
57
|
+
color?: 'cyan' | 'green' | 'yellow' | 'blue' | 'magenta' | undefined;
|
|
58
|
+
}
|
|
59
|
+
export declare class ProgressBar {
|
|
60
|
+
private current;
|
|
61
|
+
private total;
|
|
62
|
+
private startTime;
|
|
63
|
+
private lastRender;
|
|
64
|
+
private stream;
|
|
65
|
+
private options;
|
|
66
|
+
private terminated;
|
|
67
|
+
private lastDrawnWidth;
|
|
68
|
+
/**
|
|
69
|
+
* Create a new progress bar instance.
|
|
70
|
+
*
|
|
71
|
+
* @param total - Total number of units for the progress bar
|
|
72
|
+
* @param options - Configuration options for the progress bar
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const bar = new ProgressBar(100, {
|
|
77
|
+
* width: 50,
|
|
78
|
+
* format: ':bar :percent :current/:total :eta',
|
|
79
|
+
* color: 'green'
|
|
80
|
+
* })
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
constructor(total: number, options?: ProgressBarOptions);
|
|
84
|
+
/**
|
|
85
|
+
* Update progress to a specific value and redraw the bar.
|
|
86
|
+
* Updates are throttled to prevent excessive rendering (default ~60fps).
|
|
87
|
+
*
|
|
88
|
+
* @param current - Current progress value (will be clamped to total)
|
|
89
|
+
* @param tokens - Optional custom tokens to replace in format string
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* bar.update(50)
|
|
94
|
+
* bar.update(75, { status: 'Processing...' })
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
update(current: number, tokens?: Record<string, unknown>): void;
|
|
98
|
+
/**
|
|
99
|
+
* Increment progress by a specified amount.
|
|
100
|
+
* Convenience method for `update(current + amount)`.
|
|
101
|
+
*
|
|
102
|
+
* @param amount - Amount to increment by
|
|
103
|
+
* @param tokens - Optional custom tokens to replace in format string
|
|
104
|
+
* @default amount 1
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* bar.tick() // Increment by 1
|
|
109
|
+
* bar.tick(5) // Increment by 5
|
|
110
|
+
* bar.tick(1, { file: 'data.json' })
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
tick(amount?: number, tokens?: Record<string, unknown>): void;
|
|
114
|
+
/**
|
|
115
|
+
* Render the progress bar.
|
|
116
|
+
*/
|
|
117
|
+
private render;
|
|
118
|
+
/**
|
|
119
|
+
* Clear the current line.
|
|
120
|
+
*/
|
|
121
|
+
private clearLine;
|
|
122
|
+
/**
|
|
123
|
+
* Format time in seconds to human readable.
|
|
124
|
+
*/
|
|
125
|
+
private formatTime;
|
|
126
|
+
/**
|
|
127
|
+
* Terminate the progress bar and optionally clear it.
|
|
128
|
+
* Called automatically when progress reaches 100%.
|
|
129
|
+
* If `clear` option is true, removes the bar from terminal.
|
|
130
|
+
* Otherwise, moves to next line to preserve the final state.
|
|
131
|
+
*/
|
|
132
|
+
terminate(): void;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Create a simple progress indicator without a graphical bar.
|
|
136
|
+
* Returns a formatted string showing progress as percentage and fraction.
|
|
137
|
+
*
|
|
138
|
+
* @param current - Current progress value
|
|
139
|
+
* @param total - Total progress value
|
|
140
|
+
* @param label - Optional label prefix
|
|
141
|
+
* @returns Formatted progress indicator string
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* createProgressIndicator(50, 100)
|
|
146
|
+
* // Returns: '[50%] 50/100'
|
|
147
|
+
*
|
|
148
|
+
* createProgressIndicator(3, 10, 'Files')
|
|
149
|
+
* // Returns: 'Files: [30%] 3/10'
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function createProgressIndicator(current: number, total: number, label?: string | undefined): string;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Socket Lib - Built with esbuild */
|
|
3
|
+
"use strict";
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __export = (target, all) => {
|
|
11
|
+
for (var name in all)
|
|
12
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
28
|
+
mod
|
|
29
|
+
));
|
|
30
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
31
|
+
var progress_exports = {};
|
|
32
|
+
__export(progress_exports, {
|
|
33
|
+
ProgressBar: () => ProgressBar,
|
|
34
|
+
createProgressIndicator: () => createProgressIndicator
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(progress_exports);
|
|
37
|
+
var import_node_process = __toESM(require("node:process"));
|
|
38
|
+
var import_yoctocolors_cjs = __toESM(require("../external/yoctocolors-cjs"));
|
|
39
|
+
var import_strings = require("../strings");
|
|
40
|
+
class ProgressBar {
|
|
41
|
+
current = 0;
|
|
42
|
+
total;
|
|
43
|
+
startTime;
|
|
44
|
+
lastRender = 0;
|
|
45
|
+
stream;
|
|
46
|
+
options;
|
|
47
|
+
terminated = false;
|
|
48
|
+
lastDrawnWidth = 0;
|
|
49
|
+
/**
|
|
50
|
+
* Create a new progress bar instance.
|
|
51
|
+
*
|
|
52
|
+
* @param total - Total number of units for the progress bar
|
|
53
|
+
* @param options - Configuration options for the progress bar
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const bar = new ProgressBar(100, {
|
|
58
|
+
* width: 50,
|
|
59
|
+
* format: ':bar :percent :current/:total :eta',
|
|
60
|
+
* color: 'green'
|
|
61
|
+
* })
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
constructor(total, options) {
|
|
65
|
+
this.total = total;
|
|
66
|
+
this.startTime = Date.now();
|
|
67
|
+
this.stream = options?.stream || import_node_process.default.stderr;
|
|
68
|
+
this.options = {
|
|
69
|
+
width: 40,
|
|
70
|
+
format: ":bar :percent :current/:total",
|
|
71
|
+
complete: "\u2588",
|
|
72
|
+
incomplete: "\u2591",
|
|
73
|
+
head: "",
|
|
74
|
+
clear: false,
|
|
75
|
+
// ~60fps.
|
|
76
|
+
renderThrottle: 16,
|
|
77
|
+
stream: this.stream,
|
|
78
|
+
color: "cyan",
|
|
79
|
+
...options
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Update progress to a specific value and redraw the bar.
|
|
84
|
+
* Updates are throttled to prevent excessive rendering (default ~60fps).
|
|
85
|
+
*
|
|
86
|
+
* @param current - Current progress value (will be clamped to total)
|
|
87
|
+
* @param tokens - Optional custom tokens to replace in format string
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* bar.update(50)
|
|
92
|
+
* bar.update(75, { status: 'Processing...' })
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
update(current, tokens) {
|
|
96
|
+
if (this.terminated) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
this.current = Math.min(current, this.total);
|
|
100
|
+
const now = Date.now();
|
|
101
|
+
if (now - this.lastRender < (this.options.renderThrottle ?? 16) && this.current < this.total) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.lastRender = now;
|
|
105
|
+
this.render(tokens);
|
|
106
|
+
if (this.current >= this.total) {
|
|
107
|
+
this.terminate();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Increment progress by a specified amount.
|
|
112
|
+
* Convenience method for `update(current + amount)`.
|
|
113
|
+
*
|
|
114
|
+
* @param amount - Amount to increment by
|
|
115
|
+
* @param tokens - Optional custom tokens to replace in format string
|
|
116
|
+
* @default amount 1
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* bar.tick() // Increment by 1
|
|
121
|
+
* bar.tick(5) // Increment by 5
|
|
122
|
+
* bar.tick(1, { file: 'data.json' })
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
tick(amount = 1, tokens) {
|
|
126
|
+
this.update(this.current + amount, tokens);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Render the progress bar.
|
|
130
|
+
*/
|
|
131
|
+
render(tokens) {
|
|
132
|
+
const colorName = this.options.color ?? "cyan";
|
|
133
|
+
const colorFn = import_yoctocolors_cjs.default[colorName] || ((s) => s);
|
|
134
|
+
const percent = this.total === 0 ? 0 : Math.floor(this.current / this.total * 100);
|
|
135
|
+
const elapsed = Date.now() - this.startTime;
|
|
136
|
+
const eta = this.current === 0 ? 0 : elapsed / this.current * (this.total - this.current);
|
|
137
|
+
const availableWidth = this.options.width ?? 40;
|
|
138
|
+
const filledWidth = this.total === 0 ? 0 : Math.min(
|
|
139
|
+
availableWidth,
|
|
140
|
+
Math.floor(this.current / this.total * availableWidth)
|
|
141
|
+
);
|
|
142
|
+
const emptyWidth = Math.max(0, availableWidth - filledWidth);
|
|
143
|
+
const filled = (0, import_strings.repeatString)(this.options.complete ?? "\u2588", filledWidth);
|
|
144
|
+
const empty = (0, import_strings.repeatString)(this.options.incomplete ?? "\u2591", emptyWidth);
|
|
145
|
+
const bar = colorFn(filled) + empty;
|
|
146
|
+
let output = this.options.format ?? ":bar :percent :current/:total";
|
|
147
|
+
output = output.replace(":bar", bar);
|
|
148
|
+
output = output.replace(":percent", `${percent}%`);
|
|
149
|
+
output = output.replace(":current", String(this.current));
|
|
150
|
+
output = output.replace(":total", String(this.total));
|
|
151
|
+
output = output.replace(":elapsed", this.formatTime(elapsed));
|
|
152
|
+
output = output.replace(":eta", this.formatTime(eta));
|
|
153
|
+
if (tokens) {
|
|
154
|
+
for (const [key, value] of Object.entries(tokens)) {
|
|
155
|
+
output = output.replace(`:${key}`, String(value));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
this.clearLine();
|
|
159
|
+
this.stream.write(output);
|
|
160
|
+
this.lastDrawnWidth = (0, import_strings.stripAnsi)(output).length;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Clear the current line.
|
|
164
|
+
*/
|
|
165
|
+
clearLine() {
|
|
166
|
+
if (this.stream.isTTY) {
|
|
167
|
+
this.stream.cursorTo(0);
|
|
168
|
+
this.stream.clearLine(0);
|
|
169
|
+
} else if (this.lastDrawnWidth > 0) {
|
|
170
|
+
this.stream.write(`\r${(0, import_strings.repeatString)(" ", this.lastDrawnWidth)}\r`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Format time in seconds to human readable.
|
|
175
|
+
*/
|
|
176
|
+
formatTime(ms) {
|
|
177
|
+
const seconds = Math.round(ms / 1e3);
|
|
178
|
+
if (seconds < 60) {
|
|
179
|
+
return `${seconds}s`;
|
|
180
|
+
}
|
|
181
|
+
const minutes = Math.floor(seconds / 60);
|
|
182
|
+
const remainingSeconds = seconds % 60;
|
|
183
|
+
return `${minutes}m${remainingSeconds}s`;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Terminate the progress bar and optionally clear it.
|
|
187
|
+
* Called automatically when progress reaches 100%.
|
|
188
|
+
* If `clear` option is true, removes the bar from terminal.
|
|
189
|
+
* Otherwise, moves to next line to preserve the final state.
|
|
190
|
+
*/
|
|
191
|
+
terminate() {
|
|
192
|
+
if (this.terminated) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
this.terminated = true;
|
|
196
|
+
if (this.options.clear) {
|
|
197
|
+
this.clearLine();
|
|
198
|
+
} else {
|
|
199
|
+
this.stream.write("\n");
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function createProgressIndicator(current, total, label) {
|
|
204
|
+
const percent = total === 0 ? 0 : Math.floor(current / total * 100);
|
|
205
|
+
const progress = `${current}/${total}`;
|
|
206
|
+
let output = "";
|
|
207
|
+
if (label) {
|
|
208
|
+
output += `${label}: `;
|
|
209
|
+
}
|
|
210
|
+
output += `${import_yoctocolors_cjs.default.cyan(`[${percent}%]`)} ${progress}`;
|
|
211
|
+
return output;
|
|
212
|
+
}
|
|
213
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
214
|
+
0 && (module.exports = {
|
|
215
|
+
ProgressBar,
|
|
216
|
+
createProgressIndicator
|
|
217
|
+
});
|