@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/tui-menu.ts
CHANGED
|
@@ -6,10 +6,10 @@ import { parseSymbols } from "./settings.js";
|
|
|
6
6
|
type TuiMenuResult = "add" | "close";
|
|
7
7
|
type TickerRow = { kind: "ticker"; id: string; symbol: string };
|
|
8
8
|
type ActionRow = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
kind: "action";
|
|
10
|
+
id: "add" | "widget" | "refresh";
|
|
11
|
+
label: string;
|
|
12
|
+
description: string;
|
|
13
13
|
};
|
|
14
14
|
type Row = TickerRow | ActionRow;
|
|
15
15
|
|
|
@@ -17,483 +17,439 @@ const PASTE_START = "\u001b[200~";
|
|
|
17
17
|
const PASTE_END = "\u001b[201~";
|
|
18
18
|
// Keep these aligned with Input.handleInput so configured editing actions beat additive reorder.
|
|
19
19
|
const RESERVED_INPUT_BINDINGS = [
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
20
|
+
"tui.select.up",
|
|
21
|
+
"tui.select.down",
|
|
22
|
+
"tui.select.pageUp",
|
|
23
|
+
"tui.select.pageDown",
|
|
24
|
+
"tui.select.confirm",
|
|
25
|
+
"tui.select.cancel",
|
|
26
|
+
"tui.editor.undo",
|
|
27
|
+
"tui.input.submit",
|
|
28
|
+
"tui.editor.deleteCharBackward",
|
|
29
|
+
"tui.editor.deleteCharForward",
|
|
30
|
+
"tui.editor.deleteWordBackward",
|
|
31
|
+
"tui.editor.deleteWordForward",
|
|
32
|
+
"tui.editor.deleteToLineStart",
|
|
33
|
+
"tui.editor.deleteToLineEnd",
|
|
34
|
+
"tui.editor.yank",
|
|
35
|
+
"tui.editor.yankPop",
|
|
36
|
+
"tui.editor.cursorLeft",
|
|
37
|
+
"tui.editor.cursorRight",
|
|
38
|
+
"tui.editor.cursorLineStart",
|
|
39
|
+
"tui.editor.cursorLineEnd",
|
|
40
|
+
"tui.editor.cursorWordLeft",
|
|
41
|
+
"tui.editor.cursorWordRight",
|
|
42
42
|
] as const;
|
|
43
43
|
|
|
44
44
|
// Pi TUI Kit multi-select screens do not expose in-place row-reorder shortcuts.
|
|
45
45
|
// This specialized TUI keeps the same search, toggle, action, binding, and cancellation behavior
|
|
46
46
|
// while RPC mode continues to use the Kit's declarative adapter in menu.ts.
|
|
47
|
-
export async function showTickerTuiMenu(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
export async function showTickerTuiMenu(ctx: ExtensionCommandContext, options: TickerMenuOptions): Promise<void> {
|
|
48
|
+
const {
|
|
49
|
+
canonicalizeConfiguredKey,
|
|
50
|
+
formatInteractionHints,
|
|
51
|
+
HorizontalRule,
|
|
52
|
+
runCustomInteraction,
|
|
53
|
+
sanitizeTerminalText,
|
|
54
|
+
} = await import("@narumitw/pi-tui-kit");
|
|
53
55
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
{ keys: ["ctrl+c"], label: "close" },
|
|
108
|
-
]);
|
|
56
|
+
while (!options.signal.aborted && options.isCurrent()) {
|
|
57
|
+
const result = await runCustomInteraction<TuiMenuResult>(ctx, {
|
|
58
|
+
signal: options.signal,
|
|
59
|
+
isCurrent: options.isCurrent,
|
|
60
|
+
onError: (errorCtx, error) => {
|
|
61
|
+
if (!errorCtx.hasUI) return;
|
|
62
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
63
|
+
errorCtx.ui.notify(sanitizeTerminalText(message), "error");
|
|
64
|
+
},
|
|
65
|
+
create: ({ tui, theme, keybindings, complete }) => {
|
|
66
|
+
let symbols = [...options.getSymbols()];
|
|
67
|
+
let widgetEnabled = options.getWidgetEnabled();
|
|
68
|
+
const rawSearchInput = new Input();
|
|
69
|
+
const searchInput = new Input();
|
|
70
|
+
let selectedIndex = 0;
|
|
71
|
+
let rows: Row[] = [];
|
|
72
|
+
let busy = false;
|
|
73
|
+
let disposed = false;
|
|
74
|
+
let pasteActive = false;
|
|
75
|
+
let errorMessage: string | undefined;
|
|
76
|
+
let pending = Promise.resolve();
|
|
77
|
+
const shortcutUp = shortcutAvailable(keybindings, "shift+up", canonicalizeConfiguredKey);
|
|
78
|
+
const shortcutDown = shortcutAvailable(keybindings, "shift+down", canonicalizeConfiguredKey);
|
|
79
|
+
const activationHint = (label: string) =>
|
|
80
|
+
formatInteractionHints(keybindings, [{ bindings: ["tui.select.confirm"], keys: ["space"], label }]);
|
|
81
|
+
const reorderKeys = [shortcutUp, shortcutDown].filter(
|
|
82
|
+
(shortcut): shortcut is "shift+up" | "shift+down" => shortcut !== undefined,
|
|
83
|
+
);
|
|
84
|
+
const tickerDescription = `${[
|
|
85
|
+
activationHint("removes this ticker"),
|
|
86
|
+
...(reorderKeys.length > 0 ? [`${reorderKeys.join("/")} changes its order`] : []),
|
|
87
|
+
].join("; ")}.`;
|
|
88
|
+
const topRule = new HorizontalRule({
|
|
89
|
+
label: "Manage tickers",
|
|
90
|
+
labelAlignment: "left",
|
|
91
|
+
ruleStyle: (text) => theme.fg("borderMuted", text),
|
|
92
|
+
labelStyle: (text) => theme.fg("accent", text),
|
|
93
|
+
});
|
|
94
|
+
const bottomRule = new HorizontalRule({
|
|
95
|
+
ruleStyle: (text) => theme.fg("borderMuted", text),
|
|
96
|
+
});
|
|
97
|
+
const hint = formatInteractionHints(keybindings, [
|
|
98
|
+
{ bindings: ["tui.select.up", "tui.select.down"], label: "select" },
|
|
99
|
+
...(shortcutUp ? [{ keys: [shortcutUp], label: "move up" }] : []),
|
|
100
|
+
...(shortcutDown ? [{ keys: [shortcutDown], label: "move down" }] : []),
|
|
101
|
+
{ bindings: ["tui.select.confirm"], keys: ["space"], label: "toggle/select" },
|
|
102
|
+
{
|
|
103
|
+
bindings: ["tui.select.cancel"],
|
|
104
|
+
excludeKeys: ["ctrl+c"],
|
|
105
|
+
label: "close",
|
|
106
|
+
},
|
|
107
|
+
{ keys: ["ctrl+c"], label: "close" },
|
|
108
|
+
]);
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const preferredIndex = preferredId ? rows.findIndex((row) => row.id === preferredId) : -1;
|
|
152
|
-
selectedIndex =
|
|
153
|
-
preferredIndex >= 0
|
|
154
|
-
? preferredIndex
|
|
155
|
-
: Math.max(0, Math.min(fallbackIndex, rows.length - 1));
|
|
156
|
-
tui.requestRender();
|
|
157
|
-
};
|
|
158
|
-
rebuild();
|
|
110
|
+
const actionRows = (directSymbol?: string): ActionRow[] => [
|
|
111
|
+
{
|
|
112
|
+
kind: "action",
|
|
113
|
+
id: "add",
|
|
114
|
+
label: directSymbol ? `Add ${sanitizeTerminalText(directSymbol)}` : "Add custom ticker…",
|
|
115
|
+
description: directSymbol
|
|
116
|
+
? `${activationHint(`adds ${sanitizeTerminalText(directSymbol)} directly`)}.`
|
|
117
|
+
: "Add and select one Yahoo Finance symbol.",
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
kind: "action",
|
|
121
|
+
id: "widget",
|
|
122
|
+
label: `Widget: ${widgetEnabled ? "On" : "Off"}`,
|
|
123
|
+
description: "Show or hide the editor widget and persist this setting.",
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
kind: "action",
|
|
127
|
+
id: "refresh",
|
|
128
|
+
label: "Refresh now",
|
|
129
|
+
description: widgetEnabled
|
|
130
|
+
? "Fetch the selected symbols immediately."
|
|
131
|
+
: "Enable the widget before refreshing.",
|
|
132
|
+
},
|
|
133
|
+
];
|
|
134
|
+
const buildRows = (): Row[] => {
|
|
135
|
+
const tickerRows = symbols.map((symbol): TickerRow => ({ kind: "ticker", id: symbol, symbol }));
|
|
136
|
+
const query = searchInput.getValue().toLowerCase();
|
|
137
|
+
const filtered = fuzzyFilter(tickerRows, query, (row) =>
|
|
138
|
+
sanitizeTerminalText(row.symbol.replaceAll("-", " ")).toLowerCase(),
|
|
139
|
+
);
|
|
140
|
+
const directSymbol = filtered.length === 0 ? tryDirectSymbol(rawSearchInput.getValue()) : undefined;
|
|
141
|
+
return [...filtered, ...actionRows(directSymbol)];
|
|
142
|
+
};
|
|
143
|
+
const selectedRow = () => rows[selectedIndex];
|
|
144
|
+
const rebuild = (preferredId?: string, fallbackIndex = selectedIndex) => {
|
|
145
|
+
rows = buildRows();
|
|
146
|
+
const preferredIndex = preferredId ? rows.findIndex((row) => row.id === preferredId) : -1;
|
|
147
|
+
selectedIndex = preferredIndex >= 0 ? preferredIndex : Math.max(0, Math.min(fallbackIndex, rows.length - 1));
|
|
148
|
+
tui.requestRender();
|
|
149
|
+
};
|
|
150
|
+
rebuild();
|
|
159
151
|
|
|
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
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
complete("add");
|
|
286
|
-
} else if (row.id === "widget") persistWidgetEnabled(!widgetEnabled);
|
|
287
|
-
else options.refresh();
|
|
288
|
-
};
|
|
289
|
-
const handleSearch = (data: string) => {
|
|
290
|
-
const previousId = selectedRow()?.id;
|
|
291
|
-
errorMessage = undefined;
|
|
292
|
-
rawSearchInput.handleInput(data);
|
|
293
|
-
searchInput.handleInput(data);
|
|
294
|
-
searchInput.setValue(sanitizeTerminalText(rawSearchInput.getValue()));
|
|
295
|
-
rebuild(previousId, 0);
|
|
296
|
-
};
|
|
152
|
+
const select = (index: number, wrap: boolean) => {
|
|
153
|
+
if (rows.length === 0) return;
|
|
154
|
+
selectedIndex = wrap ? (index + rows.length) % rows.length : Math.max(0, Math.min(index, rows.length - 1));
|
|
155
|
+
tui.requestRender();
|
|
156
|
+
};
|
|
157
|
+
const persist = (
|
|
158
|
+
nextSymbols: readonly string[],
|
|
159
|
+
previousSymbols: readonly string[],
|
|
160
|
+
preferredId: string | undefined,
|
|
161
|
+
previousIndex: number,
|
|
162
|
+
clearSearchOnSuccess = false,
|
|
163
|
+
) => {
|
|
164
|
+
busy = true;
|
|
165
|
+
errorMessage = undefined;
|
|
166
|
+
symbols = [...nextSymbols];
|
|
167
|
+
rebuild(preferredId, previousIndex);
|
|
168
|
+
pending = (async () => {
|
|
169
|
+
try {
|
|
170
|
+
await options.applySymbols(symbols, options.signal);
|
|
171
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
172
|
+
symbols = [...options.getSymbols()];
|
|
173
|
+
if (clearSearchOnSuccess) {
|
|
174
|
+
rawSearchInput.setValue("");
|
|
175
|
+
searchInput.setValue("");
|
|
176
|
+
}
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
179
|
+
symbols = [...previousSymbols];
|
|
180
|
+
errorMessage = sanitizeTerminalText(error instanceof Error ? error.message : String(error));
|
|
181
|
+
ctx.ui.notify(`Ticker settings were not saved: ${errorMessage}`, "error");
|
|
182
|
+
} finally {
|
|
183
|
+
busy = false;
|
|
184
|
+
if (!disposed && !options.signal.aborted && options.isCurrent()) {
|
|
185
|
+
rebuild(preferredId, previousIndex);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
})();
|
|
189
|
+
};
|
|
190
|
+
const persistWidgetEnabled = (nextWidgetEnabled: boolean) => {
|
|
191
|
+
const previousWidgetEnabled = widgetEnabled;
|
|
192
|
+
widgetEnabled = nextWidgetEnabled;
|
|
193
|
+
busy = true;
|
|
194
|
+
errorMessage = undefined;
|
|
195
|
+
rebuild("widget");
|
|
196
|
+
pending = (async () => {
|
|
197
|
+
try {
|
|
198
|
+
await options.applyWidgetEnabled(nextWidgetEnabled, options.signal);
|
|
199
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
200
|
+
widgetEnabled = options.getWidgetEnabled();
|
|
201
|
+
} catch (error) {
|
|
202
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
203
|
+
widgetEnabled = previousWidgetEnabled;
|
|
204
|
+
errorMessage = sanitizeTerminalText(error instanceof Error ? error.message : String(error));
|
|
205
|
+
ctx.ui.notify(`Ticker settings were not saved: ${errorMessage}`, "error");
|
|
206
|
+
} finally {
|
|
207
|
+
busy = false;
|
|
208
|
+
if (!disposed && !options.signal.aborted && options.isCurrent()) rebuild("widget");
|
|
209
|
+
}
|
|
210
|
+
})();
|
|
211
|
+
};
|
|
212
|
+
const toggle = () => {
|
|
213
|
+
if (busy || disposed) return;
|
|
214
|
+
const row = selectedRow();
|
|
215
|
+
if (row?.kind !== "ticker") return;
|
|
216
|
+
const previous = [...symbols];
|
|
217
|
+
const next = symbols.filter((symbol) => symbol !== row.symbol);
|
|
218
|
+
persist(next, previous, undefined, selectedIndex);
|
|
219
|
+
};
|
|
220
|
+
const moveTicker = (direction: -1 | 1) => {
|
|
221
|
+
if (busy || disposed || rawSearchInput.getValue() || options.signal.aborted || !options.isCurrent()) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const row = selectedRow();
|
|
225
|
+
if (row?.kind !== "ticker") return;
|
|
226
|
+
const from = symbols.indexOf(row.symbol);
|
|
227
|
+
const to = from + direction;
|
|
228
|
+
if (from < 0 || to < 0 || to >= symbols.length) return;
|
|
229
|
+
const previous = [...symbols];
|
|
230
|
+
const next = [...symbols];
|
|
231
|
+
const [symbol] = next.splice(from, 1);
|
|
232
|
+
if (!symbol) return;
|
|
233
|
+
next.splice(to, 0, symbol);
|
|
234
|
+
persist(next, previous, symbol, selectedIndex);
|
|
235
|
+
};
|
|
236
|
+
const activate = () => {
|
|
237
|
+
if (busy || disposed) return;
|
|
238
|
+
const row = selectedRow();
|
|
239
|
+
if (!row) return;
|
|
240
|
+
if (row.kind === "ticker") {
|
|
241
|
+
toggle();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (row.id === "add") {
|
|
245
|
+
const query = rawSearchInput.getValue();
|
|
246
|
+
const hasTickerMatch = rows.some((candidate) => candidate.kind === "ticker");
|
|
247
|
+
if (query.trim() && !hasTickerMatch) {
|
|
248
|
+
let symbol: string;
|
|
249
|
+
try {
|
|
250
|
+
symbol = parseDirectSymbol(query);
|
|
251
|
+
} catch (error) {
|
|
252
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
253
|
+
errorMessage = sanitizeTerminalText(message);
|
|
254
|
+
ctx.ui.notify(errorMessage, "error");
|
|
255
|
+
tui.requestRender();
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (symbols.includes(symbol)) {
|
|
259
|
+
ctx.ui.notify(`${symbol} is already selected.`, "info");
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
persist([...symbols, symbol], [...symbols], symbol, selectedIndex, true);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
complete("add");
|
|
266
|
+
} else if (row.id === "widget") persistWidgetEnabled(!widgetEnabled);
|
|
267
|
+
else options.refresh();
|
|
268
|
+
};
|
|
269
|
+
const handleSearch = (data: string) => {
|
|
270
|
+
const previousId = selectedRow()?.id;
|
|
271
|
+
errorMessage = undefined;
|
|
272
|
+
rawSearchInput.handleInput(data);
|
|
273
|
+
searchInput.handleInput(data);
|
|
274
|
+
searchInput.setValue(sanitizeTerminalText(rawSearchInput.getValue()));
|
|
275
|
+
rebuild(previousId, 0);
|
|
276
|
+
};
|
|
297
277
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
disposed = true;
|
|
410
|
-
},
|
|
411
|
-
get focused() {
|
|
412
|
-
return searchInput.focused;
|
|
413
|
-
},
|
|
414
|
-
set focused(value: boolean) {
|
|
415
|
-
searchInput.focused = value;
|
|
416
|
-
},
|
|
417
|
-
};
|
|
418
|
-
},
|
|
419
|
-
});
|
|
278
|
+
return {
|
|
279
|
+
render(width: number) {
|
|
280
|
+
const safeWidth = Number.isFinite(width) ? Math.max(0, Math.floor(width)) : 0;
|
|
281
|
+
if (safeWidth === 0) return [""];
|
|
282
|
+
const viewportSize = availableRows(tui.terminal.rows, rows.length);
|
|
283
|
+
const viewportStart = Math.max(
|
|
284
|
+
0,
|
|
285
|
+
Math.min(selectedIndex - Math.floor(viewportSize / 2), rows.length - viewportSize),
|
|
286
|
+
);
|
|
287
|
+
const visibleRows = rows.slice(viewportStart, viewportStart + viewportSize);
|
|
288
|
+
const content = visibleRows.map((row, offset) => {
|
|
289
|
+
const index = viewportStart + offset;
|
|
290
|
+
const focused = index === selectedIndex;
|
|
291
|
+
const label =
|
|
292
|
+
row.kind === "ticker"
|
|
293
|
+
? `${focused ? "›" : " "} [x] ${sanitizeTerminalText(row.symbol)}`
|
|
294
|
+
: `${focused ? "›" : " "} ${row.label}`;
|
|
295
|
+
const bounded = truncateToWidth(label, safeWidth, safeWidth > 1 ? "…" : "");
|
|
296
|
+
return focused ? theme.fg("accent", bounded) : bounded;
|
|
297
|
+
});
|
|
298
|
+
if (viewportSize < rows.length) {
|
|
299
|
+
content.push(theme.fg("dim", truncateToWidth(` (${selectedIndex + 1}/${rows.length})`, safeWidth, "")));
|
|
300
|
+
}
|
|
301
|
+
const row = selectedRow();
|
|
302
|
+
const description =
|
|
303
|
+
row?.kind === "ticker"
|
|
304
|
+
? rawSearchInput.getValue()
|
|
305
|
+
? "Clear search before reordering."
|
|
306
|
+
: tickerDescription
|
|
307
|
+
: row?.description;
|
|
308
|
+
const status = errorMessage
|
|
309
|
+
? theme.fg("error", errorMessage)
|
|
310
|
+
: busy
|
|
311
|
+
? theme.fg("muted", "Saving changes…")
|
|
312
|
+
: undefined;
|
|
313
|
+
return [
|
|
314
|
+
...topRule.render(safeWidth),
|
|
315
|
+
theme.fg(
|
|
316
|
+
"muted",
|
|
317
|
+
truncateToWidth(`${symbols.length} selected · changes save immediately`, safeWidth, ""),
|
|
318
|
+
),
|
|
319
|
+
...searchInput.render(safeWidth).map((line) => truncateToWidth(line, safeWidth, "")),
|
|
320
|
+
...content,
|
|
321
|
+
...(description ? [theme.fg("dim", truncateToWidth(` ${description}`, safeWidth, ""))] : []),
|
|
322
|
+
...(status ? [truncateToWidth(status, safeWidth, "")] : []),
|
|
323
|
+
...bottomRule.render(safeWidth),
|
|
324
|
+
theme.fg("dim", truncateToWidth(hint, safeWidth, "")),
|
|
325
|
+
];
|
|
326
|
+
},
|
|
327
|
+
invalidate() {
|
|
328
|
+
searchInput.invalidate();
|
|
329
|
+
},
|
|
330
|
+
handleInput(data: string) {
|
|
331
|
+
if (pasteActive || data.includes(PASTE_START)) {
|
|
332
|
+
pasteActive = !data.includes(PASTE_END);
|
|
333
|
+
handleSearch(data);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (matchesKey(data, Key.ctrl("c"))) {
|
|
337
|
+
complete("close");
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (keybindings.matches(data, "tui.select.cancel")) {
|
|
341
|
+
complete("close");
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (keybindings.matches(data, "tui.select.up")) {
|
|
345
|
+
select(selectedIndex - 1, true);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (keybindings.matches(data, "tui.select.down")) {
|
|
349
|
+
select(selectedIndex + 1, true);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (keybindings.matches(data, "tui.select.pageUp")) {
|
|
353
|
+
select(selectedIndex - availableRows(tui.terminal.rows, rows.length), false);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
if (keybindings.matches(data, "tui.select.pageDown")) {
|
|
357
|
+
select(selectedIndex + availableRows(tui.terminal.rows, rows.length), false);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (keybindings.matches(data, "tui.select.confirm") || data === " ") {
|
|
361
|
+
activate();
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (shortcutUp && matchesKey(data, shortcutUp)) {
|
|
365
|
+
moveTicker(-1);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (shortcutDown && matchesKey(data, shortcutDown)) {
|
|
369
|
+
moveTicker(1);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
handleSearch(data);
|
|
373
|
+
},
|
|
374
|
+
async waitForPending() {
|
|
375
|
+
await pending;
|
|
376
|
+
},
|
|
377
|
+
dispose() {
|
|
378
|
+
disposed = true;
|
|
379
|
+
},
|
|
380
|
+
get focused() {
|
|
381
|
+
return searchInput.focused;
|
|
382
|
+
},
|
|
383
|
+
set focused(value: boolean) {
|
|
384
|
+
searchInput.focused = value;
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
},
|
|
388
|
+
});
|
|
420
389
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
}
|
|
446
|
-
}
|
|
390
|
+
if (result.kind !== "completed" || result.value === "close") return;
|
|
391
|
+
const entered = await ctx.ui.input("Add custom ticker", "Yahoo symbol, for example MSFT or SOL-USD", {
|
|
392
|
+
signal: options.signal,
|
|
393
|
+
});
|
|
394
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
395
|
+
if (!entered?.trim()) continue;
|
|
396
|
+
try {
|
|
397
|
+
const parsed = parseSymbols([entered]);
|
|
398
|
+
if (parsed.length !== 1) throw new Error("Enter exactly one Yahoo Finance symbol.");
|
|
399
|
+
const symbol = parsed[0];
|
|
400
|
+
if (!symbol) continue;
|
|
401
|
+
const current = options.getSymbols();
|
|
402
|
+
if (current.includes(symbol)) {
|
|
403
|
+
ctx.ui.notify(`${symbol} is already selected.`, "info");
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
await options.applySymbols([...current, symbol], options.signal);
|
|
407
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
408
|
+
} catch (error) {
|
|
409
|
+
if (options.signal.aborted || !options.isCurrent()) return;
|
|
410
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
411
|
+
ctx.ui.notify(sanitizeTerminalText(message), "error");
|
|
412
|
+
}
|
|
413
|
+
}
|
|
447
414
|
}
|
|
448
415
|
|
|
449
416
|
function parseDirectSymbol(value: string): string {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
417
|
+
const parsed = parseSymbols([value]);
|
|
418
|
+
if (parsed.length !== 1) throw new Error("Enter exactly one Yahoo Finance symbol.");
|
|
419
|
+
const symbol = parsed[0];
|
|
420
|
+
if (!symbol) throw new Error("Enter one Yahoo Finance symbol.");
|
|
421
|
+
return symbol;
|
|
455
422
|
}
|
|
456
423
|
|
|
457
424
|
function tryDirectSymbol(value: string): string | undefined {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
425
|
+
if (!value.trim()) return undefined;
|
|
426
|
+
try {
|
|
427
|
+
return parseDirectSymbol(value);
|
|
428
|
+
} catch {
|
|
429
|
+
return undefined;
|
|
430
|
+
}
|
|
464
431
|
}
|
|
465
432
|
|
|
466
433
|
function availableRows(terminalRows: number, rowCount: number): number {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
: 10;
|
|
470
|
-
return Math.max(1, Math.min(rowCount, boundedRows, 12));
|
|
434
|
+
const boundedRows = Number.isFinite(terminalRows) ? Math.max(1, Math.floor(terminalRows) - 9) : 10;
|
|
435
|
+
return Math.max(1, Math.min(rowCount, boundedRows, 12));
|
|
471
436
|
}
|
|
472
437
|
|
|
473
438
|
function shortcutAvailable(
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
439
|
+
keybindings: {
|
|
440
|
+
getKeys(binding: string): readonly string[];
|
|
441
|
+
},
|
|
442
|
+
shortcut: "shift+up" | "shift+down",
|
|
443
|
+
canonicalKey: (value: unknown) => string | undefined,
|
|
478
444
|
): typeof shortcut | undefined {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
function canonicalKey(value: string): string {
|
|
492
|
-
const parts = value.toLowerCase().split("+");
|
|
493
|
-
const base = parts.at(-1) ?? "";
|
|
494
|
-
const canonicalBase = base === "escape" ? "esc" : base === "return" ? "enter" : base;
|
|
495
|
-
const modifiers = ["ctrl", "shift", "alt", "super"].filter((modifier) =>
|
|
496
|
-
parts.includes(modifier),
|
|
497
|
-
);
|
|
498
|
-
return [...modifiers, canonicalBase].join("+");
|
|
445
|
+
const unavailable = new Set(
|
|
446
|
+
[
|
|
447
|
+
"ctrl+c",
|
|
448
|
+
"home",
|
|
449
|
+
"end",
|
|
450
|
+
"space",
|
|
451
|
+
...RESERVED_INPUT_BINDINGS.flatMap((binding) => keybindings.getKeys(binding)),
|
|
452
|
+
].map(canonicalKey),
|
|
453
|
+
);
|
|
454
|
+
return unavailable.has(canonicalKey(shortcut)) ? undefined : shortcut;
|
|
499
455
|
}
|