@optique/core 1.3.0-dev.2379 → 1.3.0-dev.2380

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.
@@ -1,6 +1,9 @@
1
+ import { Message } from "./message.js";
1
2
  import { DependencyRegistryLike } from "./registry-types.js";
2
3
  import { ValueParserResult } from "./valueparser.js";
3
4
  import { ParserDependencyMetadata } from "./dependency-metadata.js";
5
+ import { InputTrace } from "./input-trace.js";
6
+ import { ExecutionContext } from "./internal/parser.js";
4
7
 
5
8
  //#region src/dependency-runtime.d.ts
6
9
 
@@ -255,6 +258,164 @@ declare function replayDerivedParserAsync(node: RuntimeNode, rawInput: string, r
255
258
  * @since 1.0.0
256
259
  */
257
260
  declare function extractRawInputFromState(state: unknown): string | undefined;
261
+ /**
262
+ * Internal parser hook for constructs whose effectful scheduling nodes
263
+ * cannot be derived from flattened field pairs alone.
264
+ *
265
+ * `merge()` installs it so a parent's scheduling expansion uses the same
266
+ * child-indexed paths, declaration order, and duplicate-field exclusion
267
+ * as the merge's own scheduling pass; `or()`/`longestMatch()` install it
268
+ * to expose the committed branch; `command()` installs it to expose its
269
+ * inner parser once the command has matched. The returned node paths
270
+ * must match the execution paths used when the same parsers complete, so
271
+ * the run-scoped completion cache lines up.
272
+ *
273
+ * @internal
274
+ * @since 1.3.0
275
+ */
276
+ declare const effectfulSchedulingNodesKey: unique symbol;
277
+ /**
278
+ * The shape of the {@link effectfulSchedulingNodesKey} hook.
279
+ *
280
+ * @internal
281
+ * @since 1.3.0
282
+ */
283
+ type EffectfulSchedulingNodesFn = (state: unknown, parentPath: readonly PropertyKey[] | undefined) => readonly RuntimeNode[];
284
+ /**
285
+ * Forwards effectful scheduling through a shape-preserving wrapper such
286
+ * as `map()`, `optional()`, `withDefault()`, or `nonEmpty()`, so the
287
+ * wrapped parser—a selected exclusive or command branch, or an ordinary
288
+ * construct with nested effectful sources—stays visible to a parent
289
+ * construct's scheduling expansion.
290
+ *
291
+ * The installed hook simply re-exposes the inner parser as a node with
292
+ * the wrapper's own state shape unwrapped (`adaptState`, e.g. the
293
+ * `[innerState]` array used by `optional()`/`withDefault()`); the
294
+ * expansion then applies its ordinary rules to the inner parser, whether
295
+ * it carries its own hook, flattened field pairs, or source metadata.
296
+ * Wrappers are path-transparent, so the node keeps the wrapper's path.
297
+ *
298
+ * @internal
299
+ * @since 1.3.0
300
+ */
301
+ declare function defineForwardedEffectfulSchedulingNodes(wrapper: object, inner: {
302
+ readonly dependencyMetadata?: ParserDependencyMetadata;
303
+ }, adaptState?: (state: unknown) => unknown): void;
304
+ /**
305
+ * A completed effectful source result to be reused by the owning
306
+ * construct's final completion phase, keyed by the node's last path
307
+ * segment (its field key or tuple index).
308
+ *
309
+ * @internal
310
+ * @since 1.3.0
311
+ */
312
+ interface EffectfulSourceCompletion {
313
+ /** The node's field key or index (its last path segment). */
314
+ readonly key: PropertyKey;
315
+ /** The effectful completion result. */
316
+ readonly result: ValueParserResult<unknown>;
317
+ }
318
+ /**
319
+ * The result of scheduling effectful source completions.
320
+ *
321
+ * @internal
322
+ * @since 1.3.0
323
+ */
324
+ type EffectfulSourceCompletionResult = {
325
+ readonly success: true;
326
+ readonly completed: readonly EffectfulSourceCompletion[];
327
+ } | {
328
+ readonly success: false;
329
+ readonly error: Message;
330
+ };
331
+ /**
332
+ * Collects the dependency source IDs demanded by consumers among the
333
+ * given nodes and state subtree.
334
+ *
335
+ * A consumer demands its sources when it has raw input evidence: either a
336
+ * trace entry recorded at its path during parsing, or a legacy
337
+ * `DeferredParseState` embedded in the state subtree. Consumers without
338
+ * raw input never replay against real dependency values, so their sources
339
+ * are not demanded.
340
+ *
341
+ * @param nodes The direct-child runtime nodes of the owning construct.
342
+ * @param state The construct's state subtree (for legacy deferred states).
343
+ * @param trace The input trace recorded during parsing.
344
+ * @returns The set of demanded dependency source IDs.
345
+ * @internal
346
+ * @since 1.3.0
347
+ */
348
+ declare function collectDemandedDependencyIds(nodes: readonly RuntimeNode[], state: unknown, trace: InputTrace | undefined): ReadonlySet<symbol>;
349
+ /**
350
+ * Options for {@link completeEffectfulSourcesAsync}.
351
+ *
352
+ * @internal
353
+ * @since 1.3.0
354
+ */
355
+ interface CompleteEffectfulSourcesOptions {
356
+ /**
357
+ * Nodes used for demand detection instead of the scheduled nodes.
358
+ * Constructs whose parse-time trace paths differ from their scheduling
359
+ * node paths (e.g., `merge()`, which records child-indexed paths but
360
+ * schedules flattened field nodes) pass path-corrected nodes here.
361
+ */
362
+ readonly demandNodes?: readonly RuntimeNode[];
363
+ /**
364
+ * Whether a node's completion result may be cached by the owning
365
+ * construct for reuse in its final completion phase. Defaults to
366
+ * treating every node as reusable. Non-reusable completions (nodes
367
+ * expanded from nested children, or sources whose field value differs
368
+ * from the source value) rely on the run-scoped session cache to avoid
369
+ * running twice, and are skipped when no session is available.
370
+ */
371
+ readonly isReusable?: (node: RuntimeNode) => boolean;
372
+ }
373
+ /**
374
+ * Runs effectful source completions (e.g., interactive prompts) serially
375
+ * in declaration order for source nodes whose value is not yet registered.
376
+ *
377
+ * This is the scheduling half of the `completeSource` capability contract:
378
+ *
379
+ * - Runs only during real completion (`exec.phase === "complete"`); probe
380
+ * and suggest phases return immediately without effects.
381
+ * - Precedence is structural: a source whose value was registered before
382
+ * the pass begins (from CLI state, environment, configuration, or a
383
+ * default) or that has already failed is never completed effectfully.
384
+ * When several scheduled occurrences share one source, each occurrence
385
+ * still completes and re-registers, so the last occurrence wins—the
386
+ * same rule as repeated command-line source occurrences.
387
+ * - Completion results that are `undefined` or marked `deferred` are
388
+ * treated as declined and neither registered nor cached.
389
+ * - A successful result registers its value unless the value is
390
+ * `undefined`. When the node is reusable (a direct child whose field
391
+ * value is the source value), the result is also returned for reuse by
392
+ * the owning construct so the node is not completed twice; otherwise the
393
+ * effectful parser's own run-scoped session cache prevents a second
394
+ * execution, so nodes that are not reusable are only scheduled when a
395
+ * session is present. For a source behind a transform such as `map()`
396
+ * (`preservesSourceValue: false`), the `completeSource` contract still
397
+ * yields the pre-transform source value, so registration stays correct
398
+ * while the field's final value is produced separately.
399
+ * - A failed result (e.g., a cancelled prompt) marks the source as failed
400
+ * and aborts immediately—later effectful completions do not run.
401
+ *
402
+ * When the run-scoped session policy is `"demand-only"` (the phase-two
403
+ * seed pass), the demanded source IDs from
404
+ * {@link collectDemandedDependencyIds} are added to the session before any
405
+ * completion runs, letting effectful parsers defer when no phase-one
406
+ * consumer demands their value.
407
+ *
408
+ * @param nodes The runtime nodes to schedule, in declaration order.
409
+ * @param state The construct's state subtree (for demand detection).
410
+ * @param runtime The dependency runtime context.
411
+ * @param exec The execution context of the owning construct.
412
+ * @param options Scheduling options.
413
+ * @returns The scheduling result: completed nodes for reuse, or the first
414
+ * failure.
415
+ * @internal
416
+ * @since 1.3.0
417
+ */
418
+ declare function completeEffectfulSourcesAsync(nodes: readonly RuntimeNode[], state: unknown, runtime: DependencyRuntimeContext, exec: ExecutionContext | undefined, options?: CompleteEffectfulSourcesOptions): Promise<EffectfulSourceCompletionResult>;
258
419
  /**
259
420
  * Recursively collects dependency source values from {@link DependencySourceState}
260
421
  * objects found in the state tree and registers them in the runtime.
@@ -334,4 +495,4 @@ declare function buildRuntimeNodesFromArray(parsers: ReadonlyArray<{
334
495
  readonly initialState?: unknown;
335
496
  }>, stateArray: readonly unknown[], parentPath?: readonly PropertyKey[]): readonly RuntimeNode[];
336
497
  //#endregion
337
- export { DependencyRequest, DependencyResolution, DependencyRuntimeContext, ReplayKey, RuntimeNode, SourceDefaultFailure, buildRuntimeNodesFromArray, buildRuntimeNodesFromPairs, collectExplicitSourceValues, collectExplicitSourceValuesAsync, collectSourcesFromState, createDependencyFingerprint, createDependencyRuntimeContext, createReplayKey, extractRawInputFromState, fillMissingSourceDefaults, fillMissingSourceDefaultsAsync, replayDerivedParser, replayDerivedParserAsync, resolveStateWithRuntime, resolveStateWithRuntimeAsync };
498
+ export { CompleteEffectfulSourcesOptions, DependencyRequest, DependencyResolution, DependencyRuntimeContext, EffectfulSchedulingNodesFn, EffectfulSourceCompletion, EffectfulSourceCompletionResult, ReplayKey, RuntimeNode, SourceDefaultFailure, buildRuntimeNodesFromArray, buildRuntimeNodesFromPairs, collectDemandedDependencyIds, collectExplicitSourceValues, collectExplicitSourceValuesAsync, collectSourcesFromState, completeEffectfulSourcesAsync, createDependencyFingerprint, createDependencyRuntimeContext, createReplayKey, defineForwardedEffectfulSchedulingNodes, effectfulSchedulingNodesKey, extractRawInputFromState, fillMissingSourceDefaults, fillMissingSourceDefaultsAsync, replayDerivedParser, replayDerivedParserAsync, resolveStateWithRuntime, resolveStateWithRuntimeAsync };
@@ -478,6 +478,218 @@ function extractRawInputFromState(state) {
478
478
  return void 0;
479
479
  }
480
480
  /**
481
+ * Internal parser hook for constructs whose effectful scheduling nodes
482
+ * cannot be derived from flattened field pairs alone.
483
+ *
484
+ * `merge()` installs it so a parent's scheduling expansion uses the same
485
+ * child-indexed paths, declaration order, and duplicate-field exclusion
486
+ * as the merge's own scheduling pass; `or()`/`longestMatch()` install it
487
+ * to expose the committed branch; `command()` installs it to expose its
488
+ * inner parser once the command has matched. The returned node paths
489
+ * must match the execution paths used when the same parsers complete, so
490
+ * the run-scoped completion cache lines up.
491
+ *
492
+ * @internal
493
+ * @since 1.3.0
494
+ */
495
+ const effectfulSchedulingNodesKey = Symbol("@optique/core/dependency-runtime/effectfulSchedulingNodes");
496
+ /**
497
+ * Forwards effectful scheduling through a shape-preserving wrapper such
498
+ * as `map()`, `optional()`, `withDefault()`, or `nonEmpty()`, so the
499
+ * wrapped parser—a selected exclusive or command branch, or an ordinary
500
+ * construct with nested effectful sources—stays visible to a parent
501
+ * construct's scheduling expansion.
502
+ *
503
+ * The installed hook simply re-exposes the inner parser as a node with
504
+ * the wrapper's own state shape unwrapped (`adaptState`, e.g. the
505
+ * `[innerState]` array used by `optional()`/`withDefault()`); the
506
+ * expansion then applies its ordinary rules to the inner parser, whether
507
+ * it carries its own hook, flattened field pairs, or source metadata.
508
+ * Wrappers are path-transparent, so the node keeps the wrapper's path.
509
+ *
510
+ * @internal
511
+ * @since 1.3.0
512
+ */
513
+ function defineForwardedEffectfulSchedulingNodes(wrapper, inner, adaptState) {
514
+ if (inner.dependencyMetadata?.source != null) return;
515
+ Object.defineProperty(wrapper, effectfulSchedulingNodesKey, {
516
+ value: ((state, parentPath) => [{
517
+ path: parentPath ?? [],
518
+ parser: inner,
519
+ state: adaptState == null ? state : adaptState(state)
520
+ }]),
521
+ configurable: true,
522
+ enumerable: false
523
+ });
524
+ }
525
+ /**
526
+ * Collects the dependency source IDs demanded by consumers among the
527
+ * given nodes and state subtree.
528
+ *
529
+ * A consumer demands its sources when it has raw input evidence: either a
530
+ * trace entry recorded at its path during parsing, or a legacy
531
+ * `DeferredParseState` embedded in the state subtree. Consumers without
532
+ * raw input never replay against real dependency values, so their sources
533
+ * are not demanded.
534
+ *
535
+ * @param nodes The direct-child runtime nodes of the owning construct.
536
+ * @param state The construct's state subtree (for legacy deferred states).
537
+ * @param trace The input trace recorded during parsing.
538
+ * @returns The set of demanded dependency source IDs.
539
+ * @internal
540
+ * @since 1.3.0
541
+ */
542
+ function collectDemandedDependencyIds(nodes, state, trace) {
543
+ const demanded = /* @__PURE__ */ new Set();
544
+ for (const node of nodes) {
545
+ const derived = node.parser.dependencyMetadata?.derived;
546
+ if (derived == null) continue;
547
+ const hasRawInput = trace?.get(node.path)?.rawInput != null || extractRawInputFromState(node.state) != null;
548
+ if (!hasRawInput) continue;
549
+ for (const id of derived.dependencyIds) demanded.add(id);
550
+ }
551
+ collectDeferredDemand(state, demanded, /* @__PURE__ */ new WeakSet());
552
+ return demanded;
553
+ }
554
+ function collectDeferredDemand(state, demanded, visited) {
555
+ if (state == null || typeof state !== "object") return;
556
+ if (visited.has(state)) return;
557
+ visited.add(state);
558
+ if (isDeferredParseState(state)) {
559
+ const ids = state.dependencyIds != null && state.dependencyIds.length > 0 ? state.dependencyIds : [state.dependencyId];
560
+ for (const id of ids) demanded.add(id);
561
+ return;
562
+ }
563
+ for (const key of Reflect.ownKeys(state)) collectDeferredDemand(state[key], demanded, visited);
564
+ }
565
+ /**
566
+ * Runs effectful source completions (e.g., interactive prompts) serially
567
+ * in declaration order for source nodes whose value is not yet registered.
568
+ *
569
+ * This is the scheduling half of the `completeSource` capability contract:
570
+ *
571
+ * - Runs only during real completion (`exec.phase === "complete"`); probe
572
+ * and suggest phases return immediately without effects.
573
+ * - Precedence is structural: a source whose value was registered before
574
+ * the pass begins (from CLI state, environment, configuration, or a
575
+ * default) or that has already failed is never completed effectfully.
576
+ * When several scheduled occurrences share one source, each occurrence
577
+ * still completes and re-registers, so the last occurrence wins—the
578
+ * same rule as repeated command-line source occurrences.
579
+ * - Completion results that are `undefined` or marked `deferred` are
580
+ * treated as declined and neither registered nor cached.
581
+ * - A successful result registers its value unless the value is
582
+ * `undefined`. When the node is reusable (a direct child whose field
583
+ * value is the source value), the result is also returned for reuse by
584
+ * the owning construct so the node is not completed twice; otherwise the
585
+ * effectful parser's own run-scoped session cache prevents a second
586
+ * execution, so nodes that are not reusable are only scheduled when a
587
+ * session is present. For a source behind a transform such as `map()`
588
+ * (`preservesSourceValue: false`), the `completeSource` contract still
589
+ * yields the pre-transform source value, so registration stays correct
590
+ * while the field's final value is produced separately.
591
+ * - A failed result (e.g., a cancelled prompt) marks the source as failed
592
+ * and aborts immediately—later effectful completions do not run.
593
+ *
594
+ * When the run-scoped session policy is `"demand-only"` (the phase-two
595
+ * seed pass), the demanded source IDs from
596
+ * {@link collectDemandedDependencyIds} are added to the session before any
597
+ * completion runs, letting effectful parsers defer when no phase-one
598
+ * consumer demands their value.
599
+ *
600
+ * @param nodes The runtime nodes to schedule, in declaration order.
601
+ * @param state The construct's state subtree (for demand detection).
602
+ * @param runtime The dependency runtime context.
603
+ * @param exec The execution context of the owning construct.
604
+ * @param options Scheduling options.
605
+ * @returns The scheduling result: completed nodes for reuse, or the first
606
+ * failure.
607
+ * @internal
608
+ * @since 1.3.0
609
+ */
610
+ async function completeEffectfulSourcesAsync(nodes, state, runtime, exec, options) {
611
+ const empty = {
612
+ success: true,
613
+ completed: []
614
+ };
615
+ if (exec == null || exec.phase !== "complete") return empty;
616
+ const session = exec.effectfulCompletionSession;
617
+ if (session?.policy === "demand-only") {
618
+ const demandNodes = options?.demandNodes ?? nodes;
619
+ const demanded = collectDemandedDependencyIds(demandNodes, state, exec.trace);
620
+ for (const id of demanded) session.demanded.add(id);
621
+ }
622
+ if (session != null) {
623
+ for (const result of session.results.values()) if (!result.success) return {
624
+ success: false,
625
+ error: result.error
626
+ };
627
+ }
628
+ const schedulable = nodes.filter((node) => node.parser.dependencyMetadata?.source?.completeSource != null);
629
+ if (schedulable.length === 0) return empty;
630
+ const preexisting = /* @__PURE__ */ new Set();
631
+ for (const node of schedulable) {
632
+ const source = node.parser.dependencyMetadata?.source;
633
+ if (source != null && runtime.hasSource(source.sourceId) && session?.effectfulSources.has(source.sourceId) !== true) preexisting.add(source.sourceId);
634
+ }
635
+ const completed = [];
636
+ for (const node of schedulable) {
637
+ const source = node.parser.dependencyMetadata?.source;
638
+ if (source?.completeSource == null) continue;
639
+ const pathKey = serializeSchedulingPath(node.path);
640
+ const priorResult = session?.completedByPath.get(pathKey);
641
+ if (priorResult != null) {
642
+ if (source.preservesSourceValue && (options?.isReusable?.(node) ?? true)) completed.push({
643
+ key: node.path[node.path.length - 1],
644
+ result: priorResult
645
+ });
646
+ continue;
647
+ }
648
+ if (preexisting.has(source.sourceId)) continue;
649
+ const reusable = source.preservesSourceValue && (options?.isReusable?.(node) ?? true);
650
+ if (!reusable && session == null) continue;
651
+ const childExec = {
652
+ ...exec,
653
+ path: node.path
654
+ };
655
+ const result = await source.completeSource(node.state, childExec);
656
+ if (result == null) continue;
657
+ if (!result.success) {
658
+ runtime.markSourceFailed(source.sourceId);
659
+ return {
660
+ success: false,
661
+ error: result.error
662
+ };
663
+ }
664
+ if (result.deferred === true) continue;
665
+ session?.completedByPath.set(pathKey, result);
666
+ if (reusable) completed.push({
667
+ key: node.path[node.path.length - 1],
668
+ result
669
+ });
670
+ if (result.value !== void 0) runtime.registerSource(source.sourceId, result.value);
671
+ }
672
+ return {
673
+ success: true,
674
+ completed
675
+ };
676
+ }
677
+ /**
678
+ * Serializes a scheduling node path into a stable string key, using
679
+ * length-prefixed segments so no separator escaping is needed.
680
+ */
681
+ function serializeSchedulingPath(path) {
682
+ return path.map((segment) => {
683
+ if (typeof segment === "symbol") {
684
+ const key = stableSymbolKey(segment);
685
+ return `y${key.length}:${key}`;
686
+ }
687
+ const tag = typeof segment === "number" ? "n" : "s";
688
+ const text = String(segment);
689
+ return `${tag}${text.length}:${text}`;
690
+ }).join("");
691
+ }
692
+ /**
481
693
  * Checks if a value is a plain object (not a class instance) for the
482
694
  * purpose of recursive state traversal.
483
695
  */
@@ -717,4 +929,4 @@ function buildRuntimeNodesFromArray(parsers, stateArray, parentPath) {
717
929
  }
718
930
 
719
931
  //#endregion
720
- export { buildRuntimeNodesFromArray, buildRuntimeNodesFromPairs, collectExplicitSourceValues, collectExplicitSourceValuesAsync, collectSourcesFromState, createDependencyFingerprint, createDependencyRuntimeContext, createReplayKey, extractRawInputFromState, fillMissingSourceDefaults, fillMissingSourceDefaultsAsync, replayDerivedParser, replayDerivedParserAsync, resolveStateWithRuntime, resolveStateWithRuntimeAsync };
932
+ export { buildRuntimeNodesFromArray, buildRuntimeNodesFromPairs, collectDemandedDependencyIds, collectExplicitSourceValues, collectExplicitSourceValuesAsync, collectSourcesFromState, completeEffectfulSourcesAsync, createDependencyFingerprint, createDependencyRuntimeContext, createReplayKey, defineForwardedEffectfulSchedulingNodes, effectfulSchedulingNodesKey, extractRawInputFromState, fillMissingSourceDefaults, fillMissingSourceDefaultsAsync, replayDerivedParser, replayDerivedParserAsync, resolveStateWithRuntime, resolveStateWithRuntimeAsync };
package/dist/doc.cjs CHANGED
@@ -213,6 +213,7 @@ function defaultSectionOrder(a, b) {
213
213
  function formatDocPage(programName, page, options = {}) {
214
214
  require_validate.validateProgramName(programName);
215
215
  const termIndent = options.termIndent ?? 2;
216
+ const termWidth = options.termWidth ?? 26;
216
217
  const showUsage = options.showUsage ?? true;
217
218
  if (options.maxWidth != null && (!Number.isFinite(options.maxWidth) || !Number.isInteger(options.maxWidth))) throw new TypeError(`maxWidth must be a finite integer, got ${options.maxWidth}.`);
218
219
  const filteredSections = page.sections.map((s) => ({
@@ -231,32 +232,9 @@ function formatDocPage(programName, page, options = {}) {
231
232
  if (maxItems < 1) throw new RangeError(`showChoices.maxItems must be at least 1, but got ${maxItems}.`);
232
233
  }
233
234
  const hasContent = (msg) => Array.isArray(msg) && msg.length > 0;
234
- const needsDescriptionColumn = (entry) => hasContent(entry.description) || (options.showDefault === true || typeof options.showDefault === "object") && hasContent(entry.default) || (options.showChoices === true || typeof options.showChoices === "object") && hasContent(entry.choices);
235
- const automaticTermWidth = () => {
236
- let widest;
237
- for (const section of page.sections) for (const entry of section.entries) {
238
- if (!needsDescriptionColumn(entry)) continue;
239
- const rendered = require_usage.formatUsageTerm(entry.term, {
240
- colors: options.colors,
241
- optionsSeparator: ", ",
242
- context: "doc"
243
- });
244
- const width = Math.max(...rendered.split("\n").map((line) => require_displaywidth.getDisplayWidth(line)));
245
- widest = widest == null ? width : Math.max(widest, width);
246
- }
247
- return widest;
248
- };
249
- const termWidth = options.termWidth === "auto" ? automaticTermWidth() ?? 26 : options.termWidth ?? 26;
250
- const hasEntries = page.sections.some((s) => s.entries.length > 0);
251
- const needsDescColumn = hasEntries && page.sections.some((s) => s.entries.some(needsDescriptionColumn));
252
- let effectiveTermWidth;
253
- if (options.maxWidth == null) effectiveTermWidth = termWidth;
254
- else {
255
- const availableForColumns = options.maxWidth - termIndent - 2;
256
- const evenlySplitTermWidth = Math.max(1, Math.floor(availableForColumns / 2));
257
- effectiveTermWidth = options.termWidth === "auto" && needsDescColumn ? Math.min(termWidth, evenlySplitTermWidth) : availableForColumns >= termWidth + 1 ? termWidth : evenlySplitTermWidth;
258
- }
259
235
  if (options.maxWidth != null) {
236
+ const hasEntries = page.sections.some((s) => s.entries.length > 0);
237
+ const needsDescColumn = hasEntries && page.sections.some((s) => s.entries.some((e) => hasContent(e.description) || options.showDefault && hasContent(e.default) || options.showChoices && hasContent(e.choices)));
260
238
  let minDescWidth = 1;
261
239
  if (needsDescColumn) {
262
240
  if (options.showDefault && page.sections.some((s) => s.entries.some((e) => hasContent(e.default)))) {
@@ -282,13 +260,20 @@ function formatDocPage(programName, page, options = {}) {
282
260
  if (options.maxWidth < minWidth) throw new RangeError(`maxWidth must be at least ${minWidth}, got ${options.maxWidth}.`);
283
261
  if (needsDescColumn && minDescWidth > 1) {
284
262
  const avail = options.maxWidth - termIndent - 2;
285
- const descW = avail - effectiveTermWidth;
263
+ const effTW = avail >= termWidth + 1 ? termWidth : Math.max(1, Math.floor(avail / 2));
264
+ const descW = avail - effTW;
286
265
  if (descW < minDescWidth) {
287
- const needed = termIndent + effectiveTermWidth + 2 + minDescWidth;
266
+ const needed = termIndent + termWidth + 2 + minDescWidth;
288
267
  throw new RangeError(`maxWidth must be at least ${needed}, got ${options.maxWidth}.`);
289
268
  }
290
269
  }
291
270
  }
271
+ let effectiveTermWidth;
272
+ if (options.maxWidth == null) effectiveTermWidth = termWidth;
273
+ else {
274
+ const availableForColumns = options.maxWidth - termIndent - 2;
275
+ effectiveTermWidth = availableForColumns >= termWidth + 1 ? termWidth : Math.max(1, Math.floor(availableForColumns / 2));
276
+ }
292
277
  let output = "";
293
278
  if (hasContent(page.brief)) {
294
279
  output += require_message.formatMessage(page.brief, {
package/dist/doc.d.cts CHANGED
@@ -217,14 +217,10 @@ interface DocPageFormatOptions {
217
217
  */
218
218
  termIndent?: number;
219
219
  /**
220
- * Width allocated for terms before descriptions start. Set to `"auto"`
221
- * to align descriptions after the widest visible term that has content.
222
- * Terminal display width is used for automatic measurement.
223
- *
220
+ * Width allocated for terms before descriptions start.
224
221
  * @default `26`
225
- * @since 1.3.0 Added automatic term width.
226
222
  */
227
- termWidth?: number | "auto";
223
+ termWidth?: number;
228
224
  /**
229
225
  * Maximum width of the entire formatted output.
230
226
  */
package/dist/doc.d.ts CHANGED
@@ -217,14 +217,10 @@ interface DocPageFormatOptions {
217
217
  */
218
218
  termIndent?: number;
219
219
  /**
220
- * Width allocated for terms before descriptions start. Set to `"auto"`
221
- * to align descriptions after the widest visible term that has content.
222
- * Terminal display width is used for automatic measurement.
223
- *
220
+ * Width allocated for terms before descriptions start.
224
221
  * @default `26`
225
- * @since 1.3.0 Added automatic term width.
226
222
  */
227
- termWidth?: number | "auto";
223
+ termWidth?: number;
228
224
  /**
229
225
  * Maximum width of the entire formatted output.
230
226
  */
package/dist/doc.js CHANGED
@@ -213,6 +213,7 @@ function defaultSectionOrder(a, b) {
213
213
  function formatDocPage(programName, page, options = {}) {
214
214
  validateProgramName(programName);
215
215
  const termIndent = options.termIndent ?? 2;
216
+ const termWidth = options.termWidth ?? 26;
216
217
  const showUsage = options.showUsage ?? true;
217
218
  if (options.maxWidth != null && (!Number.isFinite(options.maxWidth) || !Number.isInteger(options.maxWidth))) throw new TypeError(`maxWidth must be a finite integer, got ${options.maxWidth}.`);
218
219
  const filteredSections = page.sections.map((s) => ({
@@ -231,32 +232,9 @@ function formatDocPage(programName, page, options = {}) {
231
232
  if (maxItems < 1) throw new RangeError(`showChoices.maxItems must be at least 1, but got ${maxItems}.`);
232
233
  }
233
234
  const hasContent = (msg) => Array.isArray(msg) && msg.length > 0;
234
- const needsDescriptionColumn = (entry) => hasContent(entry.description) || (options.showDefault === true || typeof options.showDefault === "object") && hasContent(entry.default) || (options.showChoices === true || typeof options.showChoices === "object") && hasContent(entry.choices);
235
- const automaticTermWidth = () => {
236
- let widest;
237
- for (const section of page.sections) for (const entry of section.entries) {
238
- if (!needsDescriptionColumn(entry)) continue;
239
- const rendered = formatUsageTerm(entry.term, {
240
- colors: options.colors,
241
- optionsSeparator: ", ",
242
- context: "doc"
243
- });
244
- const width = Math.max(...rendered.split("\n").map((line) => getDisplayWidth(line)));
245
- widest = widest == null ? width : Math.max(widest, width);
246
- }
247
- return widest;
248
- };
249
- const termWidth = options.termWidth === "auto" ? automaticTermWidth() ?? 26 : options.termWidth ?? 26;
250
- const hasEntries = page.sections.some((s) => s.entries.length > 0);
251
- const needsDescColumn = hasEntries && page.sections.some((s) => s.entries.some(needsDescriptionColumn));
252
- let effectiveTermWidth;
253
- if (options.maxWidth == null) effectiveTermWidth = termWidth;
254
- else {
255
- const availableForColumns = options.maxWidth - termIndent - 2;
256
- const evenlySplitTermWidth = Math.max(1, Math.floor(availableForColumns / 2));
257
- effectiveTermWidth = options.termWidth === "auto" && needsDescColumn ? Math.min(termWidth, evenlySplitTermWidth) : availableForColumns >= termWidth + 1 ? termWidth : evenlySplitTermWidth;
258
- }
259
235
  if (options.maxWidth != null) {
236
+ const hasEntries = page.sections.some((s) => s.entries.length > 0);
237
+ const needsDescColumn = hasEntries && page.sections.some((s) => s.entries.some((e) => hasContent(e.description) || options.showDefault && hasContent(e.default) || options.showChoices && hasContent(e.choices)));
260
238
  let minDescWidth = 1;
261
239
  if (needsDescColumn) {
262
240
  if (options.showDefault && page.sections.some((s) => s.entries.some((e) => hasContent(e.default)))) {
@@ -282,13 +260,20 @@ function formatDocPage(programName, page, options = {}) {
282
260
  if (options.maxWidth < minWidth) throw new RangeError(`maxWidth must be at least ${minWidth}, got ${options.maxWidth}.`);
283
261
  if (needsDescColumn && minDescWidth > 1) {
284
262
  const avail = options.maxWidth - termIndent - 2;
285
- const descW = avail - effectiveTermWidth;
263
+ const effTW = avail >= termWidth + 1 ? termWidth : Math.max(1, Math.floor(avail / 2));
264
+ const descW = avail - effTW;
286
265
  if (descW < minDescWidth) {
287
- const needed = termIndent + effectiveTermWidth + 2 + minDescWidth;
266
+ const needed = termIndent + termWidth + 2 + minDescWidth;
288
267
  throw new RangeError(`maxWidth must be at least ${needed}, got ${options.maxWidth}.`);
289
268
  }
290
269
  }
291
270
  }
271
+ let effectiveTermWidth;
272
+ if (options.maxWidth == null) effectiveTermWidth = termWidth;
273
+ else {
274
+ const availableForColumns = options.maxWidth - termIndent - 2;
275
+ effectiveTermWidth = availableForColumns >= termWidth + 1 ? termWidth : Math.max(1, Math.floor(availableForColumns / 2));
276
+ }
292
277
  let output = "";
293
278
  if (hasContent(page.brief)) {
294
279
  output += formatMessage(page.brief, {