@tidal-ts/terminal 0.11.0 → 0.12.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/dist/context.d.ts CHANGED
@@ -51,6 +51,9 @@ export declare function useSetStudyParam(): (id: string, name: string, value: nu
51
51
  /** Switch which output of a multi-output source a study reads (the output
52
52
  * picker). Pair it with `outputAllowed` to render refused choices disabled. */
53
53
  export declare function useSetStudyOutput(): (id: string, output: string) => void;
54
+ /** Switch which column of its source a study reads (`series.setStudyInput`) —
55
+ * the data node's output picker. Pair it with `inputAllowed`. */
56
+ export declare function useSetStudyInput(): (id: string, column: string) => void;
54
57
  /** Which outputs of its multi-output source a study runs on — one, several, or
55
58
  * all (`series.setStudyOutputs`). */
56
59
  export declare function useSetStudyOutputs(): (id: string, outputs: readonly string[]) => void;
package/dist/context.js CHANGED
@@ -92,6 +92,12 @@ export function useSetStudyOutput() {
92
92
  const ref = TerminalContext.useActorRef();
93
93
  return useCallback((id, output) => ref.send({ type: 'series.setStudyOutput', id, output }), [ref]);
94
94
  }
95
+ /** Switch which column of its source a study reads (`series.setStudyInput`) —
96
+ * the data node's output picker. Pair it with `inputAllowed`. */
97
+ export function useSetStudyInput() {
98
+ const ref = TerminalContext.useActorRef();
99
+ return useCallback((id, column) => ref.send({ type: 'series.setStudyInput', id, column }), [ref]);
100
+ }
95
101
  /** Which outputs of its multi-output source a study runs on — one, several, or
96
102
  * all (`series.setStudyOutputs`). */
97
103
  export function useSetStudyOutputs() {
package/dist/index.d.ts CHANGED
@@ -5,6 +5,6 @@
5
5
  * union — the pre-paid bus contract. The actor context + machine internals stay
6
6
  * private to the module.
7
7
  */
8
- export { TerminalProvider, useRows, useSelectedSeries, useExpandedSeries, useControlsOpen, usePresetSaved, useActiveSlot, useAddSeries, useAddPair, useCompareAll, useAddStudy, useSetStudyParam, useSetStudyOutput, useSetStudyOutputs, useOrderSeries, useReindexSeries, useMoveSeriesToRow, useMoveToAxis, useRemoveSeries, usePatchSeries, useToggleVisible, useToggleGroupVisible, useRemoveGroup, useSelectSeries, useExpandSeries, useSetControlsOpen, useSelectPreset, useSavePreset, useSetRowHeights, useAddRow, useRemoveRow, useAxisRanges, useToggleAxisLink, useSwapAxisSide, useSetAxisRange, useSetAxisAuto, useSetAxisLocks, useSetAxisAutoOpts, useSetAxisFormat, useTerminalHotkeys, type TerminalProviderProps, } from './context.js';
9
- export { PRESET_SLOTS, MAX_ROWS, activeSlot, canonRows, columnLabel, columnUnits, entryColumn, groupMembers, mintId, axisMoveAllowed, linkToggleAllowed, respecAllowed, outputAllowed, outputsAllowed, studyOutputChoices, studyOutputsOf, studyOutputsRead, defaultStudyOutputs, studyChainTail, studyOfferable, SOURCE_COLUMNS, swapAllowed, type EditScope, type RowState, type TerminalPreset, type MetricEntry, type PairLegInput, type PresetStorage, type TerminalEvent, type TerminalInput, } from './terminalMachine.js';
8
+ export { TerminalProvider, useRows, useSelectedSeries, useExpandedSeries, useControlsOpen, usePresetSaved, useActiveSlot, useAddSeries, useAddPair, useCompareAll, useAddStudy, useSetStudyParam, useSetStudyOutput, useSetStudyInput, useSetStudyOutputs, useOrderSeries, useReindexSeries, useMoveSeriesToRow, useMoveToAxis, useRemoveSeries, usePatchSeries, useToggleVisible, useToggleGroupVisible, useRemoveGroup, useSelectSeries, useExpandSeries, useSetControlsOpen, useSelectPreset, useSavePreset, useSetRowHeights, useAddRow, useRemoveRow, useAxisRanges, useToggleAxisLink, useSwapAxisSide, useSetAxisRange, useSetAxisAuto, useSetAxisLocks, useSetAxisAutoOpts, useSetAxisFormat, useTerminalHotkeys, type TerminalProviderProps, } from './context.js';
9
+ export { PRESET_SLOTS, MAX_ROWS, activeSlot, canonRows, columnLabel, columnUnits, entryColumn, groupMembers, mintId, axisMoveAllowed, linkToggleAllowed, respecAllowed, outputAllowed, inputAllowed, outputsAllowed, studyOutputChoices, studyOutputsOf, studyOutputsRead, defaultStudyOutputs, studyChainTail, studyOfferable, SOURCE_COLUMNS, swapAllowed, type EditScope, type RowState, type TerminalPreset, type MetricEntry, type PairLegInput, type PresetStorage, type TerminalEvent, type TerminalInput, } from './terminalMachine.js';
10
10
  export { axisBindings, allowedAxes, canAddUnit, axisForAdd, type AxisMember, } from './axisPolicy.js';
package/dist/index.js CHANGED
@@ -5,8 +5,8 @@
5
5
  * union — the pre-paid bus contract. The actor context + machine internals stay
6
6
  * private to the module.
7
7
  */
8
- export { TerminalProvider, useRows, useSelectedSeries, useExpandedSeries, useControlsOpen, usePresetSaved, useActiveSlot, useAddSeries, useAddPair, useCompareAll, useAddStudy, useSetStudyParam, useSetStudyOutput, useSetStudyOutputs, useOrderSeries, useReindexSeries, useMoveSeriesToRow, useMoveToAxis, useRemoveSeries, usePatchSeries, useToggleVisible, useToggleGroupVisible, useRemoveGroup, useSelectSeries, useExpandSeries, useSetControlsOpen, useSelectPreset, useSavePreset, useSetRowHeights, useAddRow, useRemoveRow, useAxisRanges, useToggleAxisLink, useSwapAxisSide, useSetAxisRange, useSetAxisAuto, useSetAxisLocks, useSetAxisAutoOpts, useSetAxisFormat, useTerminalHotkeys, } from './context.js';
9
- export { PRESET_SLOTS, MAX_ROWS, activeSlot, canonRows, columnLabel, columnUnits, entryColumn, groupMembers, mintId, axisMoveAllowed, linkToggleAllowed, respecAllowed, outputAllowed, outputsAllowed, studyOutputChoices, studyOutputsOf, studyOutputsRead, defaultStudyOutputs, studyChainTail, studyOfferable, SOURCE_COLUMNS, swapAllowed, } from './terminalMachine.js';
8
+ export { TerminalProvider, useRows, useSelectedSeries, useExpandedSeries, useControlsOpen, usePresetSaved, useActiveSlot, useAddSeries, useAddPair, useCompareAll, useAddStudy, useSetStudyParam, useSetStudyOutput, useSetStudyInput, useSetStudyOutputs, useOrderSeries, useReindexSeries, useMoveSeriesToRow, useMoveToAxis, useRemoveSeries, usePatchSeries, useToggleVisible, useToggleGroupVisible, useRemoveGroup, useSelectSeries, useExpandSeries, useSetControlsOpen, useSelectPreset, useSavePreset, useSetRowHeights, useAddRow, useRemoveRow, useAxisRanges, useToggleAxisLink, useSwapAxisSide, useSetAxisRange, useSetAxisAuto, useSetAxisLocks, useSetAxisAutoOpts, useSetAxisFormat, useTerminalHotkeys, } from './context.js';
9
+ export { PRESET_SLOTS, MAX_ROWS, activeSlot, canonRows, columnLabel, columnUnits, entryColumn, groupMembers, mintId, axisMoveAllowed, linkToggleAllowed, respecAllowed, outputAllowed, inputAllowed, outputsAllowed, studyOutputChoices, studyOutputsOf, studyOutputsRead, defaultStudyOutputs, studyChainTail, studyOfferable, SOURCE_COLUMNS, swapAllowed, } from './terminalMachine.js';
10
10
  // The axis policy a host's MENUS gate on — the same predicates the machine's
11
11
  // guards enforce, exported so the two cannot disagree (0.1.0 left them internal;
12
12
  // a host had to keep its own copy).
@@ -164,6 +164,14 @@ export type TerminalEvent = {
164
164
  id: string;
165
165
  output: string;
166
166
  }
167
+ /** Which COLUMN of its source a study reads straight off it (`close` →
168
+ * `high`) — the data node's output picker. The study stays under the data
169
+ * node it is built on, which offers every column of its source. */
170
+ | {
171
+ type: 'series.setStudyInput';
172
+ id: string;
173
+ column: string;
174
+ }
167
175
  /** Which outputs of its multi-output source a study runs on — one, several
168
176
  * (a FAN: one study drawing a line per output), or all. Generalises
169
177
  * `series.setStudyOutput`, and reaches a study of a fanned study too. */
@@ -469,6 +477,21 @@ export declare function respecAllowed(scope: EditScope, id: string, name: string
469
477
  * Total: it runs in a guard, where a throw kills the actor.
470
478
  */
471
479
  export declare function outputAllowed(scope: EditScope, id: string, output: string): boolean;
480
+ /**
481
+ * Whether study `id` can be re-pointed to read `column` of its source — the
482
+ * data node's output picker (`series.setStudyInput`).
483
+ *
484
+ * Narrow on purpose (PR #33 review): only a study whose TARGET is one of a
485
+ * bar's price columns ({@link studyColumn} — an ATR's bar roles are not a
486
+ * target), re-pointed at another of them, off a seated price data node on its
487
+ * own source. So not a catalog metric (a derived vol is a metric, not a study
488
+ * of a column), not a vol source's columns (each is its own metric node), not
489
+ * the column already read, not a fanned study; and never a rewrite the spec or
490
+ * unit gates of {@link outputAllowed} refuse.
491
+ *
492
+ * Total: it runs in a guard, where a throw kills the actor.
493
+ */
494
+ export declare function inputAllowed(scope: EditScope, id: string, column: string): boolean;
472
495
  /**
473
496
  * The outputs a study can be set to run on — its PARENT's
474
497
  * ({@link studyOutputsOf}), or, with no seated parent, every output its chain's
@@ -562,6 +585,10 @@ export declare const terminalMachine: import("xstate").StateMachine<TerminalCont
562
585
  type: "series.setStudyOutput";
563
586
  id: string;
564
587
  output: string;
588
+ } | {
589
+ type: "series.setStudyInput";
590
+ id: string;
591
+ column: string;
565
592
  } | {
566
593
  type: "series.setStudyOutputs";
567
594
  id: string;
@@ -664,6 +691,9 @@ export declare const terminalMachine: import("xstate").StateMachine<TerminalCont
664
691
  } | {
665
692
  type: "setStudyOutput";
666
693
  params: import("xstate").NonReducibleUnknown;
694
+ } | {
695
+ type: "setStudyInput";
696
+ params: import("xstate").NonReducibleUnknown;
667
697
  } | {
668
698
  type: "setStudyOutputs";
669
699
  params: import("xstate").NonReducibleUnknown;
@@ -760,6 +790,9 @@ export declare const terminalMachine: import("xstate").StateMachine<TerminalCont
760
790
  } | {
761
791
  type: "canSetStudyOutput";
762
792
  params: unknown;
793
+ } | {
794
+ type: "canSetStudyInput";
795
+ params: unknown;
763
796
  } | {
764
797
  type: "canSetStudyOutputs";
765
798
  params: unknown;
@@ -1,5 +1,5 @@
1
1
  import { assign, enqueueActions, setup } from 'xstate';
2
- import { COMPARE_PREFIX, hasPairOp, usesCompare, deriveId, opParams, opInputs, opNeedsColumns, opIsBand, opIsMulti, outputMark, opOutputs, TARGET_ROLES, opSharesSourceAxis, studyTag, inputNames, isPickedInput, isPairOp, isValidSpec, partStudyLabel, readPairParts, specUnit, studyInputOf, studyPick, substituteInput, withStudyOutput, fanPick, primaryOutput, withFanOutput, } from '@tidal-ts/core';
2
+ import { COMPARE_PREFIX, hasPairOp, usesCompare, deriveId, opParams, opInputs, opNeedsColumns, opIsBand, opIsMulti, outputMark, opOutputs, TARGET_ROLES, opSharesSourceAxis, studyTag, inputNames, isPickedInput, isPairOp, isValidSpec, partStudyLabel, readPairParts, specUnit, studyInputOf, studyPick, substituteInput, withStudyOutput, studyColumn, withStudyColumn, BAR_PRICE_COLUMNS, fanPick, primaryOutput, withFanOutput, } from '@tidal-ts/core';
3
3
  /**
4
4
  * The bar columns each data SOURCE carries — the one fact that decides whether a
5
5
  * multi-column study is offerable on a series. Only the price series has bars;
@@ -977,6 +977,53 @@ export function outputAllowed(scope, id, output) {
977
977
  return false;
978
978
  }
979
979
  }
980
+ /**
981
+ * Whether study `id` can be re-pointed to read `column` of its source — the
982
+ * data node's output picker (`series.setStudyInput`).
983
+ *
984
+ * Narrow on purpose (PR #33 review): only a study whose TARGET is one of a
985
+ * bar's price columns ({@link studyColumn} — an ATR's bar roles are not a
986
+ * target), re-pointed at another of them, off a seated price data node on its
987
+ * own source. So not a catalog metric (a derived vol is a metric, not a study
988
+ * of a column), not a vol source's columns (each is its own metric node), not
989
+ * the column already read, not a fanned study; and never a rewrite the spec or
990
+ * unit gates of {@link outputAllowed} refuse.
991
+ *
992
+ * Total: it runs in a guard, where a throw kills the actor.
993
+ */
994
+ export function inputAllowed(scope, id, column) {
995
+ try {
996
+ const configs = flatConfigs(scope.rows);
997
+ const target = configs.find((c) => c.id === id);
998
+ if (!target?.derive || (target.fan && target.fan.length > 0))
999
+ return false;
1000
+ if (isCatalogColumn(scope)(target.column))
1001
+ return false;
1002
+ const at = studyColumn(target.derive);
1003
+ if (!at || at.column === column)
1004
+ return false;
1005
+ if (!BAR_PRICE_COLUMNS.includes(at.column) || !BAR_PRICE_COLUMNS.includes(column))
1006
+ return false;
1007
+ const home = dataParent(configs, target);
1008
+ if (!home || !BAR_PRICE_COLUMNS.includes(home.column))
1009
+ return false;
1010
+ if (!isValidSpec(withStudyColumn(target.derive, column)))
1011
+ return false;
1012
+ const next = propagateRespec(configs, id, respecInput(target, column, home.column), unitOfColumn(scope));
1013
+ if (next === null || !unitsStayCoherent(scope.rows, next))
1014
+ return false;
1015
+ const borrowed = target.axisGroup != null && target.axisGroup !== target.id;
1016
+ return !(borrowed && next.get(id).unit !== (target.unit ?? ''));
1017
+ }
1018
+ catch {
1019
+ return false;
1020
+ }
1021
+ }
1022
+ /** The seated DATA node a study reads its raw column off — a node with no
1023
+ * derive of its own, on the study's source. */
1024
+ function dataParent(configs, study) {
1025
+ return sourcesOf(configs, study).find((p) => !p.derive && p.source === study.source);
1026
+ }
980
1027
  /**
981
1028
  * The outputs a study can be set to run on — its PARENT's
982
1029
  * ({@link studyOutputsOf}), or, with no seated parent, every output its chain's
@@ -1304,6 +1351,28 @@ function respecOutput(study, output) {
1304
1351
  : (study.family ?? label);
1305
1352
  return { ...study, column: deriveId(derive), derive, label: `${base} · ${output}${tag}` };
1306
1353
  }
1354
+ /** Re-spec a study to read another column of its source, **in place** — the
1355
+ * identity stands, like {@link respecParam}. Off its data node's own column
1356
+ * the label names the column it reads (`Price · High · BOLLINGER(20, 2)`);
1357
+ * back on it, the segment goes. */
1358
+ function respecInput(study, column, home) {
1359
+ const prev = study.derive;
1360
+ const was = studyColumn(prev).column;
1361
+ const derive = withStudyColumn(prev, column);
1362
+ const tag = ` · ${studyTag(prev)}`;
1363
+ const named = ` · ${columnSegment(was)}${tag}`;
1364
+ // `label` is untyped persisted state; a missing one must not throw here.
1365
+ const label = typeof study.label === 'string' ? study.label : '';
1366
+ const base = was !== home && label.endsWith(named)
1367
+ ? label.slice(0, -named.length)
1368
+ : label.endsWith(tag)
1369
+ ? label.slice(0, -tag.length)
1370
+ : (study.family ?? label);
1371
+ const segment = column === home ? '' : ` · ${columnSegment(column)}`;
1372
+ return { ...study, column: deriveId(derive), derive, label: `${base}${segment}${tag}` };
1373
+ }
1374
+ /** `high` → `High`: a raw column as a label segment says it. */
1375
+ const columnSegment = (column) => column.charAt(0).toUpperCase() + column.slice(1);
1307
1376
  function propagateRespec(configs, rootId, root, unitOf) {
1308
1377
  const rootCfg = configs.find((c) => c.id === rootId);
1309
1378
  if (!rootCfg?.derive || !root.derive)
@@ -1823,6 +1892,7 @@ export const terminalMachine = setup({
1823
1892
  // the actor-killing hazard `canAddStudy` guards (a throwing guard kills the
1824
1893
  // actor, and the `>= 2` pre-check does not cover the registry's max).
1825
1894
  canSetStudyOutput: ({ context, event }) => event.type === 'series.setStudyOutput' && outputAllowed(context, event.id, event.output),
1895
+ canSetStudyInput: ({ context, event }) => event.type === 'series.setStudyInput' && inputAllowed(context, event.id, event.column),
1826
1896
  canSetStudyOutputs: ({ context, event }) => event.type === 'series.setStudyOutputs' && outputsAllowed(context, event.id, event.outputs),
1827
1897
  canSetStudyParam: ({ context, event }) => event.type === 'series.setStudyParam' &&
1828
1898
  respecAllowed(context, event.id, event.name, event.value),
@@ -2026,6 +2096,41 @@ export const terminalMachine = setup({
2026
2096
  return applyRespec(context, event.id, respecOutput(target, event.output));
2027
2097
  },
2028
2098
  }),
2099
+ setStudyInput: assign({
2100
+ rows: ({ context, event }) => {
2101
+ if (event.type !== 'series.setStudyInput')
2102
+ return context.rows;
2103
+ const configs = flatConfigs(context.rows);
2104
+ const target = configs.find((c) => c.id === event.id);
2105
+ const home = target && dataParent(configs, target);
2106
+ const at = target?.derive && studyColumn(target.derive);
2107
+ if (!target || !home || !at)
2108
+ return context.rows;
2109
+ const rows = applyRespec(context, event.id, respecInput(target, event.column, home.column));
2110
+ if (rows === context.rows)
2111
+ return rows; // refused: never half-apply
2112
+ // Off its home column the study finds its data node only through
2113
+ // `parentIds`. A study that already records one keeps it; one saved
2114
+ // before studies recorded their parents gains it — but only when the
2115
+ // node it was found on is the ONE seated node drawing that column, so a
2116
+ // guess is never written down as a fact (PR #33 review).
2117
+ if (target.parentIds?.[at.index] != null)
2118
+ return rows;
2119
+ const homes = configs.filter((c) => !c.derive && c.source === target.source && c.column === at.column);
2120
+ if (homes.length !== 1)
2121
+ return rows;
2122
+ return rows.map((r) => ({
2123
+ ...r,
2124
+ configs: r.configs.map((c) => {
2125
+ if (c.id !== event.id)
2126
+ return c;
2127
+ const parentIds = [...(c.parentIds ?? [])];
2128
+ parentIds[at.index] = home.id;
2129
+ return { ...c, parentIds };
2130
+ }),
2131
+ }));
2132
+ },
2133
+ }),
2029
2134
  setStudyOutputs: assign({
2030
2135
  rows: ({ context, event }) => {
2031
2136
  if (event.type !== 'series.setStudyOutputs')
@@ -2723,6 +2828,7 @@ export const terminalMachine = setup({
2723
2828
  },
2724
2829
  'series.setStudyParam': { guard: 'canSetStudyParam', actions: 'setStudyParam' },
2725
2830
  'series.setStudyOutput': { guard: 'canSetStudyOutput', actions: 'setStudyOutput' },
2831
+ 'series.setStudyInput': { guard: 'canSetStudyInput', actions: 'setStudyInput' },
2726
2832
  'series.setStudyOutputs': { guard: 'canSetStudyOutputs', actions: 'setStudyOutputs' },
2727
2833
  'series.order': { actions: 'orderSeries' },
2728
2834
  'series.reindex': { actions: 'orderSeries' },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tidal-ts/terminal",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "private": false,
5
5
  "description": "The terminal machine — an XState v5 statechart that owns a chart's configuration (rows, series, studies, pairs, axes, presets) and never its data.",
6
6
  "license": "MIT",
@@ -36,8 +36,8 @@
36
36
  "xstate": "^5.28.0"
37
37
  },
38
38
  "dependencies": {
39
- "@tidal-ts/chart": "0.11.0",
40
- "@tidal-ts/core": "0.11.0"
39
+ "@tidal-ts/chart": "0.12.0",
40
+ "@tidal-ts/core": "0.12.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@pond-ts/charts": "^0.73.0",