@bemoje/cli 1.1.0 → 2.0.0
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/index.d.ts +27 -1
- package/index.mjs +1352 -0
- package/index.mjs.map +7 -0
- package/lib/Command.d.ts +97 -262
- package/lib/Help.d.ts +37 -83
- package/lib/helpers/findCommand.d.ts +6 -0
- package/lib/helpers/findOption.d.ts +5 -0
- package/lib/helpers/getCommandAncestors.d.ts +5 -0
- package/lib/helpers/getCommandAndAncestors.d.ts +5 -0
- package/lib/helpers/parseOptionFlags.d.ts +11 -0
- package/lib/internal/collectVariadicOptionValues.d.ts +7 -0
- package/lib/internal/mergeOptionDefaults.d.ts +3 -0
- package/lib/internal/normalizeArgv.d.ts +3 -0
- package/lib/internal/resolveArguments.d.ts +3 -0
- package/lib/internal/validateParsed.d.ts +3 -0
- package/lib/types.d.ts +383 -0
- package/package.json +35 -29
- package/LICENSE +0 -21
- package/README.md +0 -246
- package/index.js +0 -3
- package/lib/Command.js +0 -420
- package/lib/Help.js +0 -474
package/lib/Help.js
DELETED
|
@@ -1,474 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
class Help {
|
|
4
|
-
static {
|
|
5
|
-
__name(this, "Help");
|
|
6
|
-
}
|
|
7
|
-
/** output helpWidth, long lines are wrapped to fit */
|
|
8
|
-
helpWidth = process.stdout.isTTY ? process.stdout.columns : 80;
|
|
9
|
-
minWidthToWrap = 40;
|
|
10
|
-
sortSubcommands;
|
|
11
|
-
sortOptions;
|
|
12
|
-
showGlobalOptions;
|
|
13
|
-
/**
|
|
14
|
-
* Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one.
|
|
15
|
-
*/
|
|
16
|
-
visibleCommands(cmd) {
|
|
17
|
-
const visibleCommands = cmd.commands.filter((cmd2) => !cmd2.hidden);
|
|
18
|
-
if (this.sortSubcommands) {
|
|
19
|
-
visibleCommands.sort((a, b) => {
|
|
20
|
-
return a.name.localeCompare(b.name);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
return visibleCommands;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Compare options for sort.
|
|
27
|
-
*/
|
|
28
|
-
compareOptions(a, b) {
|
|
29
|
-
const getSortKey = /* @__PURE__ */ __name((option) => {
|
|
30
|
-
return option.short ? option.short.replace(/^-/, "") : option.long.replace(/^--/, "");
|
|
31
|
-
}, "getSortKey");
|
|
32
|
-
return getSortKey(a).localeCompare(getSortKey(b));
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one.
|
|
36
|
-
*/
|
|
37
|
-
visibleOptions(cmd) {
|
|
38
|
-
const visibleOptions = cmd.options.filter((option) => !option.hidden);
|
|
39
|
-
if (this.sortOptions) {
|
|
40
|
-
visibleOptions.sort(this.compareOptions);
|
|
41
|
-
}
|
|
42
|
-
return visibleOptions;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Get an array of the visible global options. (Not including help.)
|
|
46
|
-
*/
|
|
47
|
-
visibleGlobalOptions(cmd) {
|
|
48
|
-
if (!this.showGlobalOptions) return [];
|
|
49
|
-
const globalOptions = [];
|
|
50
|
-
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
51
|
-
const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
|
|
52
|
-
globalOptions.push(...visibleOptions);
|
|
53
|
-
}
|
|
54
|
-
if (this.sortOptions) {
|
|
55
|
-
globalOptions.sort(this.compareOptions);
|
|
56
|
-
}
|
|
57
|
-
return globalOptions;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Get an array of the arguments if any have a description.
|
|
61
|
-
*/
|
|
62
|
-
visibleArguments(cmd) {
|
|
63
|
-
if (cmd.arguments.find((argument) => argument.description)) {
|
|
64
|
-
return [...cmd.arguments];
|
|
65
|
-
}
|
|
66
|
-
return [];
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Get the command term to show in the list of subcommands.
|
|
70
|
-
*/
|
|
71
|
-
subcommandTerm(cmd) {
|
|
72
|
-
const args = cmd.arguments.map((arg) => {
|
|
73
|
-
const nameOutput = arg.name + (arg.variadic === true ? "..." : "");
|
|
74
|
-
return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
|
|
75
|
-
}).join(" ");
|
|
76
|
-
return cmd.name + (cmd.aliases[0] ? "|" + cmd.aliases[0] : "") + (cmd.options.length ? " [options]" : "") + // simplistic check for non-help option
|
|
77
|
-
(args ? " " + args : "");
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Get the option term to show in the list of options.
|
|
81
|
-
*/
|
|
82
|
-
optionTerm(option) {
|
|
83
|
-
return option.flags;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Get the argument term to show in the list of arguments.
|
|
87
|
-
*/
|
|
88
|
-
argumentTerm(argument) {
|
|
89
|
-
return argument.name;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Get the longest command term length.
|
|
93
|
-
*/
|
|
94
|
-
longestSubcommandTermLength(cmd, helper) {
|
|
95
|
-
return helper.visibleCommands(cmd).reduce((max, command) => {
|
|
96
|
-
return Math.max(max, this.displayWidth(helper.styleSubcommandTerm(helper.subcommandTerm(command))));
|
|
97
|
-
}, 0);
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Get the longest option term length.
|
|
101
|
-
*/
|
|
102
|
-
longestOptionTermLength(cmd, helper) {
|
|
103
|
-
return helper.visibleOptions(cmd).reduce((max, option) => {
|
|
104
|
-
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
105
|
-
}, 0);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Get the longest global option term length.
|
|
109
|
-
*/
|
|
110
|
-
longestGlobalOptionTermLength(cmd, helper) {
|
|
111
|
-
return helper.visibleGlobalOptions(cmd).reduce((max, option) => {
|
|
112
|
-
return Math.max(max, this.displayWidth(helper.styleOptionTerm(helper.optionTerm(option))));
|
|
113
|
-
}, 0);
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Get the longest argument term length.
|
|
117
|
-
*/
|
|
118
|
-
longestArgumentTermLength(cmd, helper) {
|
|
119
|
-
return helper.visibleArguments(cmd).reduce((max, argument) => {
|
|
120
|
-
return Math.max(max, this.displayWidth(helper.styleArgumentTerm(helper.argumentTerm(argument))));
|
|
121
|
-
}, 0);
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Get the command usage to be displayed at the top of the built-in help.
|
|
125
|
-
*/
|
|
126
|
-
commandUsage(cmd) {
|
|
127
|
-
let cmdName = cmd.name;
|
|
128
|
-
if (cmd.aliases[0]) {
|
|
129
|
-
cmdName = cmdName + "|" + cmd.aliases[0];
|
|
130
|
-
}
|
|
131
|
-
let ancestorCmdNames = "";
|
|
132
|
-
for (let ancestorCmd = cmd.parent; ancestorCmd; ancestorCmd = ancestorCmd.parent) {
|
|
133
|
-
ancestorCmdNames = ancestorCmd.name + " " + ancestorCmdNames;
|
|
134
|
-
}
|
|
135
|
-
return ancestorCmdNames + cmdName + " " + [
|
|
136
|
-
...cmd.options.length ? ["[options]"] : [],
|
|
137
|
-
...cmd.commands.length ? ["[command]"] : [],
|
|
138
|
-
...cmd.arguments.map((arg) => {
|
|
139
|
-
return arg.required ? arg.variadic ? `<${arg.name}...>` : `<${arg.name}>` : arg.variadic ? `[${arg.name}...]` : `[${arg.name}]`;
|
|
140
|
-
})
|
|
141
|
-
].join(" ");
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Get the description for the command.
|
|
145
|
-
*/
|
|
146
|
-
commandDescription(cmd) {
|
|
147
|
-
return cmd.description;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Get the subcommand summary to show in the list of subcommands.
|
|
151
|
-
* (Fallback to description for backwards compatibility.)
|
|
152
|
-
*/
|
|
153
|
-
subcommandDescription(cmd) {
|
|
154
|
-
return cmd.summary || (cmd.description.includes("\n") ? cmd.description.split("\n")[0] : "");
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Get the option description to show in the list of options.
|
|
158
|
-
*/
|
|
159
|
-
optionDescription(option) {
|
|
160
|
-
const extraInfo = [];
|
|
161
|
-
if (option.choices) {
|
|
162
|
-
extraInfo.push(
|
|
163
|
-
// use stringify to match the display of the default value
|
|
164
|
-
`choices: ${option.choices.map((choice) => String(choice)).join(", ")}`
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
if (option.defaultValue !== void 0) {
|
|
168
|
-
const boolean = !option.required && !option.optional && !option.negate;
|
|
169
|
-
const showDefault = option.required || option.optional || boolean && typeof option.defaultValue === "boolean";
|
|
170
|
-
if (showDefault) {
|
|
171
|
-
extraInfo.push(`default: ${option.defaultValueDescription || String(option.defaultValue)}`);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
if (option.env !== void 0) {
|
|
175
|
-
extraInfo.push(`env: ${option.env}`);
|
|
176
|
-
}
|
|
177
|
-
if (extraInfo.length > 0) {
|
|
178
|
-
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
179
|
-
if (option.description) {
|
|
180
|
-
return `${option.description} ${extraDescription}`;
|
|
181
|
-
}
|
|
182
|
-
return extraDescription;
|
|
183
|
-
}
|
|
184
|
-
return option.description;
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Get the argument description to show in the list of arguments.
|
|
188
|
-
*/
|
|
189
|
-
argumentDescription(argument) {
|
|
190
|
-
const extraInfo = [];
|
|
191
|
-
if (argument.choices) {
|
|
192
|
-
extraInfo.push(
|
|
193
|
-
// use stringify to match the display of the default value
|
|
194
|
-
`choices: ${argument.choices.map((choice) => String(choice)).join(", ")}`
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
if (argument.defaultValue !== void 0) {
|
|
198
|
-
extraInfo.push(`default: ${argument.defaultValueDescription || String(argument.defaultValue)}`);
|
|
199
|
-
}
|
|
200
|
-
if (extraInfo.length > 0) {
|
|
201
|
-
const extraDescription = `(${extraInfo.join(", ")})`;
|
|
202
|
-
if (argument.description) {
|
|
203
|
-
return `${argument.description} ${extraDescription}`;
|
|
204
|
-
}
|
|
205
|
-
return extraDescription;
|
|
206
|
-
}
|
|
207
|
-
return argument.description;
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Format a list of items, given a heading and an array of formatted items.
|
|
211
|
-
*/
|
|
212
|
-
formatItemList(heading, items, helper) {
|
|
213
|
-
if (items.length === 0) return [];
|
|
214
|
-
return [helper.styleTitle(heading), ...items, ""];
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Group items by their help group heading.
|
|
218
|
-
*/
|
|
219
|
-
groupItems(unsortedItems, visibleItems, getGroup) {
|
|
220
|
-
const result = /* @__PURE__ */ new Map();
|
|
221
|
-
unsortedItems.forEach((item) => {
|
|
222
|
-
const group = getGroup(item);
|
|
223
|
-
if (!result.has(group)) result.set(group, []);
|
|
224
|
-
});
|
|
225
|
-
visibleItems.forEach((item) => {
|
|
226
|
-
const group = getGroup(item);
|
|
227
|
-
if (!result.has(group)) {
|
|
228
|
-
result.set(group, []);
|
|
229
|
-
}
|
|
230
|
-
result.get(group).push(item);
|
|
231
|
-
});
|
|
232
|
-
return result;
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Generate the built-in help text.
|
|
236
|
-
*/
|
|
237
|
-
formatHelp(cmd, helper) {
|
|
238
|
-
const termWidth = helper.padWidth(cmd, helper);
|
|
239
|
-
const helpWidth = helper.helpWidth;
|
|
240
|
-
function callFormatItem(term, description) {
|
|
241
|
-
return helper.formatItem(term, termWidth, description, helper);
|
|
242
|
-
}
|
|
243
|
-
__name(callFormatItem, "callFormatItem");
|
|
244
|
-
let output = [`${helper.styleTitle("Usage:")} ${helper.styleUsage(helper.commandUsage(cmd))}`, ""];
|
|
245
|
-
const commandDescription = helper.commandDescription(cmd);
|
|
246
|
-
if (commandDescription.length > 0) {
|
|
247
|
-
output = output.concat([helper.boxWrap(helper.styleCommandDescription(commandDescription), helpWidth), ""]);
|
|
248
|
-
}
|
|
249
|
-
const argumentList = helper.visibleArguments(cmd).map((argument) => {
|
|
250
|
-
return callFormatItem(
|
|
251
|
-
helper.styleArgumentTerm(helper.argumentTerm(argument)),
|
|
252
|
-
helper.styleArgumentDescription(helper.argumentDescription(argument))
|
|
253
|
-
);
|
|
254
|
-
});
|
|
255
|
-
output = output.concat(this.formatItemList("Arguments:", argumentList, helper));
|
|
256
|
-
const optionGroups = this.groupItems(
|
|
257
|
-
cmd.options,
|
|
258
|
-
helper.visibleOptions(cmd),
|
|
259
|
-
(option) => option.group ?? "Options:"
|
|
260
|
-
);
|
|
261
|
-
optionGroups.forEach((options, group) => {
|
|
262
|
-
const optionList = options.map((option) => {
|
|
263
|
-
return callFormatItem(
|
|
264
|
-
helper.styleOptionTerm(helper.optionTerm(option)),
|
|
265
|
-
helper.styleOptionDescription(helper.optionDescription(option))
|
|
266
|
-
);
|
|
267
|
-
});
|
|
268
|
-
output = output.concat(this.formatItemList(group, optionList, helper));
|
|
269
|
-
});
|
|
270
|
-
if (helper.showGlobalOptions) {
|
|
271
|
-
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
|
|
272
|
-
return callFormatItem(
|
|
273
|
-
helper.styleOptionTerm(helper.optionTerm(option)),
|
|
274
|
-
helper.styleOptionDescription(helper.optionDescription(option))
|
|
275
|
-
);
|
|
276
|
-
});
|
|
277
|
-
output = output.concat(this.formatItemList("Global Options:", globalOptionList, helper));
|
|
278
|
-
}
|
|
279
|
-
const commandGroups = this.groupItems(
|
|
280
|
-
cmd.commands,
|
|
281
|
-
helper.visibleCommands(cmd),
|
|
282
|
-
(sub) => sub.group || "Commands:"
|
|
283
|
-
);
|
|
284
|
-
commandGroups.forEach((commands, group) => {
|
|
285
|
-
const commandList = commands.map((sub) => {
|
|
286
|
-
return callFormatItem(
|
|
287
|
-
helper.styleSubcommandTerm(helper.subcommandTerm(sub)),
|
|
288
|
-
helper.styleSubcommandDescription(helper.subcommandDescription(sub))
|
|
289
|
-
);
|
|
290
|
-
});
|
|
291
|
-
output = output.concat(this.formatItemList(group, commandList, helper));
|
|
292
|
-
});
|
|
293
|
-
return output.join("\n");
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* Return display width of string, ignoring ANSI escape sequences. Used in padding and wrapping calculations.
|
|
297
|
-
*/
|
|
298
|
-
displayWidth(str) {
|
|
299
|
-
const sgrPattern = /\x1b\[\d*(;\d*)*m/g;
|
|
300
|
-
return str.replace(sgrPattern, "").length;
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Style the title for displaying in the help. Called with 'Usage:', 'Options:', etc.
|
|
304
|
-
*/
|
|
305
|
-
styleTitle(str) {
|
|
306
|
-
return str;
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Style the usage line for displaying in the help. Applies specific styling to different parts like options, commands, and arguments.
|
|
310
|
-
*/
|
|
311
|
-
styleUsage(str) {
|
|
312
|
-
return str.split(" ").map((word) => {
|
|
313
|
-
if (word === "[options]") return this.styleOptionText(word);
|
|
314
|
-
if (word === "[command]") return this.styleSubcommandText(word);
|
|
315
|
-
if (word[0] === "[" || word[0] === "<") return this.styleArgumentText(word);
|
|
316
|
-
return this.styleCommandText(word);
|
|
317
|
-
}).join(" ");
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Style command descriptions for display in help output.
|
|
321
|
-
*/
|
|
322
|
-
styleCommandDescription(str) {
|
|
323
|
-
return this.styleDescriptionText(str);
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Style option descriptions for display in help output.
|
|
327
|
-
*/
|
|
328
|
-
styleOptionDescription(str) {
|
|
329
|
-
return this.styleDescriptionText(str);
|
|
330
|
-
}
|
|
331
|
-
/**
|
|
332
|
-
* Style subcommand descriptions for display in help output.
|
|
333
|
-
*/
|
|
334
|
-
styleSubcommandDescription(str) {
|
|
335
|
-
return this.styleDescriptionText(str);
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Style argument descriptions for display in help output.
|
|
339
|
-
*/
|
|
340
|
-
styleArgumentDescription(str) {
|
|
341
|
-
return this.styleDescriptionText(str);
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Base style used by descriptions. Override in subclass to apply custom formatting.
|
|
345
|
-
*/
|
|
346
|
-
styleDescriptionText(str) {
|
|
347
|
-
return str;
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Style option terms (flags) for display in help output.
|
|
351
|
-
*/
|
|
352
|
-
styleOptionTerm(str) {
|
|
353
|
-
return this.styleOptionText(str);
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Style subcommand terms for display in help output. Applies specific styling to different parts like options and arguments.
|
|
357
|
-
*/
|
|
358
|
-
styleSubcommandTerm(str) {
|
|
359
|
-
return str.split(" ").map((word) => {
|
|
360
|
-
if (word === "[options]") return this.styleOptionText(word);
|
|
361
|
-
if (word[0] === "[" || word[0] === "<") return this.styleArgumentText(word);
|
|
362
|
-
return this.styleSubcommandText(word);
|
|
363
|
-
}).join(" ");
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Style argument terms for display in help output.
|
|
367
|
-
*/
|
|
368
|
-
styleArgumentTerm(str) {
|
|
369
|
-
return this.styleArgumentText(str);
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
* Base style used in terms and usage for options. Override in subclass to apply custom formatting.
|
|
373
|
-
*/
|
|
374
|
-
styleOptionText(str) {
|
|
375
|
-
return str;
|
|
376
|
-
}
|
|
377
|
-
/**
|
|
378
|
-
* Base style used in terms and usage for arguments. Override in subclass to apply custom formatting.
|
|
379
|
-
*/
|
|
380
|
-
styleArgumentText(str) {
|
|
381
|
-
return str;
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* Base style used in terms and usage for subcommands. Override in subclass to apply custom formatting.
|
|
385
|
-
*/
|
|
386
|
-
styleSubcommandText(str) {
|
|
387
|
-
return str;
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Base style used in terms and usage for commands. Override in subclass to apply custom formatting.
|
|
391
|
-
*/
|
|
392
|
-
styleCommandText(str) {
|
|
393
|
-
return str;
|
|
394
|
-
}
|
|
395
|
-
/**
|
|
396
|
-
* Calculate the pad width from the maximum term length.
|
|
397
|
-
*/
|
|
398
|
-
padWidth(cmd, helper) {
|
|
399
|
-
return Math.max(
|
|
400
|
-
helper.longestOptionTermLength(cmd, helper),
|
|
401
|
-
helper.longestGlobalOptionTermLength(cmd, helper),
|
|
402
|
-
helper.longestSubcommandTermLength(cmd, helper),
|
|
403
|
-
helper.longestArgumentTermLength(cmd, helper)
|
|
404
|
-
);
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Detect manually wrapped and indented strings by checking for line break followed by whitespace.
|
|
408
|
-
*/
|
|
409
|
-
preformatted(str) {
|
|
410
|
-
return /\n[^\S\r\n]/.test(str);
|
|
411
|
-
}
|
|
412
|
-
/**
|
|
413
|
-
* Format the "item", which consists of a term and description. Pad the term and wrap the description, indenting the following lines.
|
|
414
|
-
*
|
|
415
|
-
* So "TTT", 5, "DDD DDDD DD DDD" might be formatted for this.helpWidth=17 like so:
|
|
416
|
-
* TTT DDD DDDD
|
|
417
|
-
* DD DDD
|
|
418
|
-
*/
|
|
419
|
-
formatItem(term, termWidth, description, helper) {
|
|
420
|
-
const itemIndent = 2;
|
|
421
|
-
const itemIndentStr = " ".repeat(itemIndent);
|
|
422
|
-
if (!description) return itemIndentStr + term;
|
|
423
|
-
const paddedTerm = term.padEnd(termWidth + term.length - helper.displayWidth(term));
|
|
424
|
-
const spacerWidth = 2;
|
|
425
|
-
const helpWidth = this.helpWidth;
|
|
426
|
-
const remainingWidth = helpWidth - termWidth - spacerWidth - itemIndent;
|
|
427
|
-
let formattedDescription;
|
|
428
|
-
if (remainingWidth < this.minWidthToWrap || helper.preformatted(description)) {
|
|
429
|
-
formattedDescription = description;
|
|
430
|
-
} else {
|
|
431
|
-
const wrappedDescription = helper.boxWrap(description, remainingWidth);
|
|
432
|
-
formattedDescription = wrappedDescription.replace(/\n/g, "\n" + " ".repeat(termWidth + spacerWidth));
|
|
433
|
-
}
|
|
434
|
-
return itemIndentStr + paddedTerm + " ".repeat(spacerWidth) + formattedDescription.replace(/\n/g, `
|
|
435
|
-
${itemIndentStr}`);
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Wrap a string at whitespace, preserving existing line breaks.
|
|
439
|
-
* Wrapping is skipped if the width is less than `minWidthToWrap`.
|
|
440
|
-
*/
|
|
441
|
-
boxWrap(str, width) {
|
|
442
|
-
if (width < this.minWidthToWrap) return str;
|
|
443
|
-
const rawLines = str.split(/\r\n|\n/);
|
|
444
|
-
const chunkPattern = /[\s]*[^\s]+/g;
|
|
445
|
-
const wrappedLines = [];
|
|
446
|
-
rawLines.forEach((line) => {
|
|
447
|
-
const chunks = line.match(chunkPattern);
|
|
448
|
-
if (chunks === null) {
|
|
449
|
-
wrappedLines.push("");
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
let sumChunks = [chunks.shift()];
|
|
453
|
-
let sumWidth = this.displayWidth(sumChunks[0]);
|
|
454
|
-
chunks.forEach((chunk) => {
|
|
455
|
-
const visibleWidth = this.displayWidth(chunk);
|
|
456
|
-
if (sumWidth + visibleWidth <= width) {
|
|
457
|
-
sumChunks.push(chunk);
|
|
458
|
-
sumWidth += visibleWidth;
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
wrappedLines.push(sumChunks.join(""));
|
|
462
|
-
const nextChunk = chunk.trimStart();
|
|
463
|
-
sumChunks = [nextChunk];
|
|
464
|
-
sumWidth = this.displayWidth(nextChunk);
|
|
465
|
-
});
|
|
466
|
-
wrappedLines.push(sumChunks.join(""));
|
|
467
|
-
});
|
|
468
|
-
return wrappedLines.join("\n");
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
export {
|
|
472
|
-
Help
|
|
473
|
-
};
|
|
474
|
-
//# sourceMappingURL=Help.js.map
|