@narumitw/pi-ticker 0.1.0 → 0.1.2
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/README.md +12 -47
- package/dist/chunks/{chunk-U5NETXSC.js → chunk-5QPNCY3F.ts} +1 -1
- package/dist/chunks/chunk-5QPNCY3F.ts.map +7 -0
- package/dist/chunks/{menu-WR7QV3P4.js → menu-SMXTISJV.ts} +7 -10
- package/dist/chunks/menu-SMXTISJV.ts.map +7 -0
- package/dist/chunks/{tui-menu-KQLX7NJ5.js → tui-menu-644RY6QY.ts} +21 -43
- package/dist/chunks/tui-menu-644RY6QY.ts.map +7 -0
- package/dist/index.ts +5 -17
- package/dist/index.ts.map +2 -2
- package/package.json +54 -55
- package/src/menu.ts +207 -226
- package/src/quotes.ts +67 -73
- package/src/render.ts +107 -114
- package/src/settings.ts +105 -111
- package/src/ticker.ts +263 -286
- package/src/tui-menu.ts +416 -460
- package/dist/chunks/chunk-U5NETXSC.js.map +0 -7
- package/dist/chunks/menu-WR7QV3P4.js.map +0 -7
- package/dist/chunks/tui-menu-KQLX7NJ5.js.map +0 -7
package/src/ticker.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
type ExtensionAPI,
|
|
4
|
+
type ExtensionCommandContext,
|
|
5
|
+
type ExtensionContext,
|
|
6
|
+
getAgentDir,
|
|
7
|
+
type Theme,
|
|
8
8
|
} from "@earendil-works/pi-coding-agent";
|
|
9
9
|
import type { AutocompleteItem } from "@earendil-works/pi-tui";
|
|
10
10
|
import { sanitizeTerminalText } from "@narumitw/pi-tui-kit/terminal-text";
|
|
11
11
|
import { fetchQuotes, QuoteRequestTimeoutError } from "./quotes.js";
|
|
12
12
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
formatTickerPlain,
|
|
14
|
+
mergeQuoteResults,
|
|
15
|
+
renderTickerWidget,
|
|
16
|
+
type TickerItem,
|
|
17
|
+
type TickerView,
|
|
18
18
|
} from "./render.js";
|
|
19
19
|
import { createSettingsWriter, loadSettings, normalizeSymbols, parseSymbols } from "./settings.js";
|
|
20
20
|
|
|
21
21
|
export interface StockTickerDependencies {
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
createSettingsWriter: typeof createSettingsWriter;
|
|
23
|
+
loadSettings: typeof loadSettings;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export const WIDGET_KEY = "ticker";
|
|
@@ -30,312 +30,289 @@ export const REQUEST_TIMEOUT_MS = 10_000;
|
|
|
30
30
|
|
|
31
31
|
const HELP = "Usage: /ticker [SYMBOL ...] | /ticker refresh | /ticker reset | /ticker help";
|
|
32
32
|
const COMPLETIONS: AutocompleteItem[] = [
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
{ value: "refresh", label: "refresh", description: "Refresh quotes now" },
|
|
34
|
+
{ value: "reset", label: "reset", description: "Explain why no default reset is available" },
|
|
35
|
+
{ value: "help", label: "help", description: "Show command usage" },
|
|
36
36
|
];
|
|
37
37
|
|
|
38
|
-
export default function stockTicker(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
let view: TickerView = createInitialView(symbols);
|
|
52
|
-
const settingsWriter = (dependencies.createSettingsWriter ?? createSettingsWriter)();
|
|
53
|
-
const readSettings = dependencies.loadSettings ?? loadSettings;
|
|
38
|
+
export default function stockTicker(pi: ExtensionAPI, dependencies: Partial<StockTickerDependencies> = {}): void {
|
|
39
|
+
let activeSession: ExtensionContext["sessionManager"] | undefined;
|
|
40
|
+
let generation = 0;
|
|
41
|
+
let cycleVersion = 0;
|
|
42
|
+
let pollTimer: ReturnType<typeof setTimeout> | undefined;
|
|
43
|
+
let requestController: AbortController | undefined;
|
|
44
|
+
let sessionController: AbortController | undefined;
|
|
45
|
+
const activeMenus = new Set<Promise<void>>();
|
|
46
|
+
let symbols: string[] = [];
|
|
47
|
+
let widgetEnabled = true;
|
|
48
|
+
let view: TickerView = createInitialView(symbols);
|
|
49
|
+
const settingsWriter = (dependencies.createSettingsWriter ?? createSettingsWriter)();
|
|
50
|
+
const readSettings = dependencies.loadSettings ?? loadSettings;
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
const ownsSession = (ctx: ExtensionContext, expectedGeneration = generation): boolean =>
|
|
53
|
+
ctx.sessionManager === activeSession && expectedGeneration === generation;
|
|
57
54
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
55
|
+
const publish = (ctx: ExtensionContext): void => {
|
|
56
|
+
if (!ctx.hasUI || !ownsSession(ctx)) return;
|
|
57
|
+
if (!widgetEnabled || view.items.length === 0) {
|
|
58
|
+
ctx.ui.setWidget(WIDGET_KEY, undefined);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const snapshot = cloneView(view);
|
|
62
|
+
if (ctx.mode === "tui") {
|
|
63
|
+
ctx.ui.setWidget(
|
|
64
|
+
WIDGET_KEY,
|
|
65
|
+
(_tui: unknown, theme: Theme) => ({
|
|
66
|
+
render: (width: number) => renderTickerWidget(snapshot, theme, width),
|
|
67
|
+
invalidate: () => {},
|
|
68
|
+
}),
|
|
69
|
+
{ placement: "aboveEditor" },
|
|
70
|
+
);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
ctx.ui.setWidget(WIDGET_KEY, formatTickerPlain(snapshot), { placement: "aboveEditor" });
|
|
74
|
+
};
|
|
78
75
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
76
|
+
const stopPolling = (): void => {
|
|
77
|
+
cycleVersion += 1;
|
|
78
|
+
if (pollTimer) clearTimeout(pollTimer);
|
|
79
|
+
pollTimer = undefined;
|
|
80
|
+
requestController?.abort();
|
|
81
|
+
requestController = undefined;
|
|
82
|
+
};
|
|
86
83
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
84
|
+
const clearWidget = (ctx: ExtensionContext): void => {
|
|
85
|
+
if (ctx.hasUI) ctx.ui.setWidget(WIDGET_KEY, undefined);
|
|
86
|
+
};
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
): Promise<void> => {
|
|
96
|
-
if (!ownsSession(ctx, expectedGeneration) || expectedCycle !== cycleVersion) return;
|
|
97
|
-
view = { ...view, loading: true };
|
|
98
|
-
publish(ctx);
|
|
88
|
+
const runCycle = async (ctx: ExtensionContext, expectedGeneration: number, expectedCycle: number): Promise<void> => {
|
|
89
|
+
if (!ownsSession(ctx, expectedGeneration) || expectedCycle !== cycleVersion) return;
|
|
90
|
+
view = { ...view, loading: true };
|
|
91
|
+
publish(ctx);
|
|
99
92
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
clearTimeout(timeout);
|
|
133
|
-
if (requestController === controller) requestController = undefined;
|
|
134
|
-
}
|
|
135
|
-
};
|
|
93
|
+
const controller = new AbortController();
|
|
94
|
+
requestController = controller;
|
|
95
|
+
const timeout = setTimeout(() => {
|
|
96
|
+
controller.abort(new QuoteRequestTimeoutError());
|
|
97
|
+
}, REQUEST_TIMEOUT_MS);
|
|
98
|
+
try {
|
|
99
|
+
const results = await fetchQuotes(symbols, controller.signal);
|
|
100
|
+
if (!ownsSession(ctx, expectedGeneration) || expectedCycle !== cycleVersion) return;
|
|
101
|
+
view = {
|
|
102
|
+
items: mergeQuoteResults(view.items, results),
|
|
103
|
+
loading: false,
|
|
104
|
+
updatedAt: results.some((result) => result.quote) ? Date.now() : view.updatedAt,
|
|
105
|
+
};
|
|
106
|
+
publish(ctx);
|
|
107
|
+
} catch {
|
|
108
|
+
if (!controller.signal.aborted && ownsSession(ctx, expectedGeneration) && expectedCycle === cycleVersion) {
|
|
109
|
+
view = {
|
|
110
|
+
items: view.items.map((item) => ({
|
|
111
|
+
...item,
|
|
112
|
+
stale: Boolean(item.quote),
|
|
113
|
+
error: "unavailable",
|
|
114
|
+
})),
|
|
115
|
+
loading: false,
|
|
116
|
+
updatedAt: view.updatedAt,
|
|
117
|
+
};
|
|
118
|
+
publish(ctx);
|
|
119
|
+
}
|
|
120
|
+
} finally {
|
|
121
|
+
clearTimeout(timeout);
|
|
122
|
+
if (requestController === controller) requestController = undefined;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
136
125
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
126
|
+
const startCycle = (ctx: ExtensionContext, expectedGeneration = generation): void => {
|
|
127
|
+
if (!ownsSession(ctx, expectedGeneration)) return;
|
|
128
|
+
if (!widgetEnabled || symbols.length === 0) {
|
|
129
|
+
stopPolling();
|
|
130
|
+
publish(ctx);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (pollTimer) clearTimeout(pollTimer);
|
|
134
|
+
requestController?.abort();
|
|
135
|
+
const expectedCycle = ++cycleVersion;
|
|
136
|
+
void runCycle(ctx, expectedGeneration, expectedCycle).finally(() => {
|
|
137
|
+
if (!ownsSession(ctx, expectedGeneration) || expectedCycle !== cycleVersion) return;
|
|
138
|
+
pollTimer = setTimeout(() => startCycle(ctx, expectedGeneration), POLL_INTERVAL_MS);
|
|
139
|
+
});
|
|
140
|
+
};
|
|
152
141
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
142
|
+
const openMenu = async (ctx: ExtensionCommandContext): Promise<void> => {
|
|
143
|
+
const controller = sessionController;
|
|
144
|
+
const expectedGeneration = generation;
|
|
145
|
+
if (!controller || controller.signal.aborted || !ownsSession(ctx, expectedGeneration)) return;
|
|
146
|
+
const running = (async () => {
|
|
147
|
+
const { showTickerMenu } = await import("./menu.js");
|
|
148
|
+
if (controller.signal.aborted || !ownsSession(ctx, expectedGeneration)) return;
|
|
149
|
+
await showTickerMenu(ctx, {
|
|
150
|
+
signal: controller.signal,
|
|
151
|
+
isCurrent: () => !controller.signal.aborted && ownsSession(ctx, expectedGeneration),
|
|
152
|
+
getSymbols: () => [...symbols],
|
|
153
|
+
applySymbols: async (nextSymbols, signal) => {
|
|
154
|
+
if (signal.aborted || !ownsSession(ctx, expectedGeneration)) return;
|
|
155
|
+
const normalized = normalizeSymbols(nextSymbols, true);
|
|
156
|
+
await settingsWriter.save(settingsPath(), normalized);
|
|
157
|
+
if (signal.aborted || !ownsSession(ctx, expectedGeneration)) return;
|
|
158
|
+
symbols = normalized;
|
|
159
|
+
view = createInitialView(symbols);
|
|
160
|
+
publish(ctx);
|
|
161
|
+
startCycle(ctx, expectedGeneration);
|
|
162
|
+
},
|
|
163
|
+
getWidgetEnabled: () => widgetEnabled,
|
|
164
|
+
applyWidgetEnabled: async (nextWidgetEnabled, signal) => {
|
|
165
|
+
if (signal.aborted || !ownsSession(ctx, expectedGeneration)) return;
|
|
166
|
+
await settingsWriter.saveWidgetEnabled(settingsPath(), nextWidgetEnabled);
|
|
167
|
+
if (signal.aborted || !ownsSession(ctx, expectedGeneration)) return;
|
|
168
|
+
widgetEnabled = nextWidgetEnabled;
|
|
169
|
+
if (!widgetEnabled) stopPolling();
|
|
170
|
+
publish(ctx);
|
|
171
|
+
if (widgetEnabled) startCycle(ctx, expectedGeneration);
|
|
172
|
+
},
|
|
173
|
+
refresh: () => {
|
|
174
|
+
if (!ownsSession(ctx, expectedGeneration)) return;
|
|
175
|
+
if (!widgetEnabled) {
|
|
176
|
+
notify(ctx, "Ticker widget is disabled. Enable it before refreshing.", "warning");
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
startCycle(ctx, expectedGeneration);
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
})();
|
|
183
|
+
activeMenus.add(running);
|
|
184
|
+
try {
|
|
185
|
+
await running;
|
|
186
|
+
} finally {
|
|
187
|
+
activeMenus.delete(running);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
201
190
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
191
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
192
|
+
sessionController?.abort();
|
|
193
|
+
stopPolling();
|
|
194
|
+
activeSession = ctx.sessionManager;
|
|
195
|
+
sessionController = new AbortController();
|
|
196
|
+
const expectedGeneration = ++generation;
|
|
197
|
+
symbols = [];
|
|
198
|
+
widgetEnabled = true;
|
|
199
|
+
view = createInitialView(symbols);
|
|
200
|
+
publish(ctx);
|
|
212
201
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
202
|
+
await settingsWriter.flush();
|
|
203
|
+
if (!ownsSession(ctx, expectedGeneration)) return;
|
|
204
|
+
const loaded = await readSettings(settingsPath());
|
|
205
|
+
if (!ownsSession(ctx, expectedGeneration)) return;
|
|
206
|
+
symbols = loaded.settings.symbols;
|
|
207
|
+
widgetEnabled = loaded.settings.widgetEnabled;
|
|
208
|
+
view = createInitialView(symbols);
|
|
209
|
+
publish(ctx);
|
|
210
|
+
if (loaded.warning) notify(ctx, loaded.warning, "warning");
|
|
211
|
+
if (ctx.hasUI && widgetEnabled && symbols.length > 0) startCycle(ctx, expectedGeneration);
|
|
212
|
+
});
|
|
224
213
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
214
|
+
pi.on("session_shutdown", async (_event, ctx) => {
|
|
215
|
+
if (!ownsSession(ctx)) return;
|
|
216
|
+
sessionController?.abort();
|
|
217
|
+
stopPolling();
|
|
218
|
+
clearWidget(ctx);
|
|
219
|
+
activeSession = undefined;
|
|
220
|
+
sessionController = undefined;
|
|
221
|
+
generation += 1;
|
|
222
|
+
await Promise.allSettled([...activeMenus]);
|
|
223
|
+
await settingsWriter.flush();
|
|
224
|
+
});
|
|
236
225
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
226
|
+
pi.registerCommand("ticker", {
|
|
227
|
+
description: "Show or change the stock ticker symbols",
|
|
228
|
+
getArgumentCompletions: completeArguments,
|
|
229
|
+
handler: async (argumentsText, ctx) => {
|
|
230
|
+
if (!ctx.hasUI) {
|
|
231
|
+
throw new Error("The /ticker command requires TUI or RPC mode.");
|
|
232
|
+
}
|
|
233
|
+
const input = argumentsText.trim();
|
|
234
|
+
if (!input) {
|
|
235
|
+
await openMenu(ctx);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
249
238
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (ownsSession(ctx)) startCycle(ctx);
|
|
270
|
-
notify(ctx, "Refreshing stock quotes.");
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
239
|
+
const words = input.split(/\s+/);
|
|
240
|
+
const route = words[0]?.toLowerCase();
|
|
241
|
+
if (["help", "refresh", "reset"].includes(route ?? "") && words.length > 1) {
|
|
242
|
+
notify(ctx, `${route} does not accept trailing arguments.`, "error");
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (route === "help") {
|
|
246
|
+
notify(ctx, HELP);
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (route === "refresh") {
|
|
250
|
+
if (!widgetEnabled) {
|
|
251
|
+
notify(ctx, "Ticker widget is disabled. Enable it with /ticker before refreshing.", "warning");
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (ownsSession(ctx)) startCycle(ctx);
|
|
255
|
+
notify(ctx, "Refreshing stock quotes.");
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
273
258
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
"error",
|
|
279
|
-
);
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
259
|
+
if (route === "reset") {
|
|
260
|
+
notify(ctx, "No default ticker list is configured. Use /ticker SYMBOL ... instead.", "error");
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
282
263
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
264
|
+
let nextSymbols: string[];
|
|
265
|
+
try {
|
|
266
|
+
nextSymbols = parseSymbols([input]);
|
|
267
|
+
} catch (error) {
|
|
268
|
+
notify(ctx, errorMessage(error), "error");
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
await settingsWriter.save(settingsPath(), nextSymbols);
|
|
273
|
+
} catch (error) {
|
|
274
|
+
notify(ctx, `Could not save stock ticker settings: ${errorMessage(error)}`, "error");
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (!ownsSession(ctx)) return;
|
|
278
|
+
symbols = nextSymbols;
|
|
279
|
+
view = createInitialView(symbols);
|
|
280
|
+
publish(ctx);
|
|
281
|
+
startCycle(ctx);
|
|
282
|
+
notify(ctx, `Stock ticker symbols: ${symbols.join(" ")}`);
|
|
283
|
+
},
|
|
284
|
+
});
|
|
304
285
|
}
|
|
305
286
|
|
|
306
287
|
export function settingsPath(): string {
|
|
307
|
-
|
|
288
|
+
return join(getAgentDir(), SETTINGS_FILE_NAME);
|
|
308
289
|
}
|
|
309
290
|
|
|
310
291
|
export function completeArguments(prefix: string): AutocompleteItem[] | null {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
292
|
+
if (/\s/.test(prefix)) return null;
|
|
293
|
+
const normalized = prefix.toLowerCase();
|
|
294
|
+
const matches = COMPLETIONS.filter((item) => item.value.toLowerCase().startsWith(normalized));
|
|
295
|
+
return matches.length > 0 ? matches : null;
|
|
315
296
|
}
|
|
316
297
|
|
|
317
298
|
function createInitialView(symbols: readonly string[]): TickerView {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
299
|
+
return {
|
|
300
|
+
items: symbols.map((symbol): TickerItem => ({ symbol })),
|
|
301
|
+
loading: true,
|
|
302
|
+
};
|
|
322
303
|
}
|
|
323
304
|
|
|
324
305
|
function cloneView(view: TickerView): TickerView {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
306
|
+
return {
|
|
307
|
+
...view,
|
|
308
|
+
items: view.items.map((item) => ({ ...item, quote: item.quote && { ...item.quote } })),
|
|
309
|
+
};
|
|
329
310
|
}
|
|
330
311
|
|
|
331
|
-
function notify(
|
|
332
|
-
|
|
333
|
-
message: string,
|
|
334
|
-
type: "info" | "warning" | "error" = "info",
|
|
335
|
-
): void {
|
|
336
|
-
if (ctx.hasUI) ctx.ui.notify(sanitizeTerminalText(message), type);
|
|
312
|
+
function notify(ctx: ExtensionContext, message: string, type: "info" | "warning" | "error" = "info"): void {
|
|
313
|
+
if (ctx.hasUI) ctx.ui.notify(sanitizeTerminalText(message), type);
|
|
337
314
|
}
|
|
338
315
|
|
|
339
316
|
function errorMessage(error: unknown): string {
|
|
340
|
-
|
|
317
|
+
return error instanceof Error ? error.message : String(error);
|
|
341
318
|
}
|