mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-02 15:17:15 +00:00
CLI: switch command selector to interactive autocomplete
This commit is contained in:
@@ -1,16 +1,14 @@
|
|||||||
import type { Command } from "commander";
|
import type { Command } from "commander";
|
||||||
import { isCancel, select as clackSelect, text as clackText } from "@clack/prompts";
|
import { autocomplete as clackAutocomplete, isCancel } from "@clack/prompts";
|
||||||
import { stylePromptHint, stylePromptMessage } from "../../terminal/prompt-style.js";
|
import { stylePromptHint, stylePromptMessage } from "../../terminal/prompt-style.js";
|
||||||
import { theme } from "../../terminal/theme.js";
|
|
||||||
import { fuzzyFilterLower, prepareSearchItems } from "../../tui/components/fuzzy-filter.js";
|
import { fuzzyFilterLower, prepareSearchItems } from "../../tui/components/fuzzy-filter.js";
|
||||||
import { getCoreCliCommandNames, registerCoreCliByName } from "./command-registry.js";
|
import { getCoreCliCommandNames, registerCoreCliByName } from "./command-registry.js";
|
||||||
import { getProgramContext } from "./program-context.js";
|
import { getProgramContext } from "./program-context.js";
|
||||||
import { getSubCliEntries, registerSubCliByName } from "./register.subclis.js";
|
import { getSubCliEntries, registerSubCliByName } from "./register.subclis.js";
|
||||||
|
|
||||||
const SEARCH_AGAIN_VALUE = "__search_again__";
|
|
||||||
const SHOW_HELP_VALUE = "__show_help__";
|
const SHOW_HELP_VALUE = "__show_help__";
|
||||||
const PATH_SEPARATOR = "\u0000";
|
const PATH_SEPARATOR = "\u0000";
|
||||||
const MAX_MATCHES = 24;
|
const MAX_RESULTS = 200;
|
||||||
|
|
||||||
type CommandSelectorCandidate = {
|
type CommandSelectorCandidate = {
|
||||||
path: string[];
|
path: string[];
|
||||||
@@ -122,64 +120,37 @@ export async function runInteractiveCommandSelector(program: Command): Promise<s
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let lastQuery = "";
|
const selection = await clackAutocomplete<string>({
|
||||||
|
message: stylePromptMessage("Find and run a command") ?? "Find and run a command",
|
||||||
while (true) {
|
placeholder: "Type to fuzzy-search (e.g. msg snd)",
|
||||||
const queryResult = await clackText({
|
maxItems: 10,
|
||||||
message: stylePromptMessage("Find a command (fuzzy)") ?? "Find a command (fuzzy)",
|
// We pre-rank the list with our fuzzy scorer, then opt out of clack's own
|
||||||
placeholder: "message send",
|
// filter so item order stays stable and score-based.
|
||||||
defaultValue: lastQuery,
|
filter: () => true,
|
||||||
});
|
options() {
|
||||||
if (isCancel(queryResult)) {
|
const query = this.userInput.trim();
|
||||||
return null;
|
const ranked = rankCommandSelectorCandidates(candidates, query).slice(0, MAX_RESULTS);
|
||||||
}
|
return [
|
||||||
|
...ranked.map((candidate) => ({
|
||||||
const query = String(queryResult ?? "").trim();
|
|
||||||
lastQuery = query;
|
|
||||||
|
|
||||||
const matches = rankCommandSelectorCandidates(candidates, query);
|
|
||||||
if (matches.length === 0) {
|
|
||||||
console.error(theme.warn("No matching commands. Try a different search."));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const shown = matches.slice(0, MAX_MATCHES);
|
|
||||||
const selection = await clackSelect<string>({
|
|
||||||
message:
|
|
||||||
stylePromptMessage(
|
|
||||||
shown.length === matches.length
|
|
||||||
? `Select a command (${matches.length} matches)`
|
|
||||||
: `Select a command (showing ${shown.length} of ${matches.length} matches)`,
|
|
||||||
) ?? "Select a command",
|
|
||||||
options: [
|
|
||||||
...shown.map((candidate) => ({
|
|
||||||
value: candidate.path.join(PATH_SEPARATOR),
|
value: candidate.path.join(PATH_SEPARATOR),
|
||||||
label: candidate.label,
|
label: candidate.label,
|
||||||
hint: stylePromptHint(candidate.description),
|
hint: stylePromptHint(candidate.description),
|
||||||
})),
|
})),
|
||||||
{
|
|
||||||
value: SEARCH_AGAIN_VALUE,
|
|
||||||
label: "Search again",
|
|
||||||
hint: stylePromptHint("Change your fuzzy query"),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
value: SHOW_HELP_VALUE,
|
value: SHOW_HELP_VALUE,
|
||||||
label: "Show help",
|
label: "Show help",
|
||||||
hint: stylePromptHint("Skip selector and print CLI help"),
|
hint: stylePromptHint("Skip selector and print CLI help"),
|
||||||
},
|
},
|
||||||
],
|
];
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (isCancel(selection) || selection === SHOW_HELP_VALUE) {
|
if (isCancel(selection) || selection === SHOW_HELP_VALUE) {
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
if (selection === SEARCH_AGAIN_VALUE) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return selection
|
|
||||||
.split(PATH_SEPARATOR)
|
|
||||||
.map((segment) => segment.trim())
|
|
||||||
.filter(Boolean);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return selection
|
||||||
|
.split(PATH_SEPARATOR)
|
||||||
|
.map((segment) => segment.trim())
|
||||||
|
.filter(Boolean);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user