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