Line data Source code
1 : import type { DashQLShellCommand } from './api.js';
2 :
3 : export interface ExamplesCommandDependencies {
4 : /// Browse the snippets, resolves with the text of the used snippet or null if none was used
5 : requestSnippet(signal?: AbortSignal): Promise<string | null>;
6 : /// Split the text of a snippet into the prompts of its statements
7 : splitPrompts(text: string): string[];
8 : /// Offer the prompts one after another, each submit fills in the next one
9 : queuePrompts(prompts: readonly string[]): void;
10 : }
11 :
12 1 : export function createExamplesCommand(dependencies: ExamplesCommandDependencies): DashQLShellCommand {
13 1 : return [
14 1 : 'examples',
15 1 : 'Browse example SQL snippets',
16 1 : async (args, context) => {
17 1 : if (args.length !== 0) throw new Error('usage: .examples');
18 1 : if (context.signal?.aborted) return;
19 1 : const text = await dependencies.requestSnippet(context.signal);
20 1 : if (text == null || context.signal?.aborted) return;
21 1 : const prompts = dependencies.splitPrompts(text);
22 1 : if (prompts.length === 0) return;
23 1 : dependencies.queuePrompts(prompts);
24 1 : if (prompts.length > 1) {
25 1 : return `Inserted ${prompts.length} statements. Press Enter to run each, Ctrl+C to skip the rest.`;
26 : }
27 1 : },
28 1 : ];
29 1 : }
|