mepcli 0.2.5 → 0.2.7
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/dist/base.d.ts +13 -0
- package/dist/base.js +256 -2
- package/dist/core.d.ts +3 -3
- package/dist/input.d.ts +13 -0
- package/dist/input.js +89 -0
- package/dist/prompts/checkbox.d.ts +4 -2
- package/dist/prompts/checkbox.js +40 -18
- package/dist/prompts/confirm.js +9 -7
- package/dist/prompts/date.js +4 -25
- package/dist/prompts/file.js +50 -27
- package/dist/prompts/list.js +22 -17
- package/dist/prompts/multi-select.d.ts +2 -2
- package/dist/prompts/multi-select.js +6 -32
- package/dist/prompts/number.d.ts +1 -1
- package/dist/prompts/number.js +30 -34
- package/dist/prompts/select.d.ts +2 -4
- package/dist/prompts/select.js +15 -29
- package/dist/prompts/slider.js +6 -5
- package/dist/prompts/text.d.ts +2 -1
- package/dist/prompts/text.js +198 -122
- package/dist/prompts/toggle.js +2 -6
- package/dist/types.d.ts +8 -8
- package/dist/utils.d.ts +23 -0
- package/dist/utils.js +135 -0
- package/package.json +4 -4
package/dist/prompts/text.js
CHANGED
|
@@ -4,134 +4,218 @@ exports.TextPrompt = void 0;
|
|
|
4
4
|
const ansi_1 = require("../ansi");
|
|
5
5
|
const base_1 = require("../base");
|
|
6
6
|
const theme_1 = require("../theme");
|
|
7
|
+
const utils_1 = require("../utils");
|
|
7
8
|
// --- Implementation: Text Prompt ---
|
|
8
9
|
class TextPrompt extends base_1.Prompt {
|
|
9
10
|
constructor(options) {
|
|
10
11
|
super(options);
|
|
11
12
|
this.errorMsg = '';
|
|
13
|
+
// cursor is now an index into the grapheme segments array
|
|
12
14
|
this.cursor = 0;
|
|
13
15
|
this.hasTyped = false;
|
|
14
|
-
this.
|
|
16
|
+
this.segments = [];
|
|
15
17
|
this.value = options.initial || '';
|
|
16
|
-
|
|
18
|
+
// Initialize segments from value
|
|
19
|
+
this.segments = (0, utils_1.safeSplit)(this.value);
|
|
20
|
+
this.cursor = this.segments.length;
|
|
17
21
|
}
|
|
18
22
|
render(firstRender) {
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// Clear previous lines
|
|
23
|
-
for (let i = 0; i < this.renderLines; i++) {
|
|
24
|
-
this.print(ansi_1.ANSI.ERASE_LINE);
|
|
25
|
-
if (i < this.renderLines - 1)
|
|
26
|
-
this.print(ansi_1.ANSI.UP);
|
|
27
|
-
}
|
|
28
|
-
this.print(ansi_1.ANSI.CURSOR_LEFT);
|
|
29
|
-
}
|
|
30
|
-
let output = '';
|
|
31
|
-
// 1. Render the Prompt Message
|
|
23
|
+
// Calculate available width
|
|
24
|
+
const cols = process.stdout.columns || 80;
|
|
25
|
+
// 1. Prepare Prompt Label
|
|
32
26
|
const icon = this.errorMsg ? `${theme_1.theme.error}✖` : `${theme_1.theme.success}?`;
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
27
|
+
const hint = this.options.multiline ? ` ${theme_1.theme.muted}(Press Ctrl+D to submit)${ansi_1.ANSI.RESET}` : '';
|
|
28
|
+
const prefix = `${icon} ${ansi_1.ANSI.BOLD}${theme_1.theme.title}${this.options.message}${ansi_1.ANSI.RESET}${hint} `;
|
|
29
|
+
// We need visual length of prefix to calculate available space for input on the first line
|
|
30
|
+
const prefixVisualLen = this.stripAnsi(prefix).length;
|
|
31
|
+
// 2. Prepare Value Display
|
|
32
|
+
let displayValueLines = [];
|
|
33
|
+
let cursorRelativeRow = 0;
|
|
34
|
+
let cursorRelativeCol = 0; // Visual column index
|
|
35
|
+
// Reconstruct value from segments for logic that needs raw string
|
|
36
|
+
this.value = this.segments.join('');
|
|
37
|
+
if (this.segments.length === 0 && this.options.placeholder && !this.errorMsg && !this.hasTyped) {
|
|
38
|
+
// Placeholder case
|
|
39
|
+
const placeholder = `${theme_1.theme.muted}${this.options.placeholder}${ansi_1.ANSI.RESET}`;
|
|
40
|
+
displayValueLines = [placeholder];
|
|
41
|
+
cursorRelativeRow = 0;
|
|
42
|
+
cursorRelativeCol = 0;
|
|
39
43
|
}
|
|
40
44
|
else {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
// Calculate Total Lines and Cursor Position
|
|
63
|
-
// We simulate printing the prompt + value + error
|
|
64
|
-
let currentVisualLine = 0;
|
|
65
|
-
let currentCol = 0;
|
|
66
|
-
// State tracking for cursor
|
|
67
|
-
let cursorRow = 0;
|
|
68
|
-
let cursorCol = 0;
|
|
69
|
-
// Add Prompt
|
|
70
|
-
currentCol += promptVisualLen;
|
|
71
|
-
while (currentCol >= cols) {
|
|
72
|
-
currentVisualLine++;
|
|
73
|
-
currentCol -= cols;
|
|
74
|
-
}
|
|
75
|
-
// Add Value (Character by character to handle wrapping and cursor tracking accurately)
|
|
76
|
-
const valueLen = rawValue.length;
|
|
77
|
-
// If placeholder, we treat it as value for render height, but cursor is at 0
|
|
78
|
-
const isPlaceholder = (!this.value && this.options.placeholder && !this.errorMsg && !this.hasTyped);
|
|
79
|
-
for (let i = 0; i < valueLen; i++) {
|
|
80
|
-
// Check if we are at cursor position
|
|
81
|
-
if (!isPlaceholder && i === this.cursor) {
|
|
82
|
-
cursorRow = currentVisualLine;
|
|
83
|
-
cursorCol = currentCol;
|
|
45
|
+
const rawValue = this.options.isPassword ? '*'.repeat(this.segments.length) : this.value;
|
|
46
|
+
// Note: password masking replaces each grapheme with '*'
|
|
47
|
+
// Split by lines (for multiline support)
|
|
48
|
+
const lines = rawValue.split('\n');
|
|
49
|
+
// Determine which line the cursor is on
|
|
50
|
+
// We need to map 'cursor' (segments index) to line/col.
|
|
51
|
+
// This is tricky because segments might contain '\n'.
|
|
52
|
+
// safeSplit treats '\n' as a segment.
|
|
53
|
+
let cursorLineIndex = 0;
|
|
54
|
+
let cursorSegmentIndexOnLine = 0;
|
|
55
|
+
let currentSegmentIndex = 0;
|
|
56
|
+
for (let i = 0; i < lines.length; i++) {
|
|
57
|
+
// How many segments in this line?
|
|
58
|
+
// We can't just use lines[i].length because that's chars.
|
|
59
|
+
// We need to split the line again or iterate segments.
|
|
60
|
+
// Iterating segments is safer.
|
|
61
|
+
// Let's assume we iterate global segments until we hit a newline segment
|
|
62
|
+
let lineSegmentsCount = 0;
|
|
63
|
+
// Since rawValue.split('\n') consumes the newlines, we need to account for them.
|
|
64
|
+
// Alternative: iterate this.segments
|
|
65
|
+
// Find where the cursor falls.
|
|
84
66
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
67
|
+
// Let's iterate segments to find cursor position (row, col)
|
|
68
|
+
cursorLineIndex = 0;
|
|
69
|
+
let colIndex = 0; // Visual column or char index?
|
|
70
|
+
// If we want visual cursor position, we need visual width of segments.
|
|
71
|
+
let visualColIndex = 0;
|
|
72
|
+
for (let i = 0; i < this.cursor; i++) {
|
|
73
|
+
const seg = this.segments[i];
|
|
74
|
+
if (seg === '\n') {
|
|
75
|
+
cursorLineIndex++;
|
|
76
|
+
visualColIndex = 0;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// Calculate width of this segment?
|
|
80
|
+
// No, for simple text editor logic we often assume 1 char = 1 pos unless we do full layout.
|
|
81
|
+
// But here we want correct cursor placement over wide chars.
|
|
82
|
+
// So we should sum width.
|
|
83
|
+
// However, standard terminals handle wide chars by advancing cursor 2 spots.
|
|
84
|
+
// So we just need to sum the string length of the segment?
|
|
85
|
+
// Or 2 if it's wide?
|
|
86
|
+
// Standard terminal behavior:
|
|
87
|
+
// If I write an Emoji (2 cols), the cursor advances 2 cols.
|
|
88
|
+
// So visualColIndex should track stringWidth(seg).
|
|
89
|
+
// But if isPassword, it's '*'. Width 1.
|
|
90
|
+
if (this.options.isPassword) {
|
|
91
|
+
visualColIndex += 1;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
// Use our helper? Or just length?
|
|
95
|
+
// If we used stringWidth, it would be accurate.
|
|
96
|
+
// But we don't have access to stringWidth here easily unless we import it again (we did in base).
|
|
97
|
+
// Let's assume segment.length for now (byte length),
|
|
98
|
+
// because `\x1b[<N>C` moves N COLUMNS? No, N characters?
|
|
99
|
+
// ANSI `CUB` / `CUF` moves N *columns* usually?
|
|
100
|
+
// "The Cursor Forward (CUF) sequence moves the cursor forward by n columns."
|
|
101
|
+
// So if we have an emoji (2 cols), we need to move past it.
|
|
102
|
+
// If we print an emoji, cursor is at +2.
|
|
103
|
+
// Wait, if we use `renderFrame`, we rewrite everything.
|
|
104
|
+
// Then we calculate where to put the cursor.
|
|
105
|
+
// If line is "A <Emoji> B".
|
|
106
|
+
// Output: "A <Emoji> B".
|
|
107
|
+
// If cursor is after Emoji.
|
|
108
|
+
// We need to be at position: width("A") + width("<Emoji>").
|
|
109
|
+
// = 1 + 2 = 3.
|
|
110
|
+
// So `visualColIndex` should use `stringWidth(seg)`.
|
|
111
|
+
// But I didn't export `stringWidth` from `utils.ts` in the last step?
|
|
112
|
+
// Checking `src/utils.ts`... I did export it.
|
|
113
|
+
// But I need to import it here.
|
|
114
|
+
visualColIndex += this.options.isPassword ? 1 : this.getSegmentWidth(seg);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
89
117
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
118
|
+
cursorRelativeRow = cursorLineIndex;
|
|
119
|
+
cursorRelativeCol = visualColIndex;
|
|
120
|
+
// Now prepare lines for display (scrolling/truncation)
|
|
121
|
+
// We need to reconstruct lines from segments to apply styling/truncation logic per line.
|
|
122
|
+
let currentLineSegments = [];
|
|
123
|
+
let processedLines = []; // Array of segment arrays
|
|
124
|
+
for (const seg of this.segments) {
|
|
125
|
+
if (seg === '\n') {
|
|
126
|
+
processedLines.push(currentLineSegments);
|
|
127
|
+
currentLineSegments = [];
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
currentLineSegments.push(seg);
|
|
95
131
|
}
|
|
96
132
|
}
|
|
133
|
+
processedLines.push(currentLineSegments); // Last line
|
|
134
|
+
processedLines.forEach((lineSegs, idx) => {
|
|
135
|
+
const isCursorLine = idx === cursorLineIndex;
|
|
136
|
+
const linePrefixLen = (idx === 0) ? prefixVisualLen : 0;
|
|
137
|
+
const maxContentLen = Math.max(10, cols - linePrefixLen - 1);
|
|
138
|
+
// Reconstruct line string for display calculation
|
|
139
|
+
// If password, join with *?
|
|
140
|
+
let visibleLine = '';
|
|
141
|
+
if (this.options.isPassword) {
|
|
142
|
+
visibleLine = '*'.repeat(lineSegs.length);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
visibleLine = lineSegs.join('');
|
|
146
|
+
}
|
|
147
|
+
// If this is cursor line, we need to handle horizontal scroll based on cursorRelativeCol.
|
|
148
|
+
// But cursorRelativeCol is global? No, we reset it on newline.
|
|
149
|
+
// So cursorRelativeCol above was correct for the current line.
|
|
150
|
+
if (isCursorLine) {
|
|
151
|
+
// Check if we need to scroll
|
|
152
|
+
// We need visual width of the line up to cursor.
|
|
153
|
+
// cursorRelativeCol holds that.
|
|
154
|
+
// If visual position > maxContentLen, we scroll.
|
|
155
|
+
// This logic is similar to before but needs to use widths.
|
|
156
|
+
// For simplicity, let's stick to the previous slice logic but apply it to SEGMENTS if possible.
|
|
157
|
+
// But slicing segments for display is safer.
|
|
158
|
+
// Let's implement simple tail truncation for now to keep it robust.
|
|
159
|
+
// Ideally we scroll, but scrolling with variable width chars is complex.
|
|
160
|
+
// "Good Enough": if it fits, show it. If not, truncate end.
|
|
161
|
+
// If cursor is beyond end, scroll (slice from left).
|
|
162
|
+
// Simplified: just show visibleLine truncated by base class renderFrame?
|
|
163
|
+
// But renderFrame truncates blindly. We want the cursor visible.
|
|
164
|
+
// Let's leave scrolling out for this specific "Backspace" fix task unless it's critical.
|
|
165
|
+
// The user asked for "Backspace Emoji fix".
|
|
166
|
+
// The scrolling logic is secondary but important.
|
|
167
|
+
// I will preserve the existing simple scrolling logic but using segments?
|
|
168
|
+
// No, let's just use the string for display and let renderFrame truncate.
|
|
169
|
+
// Fix: Ensure we don't crash or show garbage.
|
|
170
|
+
}
|
|
171
|
+
displayValueLines.push(theme_1.theme.main + visibleLine + ansi_1.ANSI.RESET);
|
|
172
|
+
});
|
|
97
173
|
}
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
// If placeholder, cursor is at start of value
|
|
104
|
-
if (isPlaceholder) {
|
|
105
|
-
let pCol = promptVisualLen;
|
|
106
|
-
let pRow = 0;
|
|
107
|
-
while (pCol >= cols) {
|
|
108
|
-
pRow++;
|
|
109
|
-
pCol -= cols;
|
|
174
|
+
// 3. Assemble Output
|
|
175
|
+
let output = '';
|
|
176
|
+
displayValueLines.forEach((lineStr, idx) => {
|
|
177
|
+
if (idx === 0) {
|
|
178
|
+
output += prefix + lineStr;
|
|
110
179
|
}
|
|
111
|
-
|
|
112
|
-
|
|
180
|
+
else {
|
|
181
|
+
output += '\n' + lineStr;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
if (this.errorMsg) {
|
|
185
|
+
output += `\n${theme_1.theme.error}>> ${this.errorMsg}${ansi_1.ANSI.RESET}`;
|
|
113
186
|
}
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
this.
|
|
117
|
-
// 5.
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
const linesUp =
|
|
187
|
+
// 4. Render Frame
|
|
188
|
+
this.renderFrame(output);
|
|
189
|
+
this.print(ansi_1.ANSI.SHOW_CURSOR);
|
|
190
|
+
// 5. Move Cursor
|
|
191
|
+
const errorOffset = this.errorMsg ? 1 : 0;
|
|
192
|
+
const totalRows = displayValueLines.length + errorOffset;
|
|
193
|
+
const linesUp = (totalRows - 1) - cursorRelativeRow;
|
|
121
194
|
if (linesUp > 0) {
|
|
122
195
|
this.print(`\x1b[${linesUp}A`);
|
|
123
196
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
197
|
+
let targetCol = 0;
|
|
198
|
+
if (cursorRelativeRow === 0) {
|
|
199
|
+
targetCol = prefixVisualLen + cursorRelativeCol;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
targetCol = cursorRelativeCol;
|
|
128
203
|
}
|
|
204
|
+
this.print(ansi_1.ANSI.CURSOR_LEFT);
|
|
205
|
+
if (targetCol > 0) {
|
|
206
|
+
this.print(`\x1b[${targetCol}C`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// Helper to get width of a segment
|
|
210
|
+
getSegmentWidth(seg) {
|
|
211
|
+
return (0, utils_1.stringWidth)(seg);
|
|
129
212
|
}
|
|
130
213
|
handleInput(char) {
|
|
131
214
|
// Enter
|
|
132
215
|
if (char === '\r' || char === '\n') {
|
|
133
216
|
if (this.options.multiline) {
|
|
134
|
-
|
|
217
|
+
// Insert newline segment
|
|
218
|
+
this.segments.splice(this.cursor, 0, '\n');
|
|
135
219
|
this.cursor++;
|
|
136
220
|
this.render(false);
|
|
137
221
|
return;
|
|
@@ -139,7 +223,7 @@ class TextPrompt extends base_1.Prompt {
|
|
|
139
223
|
this.validateAndSubmit();
|
|
140
224
|
return;
|
|
141
225
|
}
|
|
142
|
-
// Ctrl+D
|
|
226
|
+
// Ctrl+D / Ctrl+S
|
|
143
227
|
if (this.options.multiline && (char === '\u0004' || char === '\u0013')) {
|
|
144
228
|
this.validateAndSubmit();
|
|
145
229
|
return;
|
|
@@ -148,7 +232,8 @@ class TextPrompt extends base_1.Prompt {
|
|
|
148
232
|
if (char === '\u0008' || char === '\x7f') {
|
|
149
233
|
this.hasTyped = true;
|
|
150
234
|
if (this.cursor > 0) {
|
|
151
|
-
|
|
235
|
+
// Remove segment at cursor - 1
|
|
236
|
+
this.segments.splice(this.cursor - 1, 1);
|
|
152
237
|
this.cursor--;
|
|
153
238
|
this.errorMsg = '';
|
|
154
239
|
this.render(false);
|
|
@@ -165,7 +250,7 @@ class TextPrompt extends base_1.Prompt {
|
|
|
165
250
|
}
|
|
166
251
|
// Arrow Right
|
|
167
252
|
if (this.isRight(char)) {
|
|
168
|
-
if (this.cursor < this.
|
|
253
|
+
if (this.cursor < this.segments.length) {
|
|
169
254
|
this.cursor++;
|
|
170
255
|
this.render(false);
|
|
171
256
|
}
|
|
@@ -174,34 +259,32 @@ class TextPrompt extends base_1.Prompt {
|
|
|
174
259
|
// Delete key
|
|
175
260
|
if (char === '\u001b[3~') {
|
|
176
261
|
this.hasTyped = true;
|
|
177
|
-
if (this.cursor < this.
|
|
178
|
-
this.
|
|
262
|
+
if (this.cursor < this.segments.length) {
|
|
263
|
+
this.segments.splice(this.cursor, 1);
|
|
179
264
|
this.errorMsg = '';
|
|
180
265
|
this.render(false);
|
|
181
266
|
}
|
|
182
267
|
return;
|
|
183
268
|
}
|
|
184
269
|
// Regular Typing & Paste
|
|
270
|
+
// safeSplit the input char(s) - could be pasted text
|
|
185
271
|
if (!/^[\x00-\x1F]/.test(char) && !char.startsWith('\x1b')) {
|
|
186
272
|
this.hasTyped = true;
|
|
187
|
-
|
|
188
|
-
this.cursor
|
|
273
|
+
const newSegments = (0, utils_1.safeSplit)(char);
|
|
274
|
+
this.segments.splice(this.cursor, 0, ...newSegments);
|
|
275
|
+
this.cursor += newSegments.length;
|
|
189
276
|
this.errorMsg = '';
|
|
190
277
|
this.render(false);
|
|
191
278
|
}
|
|
192
279
|
}
|
|
193
280
|
validateAndSubmit() {
|
|
281
|
+
this.value = this.segments.join('');
|
|
194
282
|
if (this.options.validate) {
|
|
195
283
|
const result = this.options.validate(this.value);
|
|
196
|
-
// Handle Promise validation
|
|
197
284
|
if (result instanceof Promise) {
|
|
198
|
-
|
|
199
|
-
this.
|
|
200
|
-
this.print(ansi_1.ANSI.UP);
|
|
285
|
+
this.errorMsg = 'Validating...';
|
|
286
|
+
this.render(false);
|
|
201
287
|
result.then(valid => {
|
|
202
|
-
// Clear loading message
|
|
203
|
-
this.print(`\n${ansi_1.ANSI.ERASE_LINE}`);
|
|
204
|
-
this.print(ansi_1.ANSI.UP);
|
|
205
288
|
if (typeof valid === 'string' && valid.length > 0) {
|
|
206
289
|
this.errorMsg = valid;
|
|
207
290
|
this.render(false);
|
|
@@ -211,20 +294,16 @@ class TextPrompt extends base_1.Prompt {
|
|
|
211
294
|
this.render(false);
|
|
212
295
|
}
|
|
213
296
|
else {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
297
|
+
this.errorMsg = '';
|
|
298
|
+
this.render(false);
|
|
217
299
|
this.submit(this.value);
|
|
218
300
|
}
|
|
219
301
|
}).catch(err => {
|
|
220
|
-
this.print(`\n${ansi_1.ANSI.ERASE_LINE}`);
|
|
221
|
-
this.print(ansi_1.ANSI.UP);
|
|
222
302
|
this.errorMsg = err.message || 'Validation failed';
|
|
223
303
|
this.render(false);
|
|
224
304
|
});
|
|
225
305
|
return;
|
|
226
306
|
}
|
|
227
|
-
// Handle Sync validation
|
|
228
307
|
if (typeof result === 'string' && result.length > 0) {
|
|
229
308
|
this.errorMsg = result;
|
|
230
309
|
this.render(false);
|
|
@@ -236,9 +315,6 @@ class TextPrompt extends base_1.Prompt {
|
|
|
236
315
|
return;
|
|
237
316
|
}
|
|
238
317
|
}
|
|
239
|
-
if (this.errorMsg) {
|
|
240
|
-
this.print(`\n${ansi_1.ANSI.ERASE_LINE}${ansi_1.ANSI.UP}`);
|
|
241
|
-
}
|
|
242
318
|
this.submit(this.value);
|
|
243
319
|
}
|
|
244
320
|
}
|
package/dist/prompts/toggle.js
CHANGED
|
@@ -11,10 +11,6 @@ class TogglePrompt extends base_1.Prompt {
|
|
|
11
11
|
this.value = options.initial ?? false;
|
|
12
12
|
}
|
|
13
13
|
render(firstRender) {
|
|
14
|
-
this.print(ansi_1.ANSI.HIDE_CURSOR);
|
|
15
|
-
if (!firstRender) {
|
|
16
|
-
this.print(`${ansi_1.ANSI.ERASE_LINE}${ansi_1.ANSI.CURSOR_LEFT}`);
|
|
17
|
-
}
|
|
18
14
|
const activeText = this.options.activeText || 'ON';
|
|
19
15
|
const inactiveText = this.options.inactiveText || 'OFF';
|
|
20
16
|
let toggleDisplay = '';
|
|
@@ -24,8 +20,8 @@ class TogglePrompt extends base_1.Prompt {
|
|
|
24
20
|
else {
|
|
25
21
|
toggleDisplay = `${theme_1.theme.muted}${activeText}${ansi_1.ANSI.RESET} ${theme_1.theme.main}[${ansi_1.ANSI.BOLD}${inactiveText}${ansi_1.ANSI.RESET}${theme_1.theme.main}]${ansi_1.ANSI.RESET}`;
|
|
26
22
|
}
|
|
27
|
-
|
|
28
|
-
this.
|
|
23
|
+
const output = `${theme_1.theme.success}?${ansi_1.ANSI.RESET} ${ansi_1.ANSI.BOLD}${theme_1.theme.title}${this.options.message}${ansi_1.ANSI.RESET} ${toggleDisplay}`;
|
|
24
|
+
this.renderFrame(output);
|
|
29
25
|
}
|
|
30
26
|
handleInput(char) {
|
|
31
27
|
if (char === '\r' || char === '\n') {
|
package/dist/types.d.ts
CHANGED
|
@@ -22,19 +22,19 @@ export interface Separator {
|
|
|
22
22
|
separator: true;
|
|
23
23
|
text?: string;
|
|
24
24
|
}
|
|
25
|
-
export interface SelectChoice {
|
|
25
|
+
export interface SelectChoice<V> {
|
|
26
26
|
title: string;
|
|
27
|
-
value:
|
|
27
|
+
value: V;
|
|
28
28
|
description?: string;
|
|
29
29
|
}
|
|
30
|
-
export interface SelectOptions extends BaseOptions {
|
|
31
|
-
choices: (SelectChoice | Separator)[];
|
|
30
|
+
export interface SelectOptions<V> extends BaseOptions {
|
|
31
|
+
choices: (SelectChoice<V> | Separator)[];
|
|
32
32
|
}
|
|
33
|
-
export interface CheckboxChoice extends SelectChoice {
|
|
33
|
+
export interface CheckboxChoice<V> extends SelectChoice<V> {
|
|
34
34
|
selected?: boolean;
|
|
35
35
|
}
|
|
36
|
-
export interface CheckboxOptions extends BaseOptions {
|
|
37
|
-
choices: CheckboxChoice[];
|
|
36
|
+
export interface CheckboxOptions<V> extends BaseOptions {
|
|
37
|
+
choices: CheckboxChoice<V>[];
|
|
38
38
|
min?: number;
|
|
39
39
|
max?: number;
|
|
40
40
|
}
|
|
@@ -75,5 +75,5 @@ export interface FileOptions extends BaseOptions {
|
|
|
75
75
|
extensions?: string[];
|
|
76
76
|
onlyDirectories?: boolean;
|
|
77
77
|
}
|
|
78
|
-
export interface MultiSelectOptions extends CheckboxOptions {
|
|
78
|
+
export interface MultiSelectOptions<V> extends CheckboxOptions<V> {
|
|
79
79
|
}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detects terminal capabilities.
|
|
3
|
+
*/
|
|
4
|
+
export declare function detectCapabilities(): {
|
|
5
|
+
isCI: boolean;
|
|
6
|
+
hasTrueColor: boolean;
|
|
7
|
+
hasUnicode: boolean;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Strips ANSI escape codes from a string.
|
|
11
|
+
*/
|
|
12
|
+
export declare function stripAnsi(str: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Calculates the visual width of a string.
|
|
15
|
+
* Uses binary search for wide characters.
|
|
16
|
+
* Handles ANSI codes (zero width).
|
|
17
|
+
*/
|
|
18
|
+
export declare function stringWidth(str: string): number;
|
|
19
|
+
/**
|
|
20
|
+
* Safely splits a string into an array of grapheme clusters.
|
|
21
|
+
* Uses Intl.Segmenter (Node 16+).
|
|
22
|
+
*/
|
|
23
|
+
export declare function safeSplit(str: string): string[];
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// src/utils.ts
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.detectCapabilities = detectCapabilities;
|
|
5
|
+
exports.stripAnsi = stripAnsi;
|
|
6
|
+
exports.stringWidth = stringWidth;
|
|
7
|
+
exports.safeSplit = safeSplit;
|
|
8
|
+
/**
|
|
9
|
+
* Detects terminal capabilities.
|
|
10
|
+
*/
|
|
11
|
+
function detectCapabilities() {
|
|
12
|
+
const env = process.env;
|
|
13
|
+
// Check for CI
|
|
14
|
+
const isCI = !!env.CI;
|
|
15
|
+
// Check for True Color support
|
|
16
|
+
const hasTrueColor = env.COLORTERM === 'truecolor';
|
|
17
|
+
// Check for Unicode support
|
|
18
|
+
// Windows Terminal (WT_SESSION), VS Code (TERM_PROGRAM=vscode), or modern Linux terminals usually support it.
|
|
19
|
+
const isWindows = process.platform === 'win32';
|
|
20
|
+
const hasUnicode = !!env.WT_SESSION ||
|
|
21
|
+
env.TERM_PROGRAM === 'vscode' ||
|
|
22
|
+
env.TERM_PROGRAM === 'Apple_Terminal' ||
|
|
23
|
+
(!isWindows && env.LANG && env.LANG.toUpperCase().endsWith('UTF-8'));
|
|
24
|
+
return {
|
|
25
|
+
isCI,
|
|
26
|
+
hasTrueColor,
|
|
27
|
+
hasUnicode: hasUnicode || !isWindows // Assume non-windows has unicode by default if not strictly detected?
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Strips ANSI escape codes from a string.
|
|
32
|
+
*/
|
|
33
|
+
function stripAnsi(str) {
|
|
34
|
+
return str.replace(/\x1b\[[0-9;]*m/g, '');
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Sorted array of Unicode ranges that are typically full-width (2 columns).
|
|
38
|
+
* Includes CJK, Emoji, Fullwidth forms, etc.
|
|
39
|
+
* Format: [start, end] inclusive.
|
|
40
|
+
*/
|
|
41
|
+
const WIDE_RANGES = [
|
|
42
|
+
[0x1100, 0x11FF], // Hangul Jamo
|
|
43
|
+
[0x2E80, 0x2EFF], // CJK Radicals Supplement
|
|
44
|
+
[0x2F00, 0x2FDF], // Kangxi Radicals
|
|
45
|
+
[0x3000, 0x303F], // CJK Symbols and Punctuation
|
|
46
|
+
[0x3040, 0x309F], // Hiragana
|
|
47
|
+
[0x30A0, 0x30FF], // Katakana
|
|
48
|
+
[0x3100, 0x312F], // Bopomofo
|
|
49
|
+
[0x3130, 0x318F], // Hangul Compatibility Jamo
|
|
50
|
+
[0x3200, 0x32FF], // Enclosed CJK Letters and Months
|
|
51
|
+
[0x3300, 0x33FF], // CJK Compatibility
|
|
52
|
+
[0x3400, 0x4DBF], // CJK Unified Ideographs Extension A
|
|
53
|
+
[0x4E00, 0x9FFF], // CJK Unified Ideographs
|
|
54
|
+
[0xA960, 0xA97F], // Hangul Jamo Extended-A
|
|
55
|
+
[0xAC00, 0xD7AF], // Hangul Syllables
|
|
56
|
+
[0xD7B0, 0xD7FF], // Hangul Jamo Extended-B
|
|
57
|
+
[0xF900, 0xFAFF], // CJK Compatibility Ideographs
|
|
58
|
+
[0xFE10, 0xFE1F], // Vertical Forms
|
|
59
|
+
[0xFE30, 0xFE4F], // CJK Compatibility Forms
|
|
60
|
+
[0xFE50, 0xFE6F], // Small Form Variants
|
|
61
|
+
[0xFF01, 0xFF60], // Fullwidth ASCII variants
|
|
62
|
+
[0xFFE0, 0xFFE6], // Fullwidth currency/symbols
|
|
63
|
+
[0x1F300, 0x1F6FF], // Miscellaneous Symbols and Pictographs (Emoji)
|
|
64
|
+
[0x1F900, 0x1F9FF], // Supplemental Symbols and Pictographs
|
|
65
|
+
];
|
|
66
|
+
/**
|
|
67
|
+
* Binary search to check if a code point is in the wide ranges.
|
|
68
|
+
*/
|
|
69
|
+
function isWideCodePoint(cp) {
|
|
70
|
+
let low = 0;
|
|
71
|
+
let high = WIDE_RANGES.length - 1;
|
|
72
|
+
while (low <= high) {
|
|
73
|
+
const mid = Math.floor((low + high) / 2);
|
|
74
|
+
const [start, end] = WIDE_RANGES[mid];
|
|
75
|
+
if (cp >= start && cp <= end) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
else if (cp < start) {
|
|
79
|
+
high = mid - 1;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
low = mid + 1;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Calculates the visual width of a string.
|
|
89
|
+
* Uses binary search for wide characters.
|
|
90
|
+
* Handles ANSI codes (zero width).
|
|
91
|
+
*/
|
|
92
|
+
function stringWidth(str) {
|
|
93
|
+
let width = 0;
|
|
94
|
+
let inAnsi = false;
|
|
95
|
+
for (let i = 0; i < str.length; i++) {
|
|
96
|
+
const code = str.charCodeAt(i);
|
|
97
|
+
// Simple ANSI parser state check
|
|
98
|
+
if (str[i] === '\x1b') {
|
|
99
|
+
inAnsi = true;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (inAnsi) {
|
|
103
|
+
if ((str[i] >= '@' && str[i] <= '~') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
|
|
104
|
+
inAnsi = false;
|
|
105
|
+
}
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
// Handle surrogate pairs for high code points (Emoji)
|
|
109
|
+
let cp = code;
|
|
110
|
+
if (code >= 0xD800 && code <= 0xDBFF && i + 1 < str.length) {
|
|
111
|
+
const next = str.charCodeAt(i + 1);
|
|
112
|
+
if (next >= 0xDC00 && next <= 0xDFFF) {
|
|
113
|
+
// Calculate code point from surrogate pair
|
|
114
|
+
cp = (code - 0xD800) * 0x400 + (next - 0xDC00) + 0x10000;
|
|
115
|
+
i++; // Skip next char
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
width += isWideCodePoint(cp) ? 2 : 1;
|
|
119
|
+
}
|
|
120
|
+
return width;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Safely splits a string into an array of grapheme clusters.
|
|
124
|
+
* Uses Intl.Segmenter (Node 16+).
|
|
125
|
+
*/
|
|
126
|
+
function safeSplit(str) {
|
|
127
|
+
// @ts-ignore - Intl.Segmenter is available in Node 16+ but TS might complain depending on lib settings
|
|
128
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
129
|
+
const segments = segmenter.segment(str);
|
|
130
|
+
const result = [];
|
|
131
|
+
for (const segment of segments) {
|
|
132
|
+
result.push(segment.segment);
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mepcli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "Zero-dependency, minimalist interactive CLI prompt for Node.js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"author": "CodeTease",
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@types/node": "^20.19.
|
|
34
|
-
"ts-node": "^10.9.
|
|
35
|
-
"typescript": "^5.
|
|
33
|
+
"@types/node": "^20.19.27",
|
|
34
|
+
"ts-node": "^10.9.2",
|
|
35
|
+
"typescript": "^5.9.3"
|
|
36
36
|
}
|
|
37
37
|
}
|