|
1 | 1 | import { Command } from "@cliffy/command"; |
2 | 2 | import { Input } from "@cliffy/prompt/input"; |
3 | 3 | import { colors } from "@cliffy/ansi/colors"; |
4 | | -import sdk, { getCurrentUser } from "~/sdk.ts"; |
| 4 | +import sdk, { getCurrentUser, typeaheadValNames, valNameToVal } from "~/sdk.ts"; |
5 | 5 | import VTClient from "~/vt/vt/VTClient.ts"; |
6 | 6 | import { relative } from "@std/path"; |
7 | 7 | import { doWithSpinner, getClonePath } from "~/cmd/utils.ts"; |
@@ -55,41 +55,27 @@ export const cloneCmd = new Command() |
55 | 55 |
|
56 | 56 | // If no Val URI is provided, show interactive Val selection |
57 | 57 | if (!valUri) { |
58 | | - const vals = await doWithSpinner( |
59 | | - "Loading vals...", |
60 | | - async (spinner) => { |
61 | | - const [allVals, _] = await arrayFromAsyncN( |
62 | | - sdk.me.vals.list({ limit: 100 }), |
63 | | - 400, |
64 | | - ); |
65 | | - spinner.stop(); |
66 | | - return allVals; |
67 | | - }, |
68 | | - ); |
69 | | - |
70 | | - if (vals.length === 0) { |
71 | | - console.log(colors.yellow("You don't have any Vals yet.")); |
72 | | - return; |
73 | | - } |
74 | | - |
75 | | - // Map vals to name format for selection |
76 | | - const valNames = vals |
77 | | - // Only show vals owned by the user (not orgs that the user is in) |
78 | | - .filter((p) => p.author.id === user.id) |
79 | | - .map((p) => p.name); |
80 | | - |
| 58 | + let suggestions = new Set(); |
81 | 59 | const selectedVal = await Input.prompt({ |
82 | 60 | message: "Choose a Val to clone", |
83 | 61 | list: true, |
84 | 62 | info: true, |
85 | | - suggestions: valNames, |
| 63 | + validate: (input) => { |
| 64 | + const split = input.split("/"); |
| 65 | + return suggestions.has(input) && (split.length === 2) && |
| 66 | + split[1].length !== 0; |
| 67 | + }, |
| 68 | + suggestions: async (prefix) => { |
| 69 | + suggestions = new Set( |
| 70 | + await typeaheadValNames(prefix || `${user.username}/`), |
| 71 | + ); |
| 72 | + return Array.from(suggestions) as (string | number)[]; |
| 73 | + }, |
86 | 74 | }); |
87 | 75 |
|
88 | | - const val = vals.find((p) => p.name === selectedVal); |
89 | | - if (!val) { |
90 | | - console.log(colors.red("Val not found")); |
91 | | - return; |
92 | | - } |
| 76 | + const parts = selectedVal.split("/"); |
| 77 | + let [handle, valName] = parts; |
| 78 | + const val = await valNameToVal(handle, valName); |
93 | 79 |
|
94 | 80 | ownerName = val.author.username || user.username!; |
95 | 81 | valName = val.name; |
|
0 commit comments