@optique/core 1.0.0-dev.1470 → 1.0.0-dev.1484

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/doc.cjs CHANGED
@@ -248,7 +248,7 @@ function formatDocPage(programName, page, options = {}) {
248
248
  const splitEntryMin = termIndent + 2 + Math.max(2, 2 * minDescWidth - 1);
249
249
  const fixedEntryMin = termIndent + 2 + termWidth + minDescWidth;
250
250
  const entryMin = needsDescColumn ? Math.min(splitEntryMin, fixedEntryMin) : hasEntries ? termIndent + 1 : 1;
251
- const usageMin = page.usage != null ? 8 + programName.length : 1;
251
+ const usageMin = page.usage != null ? 7 + Math.max(programName.length, Math.min(maxVisibleAtomicWidth(page.usage), programName.length + 7)) : 1;
252
252
  let sectionMin = 1;
253
253
  if (page.examples != null) sectionMin = Math.max(sectionMin, 9);
254
254
  if (page.author != null) sectionMin = Math.max(sectionMin, 7);
@@ -451,6 +451,54 @@ function formatDocPage(programName, page, options = {}) {
451
451
  function indentLines(text$1, indent) {
452
452
  return text$1.split("\n").join("\n" + " ".repeat(indent));
453
453
  }
454
+ /**
455
+ * Returns the width of the widest non-breakable segment among visible
456
+ * (non-usage-hidden) terms in a usage tree. Hidden terms are excluded
457
+ * because they are filtered out before rendering, so they do not
458
+ * contribute to the rendered width.
459
+ */
460
+ function maxVisibleAtomicWidth(usage) {
461
+ let max = 0;
462
+ for (const term of usage) switch (term.type) {
463
+ case "argument":
464
+ if (!require_usage.isUsageHidden(term.hidden)) max = Math.max(max, term.metavar.length);
465
+ break;
466
+ case "option":
467
+ if (!require_usage.isUsageHidden(term.hidden) && term.names.length > 0) {
468
+ for (const name of term.names) max = Math.max(max, name.length);
469
+ if (term.metavar != null) max = Math.max(max, term.metavar.length);
470
+ }
471
+ break;
472
+ case "command":
473
+ if (!require_usage.isUsageHidden(term.hidden)) max = Math.max(max, term.name.length);
474
+ break;
475
+ case "passthrough":
476
+ if (!require_usage.isUsageHidden(term.hidden)) max = Math.max(max, 5);
477
+ break;
478
+ case "optional":
479
+ max = Math.max(max, maxVisibleAtomicWidth(term.terms));
480
+ break;
481
+ case "multiple": {
482
+ const innerMax = maxVisibleAtomicWidth(term.terms);
483
+ if (innerMax > 0) max = Math.max(max, 3, innerMax);
484
+ break;
485
+ }
486
+ case "exclusive":
487
+ for (const branch of term.terms) {
488
+ const first = branch[0];
489
+ if (first?.type === "command" && require_usage.isUsageHidden(first.hidden)) continue;
490
+ max = Math.max(max, maxVisibleAtomicWidth(branch));
491
+ }
492
+ break;
493
+ case "literal":
494
+ if (term.value !== "") max = Math.max(max, term.value.length);
495
+ break;
496
+ case "ellipsis":
497
+ max = Math.max(max, 3);
498
+ break;
499
+ }
500
+ return max;
501
+ }
454
502
  const ansiEscapeCodeRegex = /\x1B\[[0-9;]*[a-zA-Z]/g;
455
503
  function ansiAwareRightPad(text$1, length, char = " ") {
456
504
  const strippedText = text$1.replace(ansiEscapeCodeRegex, "");
package/dist/doc.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { cloneMessage, formatMessage, text } from "./message.js";
2
2
  import { validateProgramName } from "./validate.js";
3
- import { cloneUsageTerm, formatUsage, formatUsageTerm, isDocHidden } from "./usage.js";
3
+ import { cloneUsageTerm, formatUsage, formatUsageTerm, isDocHidden, isUsageHidden } from "./usage.js";
4
4
 
5
5
  //#region src/doc.ts
6
6
  /**
@@ -248,7 +248,7 @@ function formatDocPage(programName, page, options = {}) {
248
248
  const splitEntryMin = termIndent + 2 + Math.max(2, 2 * minDescWidth - 1);
249
249
  const fixedEntryMin = termIndent + 2 + termWidth + minDescWidth;
250
250
  const entryMin = needsDescColumn ? Math.min(splitEntryMin, fixedEntryMin) : hasEntries ? termIndent + 1 : 1;
251
- const usageMin = page.usage != null ? 8 + programName.length : 1;
251
+ const usageMin = page.usage != null ? 7 + Math.max(programName.length, Math.min(maxVisibleAtomicWidth(page.usage), programName.length + 7)) : 1;
252
252
  let sectionMin = 1;
253
253
  if (page.examples != null) sectionMin = Math.max(sectionMin, 9);
254
254
  if (page.author != null) sectionMin = Math.max(sectionMin, 7);
@@ -451,6 +451,54 @@ function formatDocPage(programName, page, options = {}) {
451
451
  function indentLines(text$1, indent) {
452
452
  return text$1.split("\n").join("\n" + " ".repeat(indent));
453
453
  }
454
+ /**
455
+ * Returns the width of the widest non-breakable segment among visible
456
+ * (non-usage-hidden) terms in a usage tree. Hidden terms are excluded
457
+ * because they are filtered out before rendering, so they do not
458
+ * contribute to the rendered width.
459
+ */
460
+ function maxVisibleAtomicWidth(usage) {
461
+ let max = 0;
462
+ for (const term of usage) switch (term.type) {
463
+ case "argument":
464
+ if (!isUsageHidden(term.hidden)) max = Math.max(max, term.metavar.length);
465
+ break;
466
+ case "option":
467
+ if (!isUsageHidden(term.hidden) && term.names.length > 0) {
468
+ for (const name of term.names) max = Math.max(max, name.length);
469
+ if (term.metavar != null) max = Math.max(max, term.metavar.length);
470
+ }
471
+ break;
472
+ case "command":
473
+ if (!isUsageHidden(term.hidden)) max = Math.max(max, term.name.length);
474
+ break;
475
+ case "passthrough":
476
+ if (!isUsageHidden(term.hidden)) max = Math.max(max, 5);
477
+ break;
478
+ case "optional":
479
+ max = Math.max(max, maxVisibleAtomicWidth(term.terms));
480
+ break;
481
+ case "multiple": {
482
+ const innerMax = maxVisibleAtomicWidth(term.terms);
483
+ if (innerMax > 0) max = Math.max(max, 3, innerMax);
484
+ break;
485
+ }
486
+ case "exclusive":
487
+ for (const branch of term.terms) {
488
+ const first = branch[0];
489
+ if (first?.type === "command" && isUsageHidden(first.hidden)) continue;
490
+ max = Math.max(max, maxVisibleAtomicWidth(branch));
491
+ }
492
+ break;
493
+ case "literal":
494
+ if (term.value !== "") max = Math.max(max, term.value.length);
495
+ break;
496
+ case "ellipsis":
497
+ max = Math.max(max, 3);
498
+ break;
499
+ }
500
+ return max;
501
+ }
454
502
  const ansiEscapeCodeRegex = /\x1B\[[0-9;]*[a-zA-Z]/g;
455
503
  function ansiAwareRightPad(text$1, length, char = " ") {
456
504
  const strippedText = text$1.replace(ansiEscapeCodeRegex, "");
package/dist/usage.cjs CHANGED
@@ -164,13 +164,24 @@ function formatUsage(programName, usage, options = {}) {
164
164
  if (usage.length > 1) command = [...usage.slice(0, -1), ...command];
165
165
  lines.push(formatUsage(programName, command, options));
166
166
  }
167
- return lines.join("\n");
167
+ if (lines.length > 0) return lines.join("\n");
168
168
  }
169
169
  }
170
- let output = options.colors ? `\x1b[1m${programName}\x1b[0m ` : `${programName} `;
171
- let lineWidth = programName.length + 1;
170
+ let output = options.colors ? `\x1b[1m${programName}\x1b[0m` : programName;
171
+ let lineWidth = programName.length;
172
+ let first = true;
172
173
  for (const { text, width } of formatUsageTerms(usage, options)) {
173
- if (options.maxWidth != null && lineWidth + width > options.maxWidth) {
174
+ if (first) {
175
+ first = false;
176
+ if (options.maxWidth != null && lineWidth + 1 + width > options.maxWidth) {
177
+ output += "\n";
178
+ lineWidth = 0;
179
+ } else {
180
+ output += " ";
181
+ lineWidth += 1;
182
+ }
183
+ } else if (options.maxWidth != null && lineWidth + width > options.maxWidth) {
184
+ if (output.endsWith(" ")) output = output.slice(0, -1);
174
185
  output += "\n";
175
186
  lineWidth = 0;
176
187
  if (text === " ") continue;
@@ -408,6 +419,7 @@ function formatUsageTerm(term, options = {}) {
408
419
  let output = "";
409
420
  for (const { text, width } of formatUsageTermInternal(visibleTerms[0], options)) {
410
421
  if (options.maxWidth != null && lineWidth + width > options.maxWidth) {
422
+ if (output.endsWith(" ")) output = output.slice(0, -1);
411
423
  output += "\n";
412
424
  lineWidth = 0;
413
425
  if (text === " ") continue;
package/dist/usage.js CHANGED
@@ -164,13 +164,24 @@ function formatUsage(programName, usage, options = {}) {
164
164
  if (usage.length > 1) command = [...usage.slice(0, -1), ...command];
165
165
  lines.push(formatUsage(programName, command, options));
166
166
  }
167
- return lines.join("\n");
167
+ if (lines.length > 0) return lines.join("\n");
168
168
  }
169
169
  }
170
- let output = options.colors ? `\x1b[1m${programName}\x1b[0m ` : `${programName} `;
171
- let lineWidth = programName.length + 1;
170
+ let output = options.colors ? `\x1b[1m${programName}\x1b[0m` : programName;
171
+ let lineWidth = programName.length;
172
+ let first = true;
172
173
  for (const { text, width } of formatUsageTerms(usage, options)) {
173
- if (options.maxWidth != null && lineWidth + width > options.maxWidth) {
174
+ if (first) {
175
+ first = false;
176
+ if (options.maxWidth != null && lineWidth + 1 + width > options.maxWidth) {
177
+ output += "\n";
178
+ lineWidth = 0;
179
+ } else {
180
+ output += " ";
181
+ lineWidth += 1;
182
+ }
183
+ } else if (options.maxWidth != null && lineWidth + width > options.maxWidth) {
184
+ if (output.endsWith(" ")) output = output.slice(0, -1);
174
185
  output += "\n";
175
186
  lineWidth = 0;
176
187
  if (text === " ") continue;
@@ -408,6 +419,7 @@ function formatUsageTerm(term, options = {}) {
408
419
  let output = "";
409
420
  for (const { text, width } of formatUsageTermInternal(visibleTerms[0], options)) {
410
421
  if (options.maxWidth != null && lineWidth + width > options.maxWidth) {
422
+ if (output.endsWith(" ")) output = output.slice(0, -1);
411
423
  output += "\n";
412
424
  lineWidth = 0;
413
425
  if (text === " ") continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1470+e7499369",
3
+ "version": "1.0.0-dev.1484+220c0bfd",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",