From f9f3bdd44ef61862a7f819d5dd19f9ac14a9eff4 Mon Sep 17 00:00:00 2001 From: Chapman Pendery Date: Fri, 6 Oct 2023 21:32:23 -0700 Subject: [PATCH] fix: support isCommand Signed-off-by: Chapman Pendery --- src/runtime/runtime.ts | 10 +++--- src/runtime/suggestion.ts | 1 - .../__snapshots__/runtime.test.ts.snap | 31 +++++++++++++++++++ src/tests/runtime/runtime.test.ts | 2 +- src/ui/suggestions.tsx | 25 +++++++-------- 5 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/runtime/runtime.ts b/src/runtime/runtime.ts index b8beec7..188397a 100644 --- a/src/runtime/runtime.ts +++ b/src/runtime/runtime.ts @@ -155,12 +155,14 @@ const runArg = async ( if (activeArg.isVariadic) { return runArg(tokens.slice(1), args, subcommand, persistentOptions, acceptedTokens.concat(activeToken), fromOption, true); } else if (activeArg.isCommand) { - if (tokens.length <= 1) { + if (tokens.length <= 0) { return; } - const activeCmd = tokens[1]; - // TODO: handle subcommands that isCommands - return; + const spec = await loadSpec(tokens); + if (spec == null) return; + const subcommand = getSubcommand(spec); + if (subcommand == null) return; + return runSubcommand(tokens.slice(1), subcommand); } return runArg(tokens.slice(1), args.slice(1), subcommand, persistentOptions, acceptedTokens.concat(activeToken), fromOption, false); }; diff --git a/src/runtime/suggestion.ts b/src/runtime/suggestion.ts index 48f3aa4..7d1aa54 100644 --- a/src/runtime/suggestion.ts +++ b/src/runtime/suggestion.ts @@ -3,7 +3,6 @@ import { runGenerator } from "./generator.js"; import { runTemplates } from "./template.js"; import { Suggestion, SuggestionBlob } from "./model.js"; -// TODO: support other suggestion attributes enum SuggestionIcons { File = "📄", Folder = "📁", diff --git a/src/tests/runtime/__snapshots__/runtime.test.ts.snap b/src/tests/runtime/__snapshots__/runtime.test.ts.snap index 355667c..33bc41a 100644 --- a/src/tests/runtime/__snapshots__/runtime.test.ts.snap +++ b/src/tests/runtime/__snapshots__/runtime.test.ts.snap @@ -17,6 +17,37 @@ exports[`parseCommand alreadyUsedOption 1`] = ` exports[`parseCommand alreadyUsedSuggestion 1`] = `undefined`; +exports[`parseCommand command 1`] = ` +{ + "suggestions": [ + { + "allNames": [ + "stage", + ], + "description": "Add file contents to the staging area", + "icon": "📦", + "name": "stage", + }, + { + "allNames": [ + "status", + ], + "description": "Show the working tree status", + "icon": "📦", + "name": "status", + }, + { + "allNames": [ + "stash", + ], + "description": "Temporarily stores all the modified tracked files", + "icon": "📦", + "name": "stash", + }, + ], +} +`; + exports[`parseCommand completePrefixFilter 1`] = ` { "suggestions": [ diff --git a/src/tests/runtime/runtime.test.ts b/src/tests/runtime/runtime.test.ts index f3c3348..8357523 100644 --- a/src/tests/runtime/runtime.test.ts +++ b/src/tests/runtime/runtime.test.ts @@ -12,7 +12,7 @@ const testData = [ { name: "noOptionsSuggestedAfterVariadicArg", command: "ls item -" }, { name: "providedArgDescription", command: "act completion bash -a " }, { name: "completedOptionWithArg", command: "act completion bash -a 'actor' " }, - { name: "command", command: "sudo git sta", skip: true }, // TODO: fix skipped test + { name: "command", command: "sudo git sta" }, { name: "nestedNonCommands", command: "az az ", skip: true }, // TODO: fix skipped test ]; diff --git a/src/ui/suggestions.tsx b/src/ui/suggestions.tsx index ba55ec3..8b1e586 100644 --- a/src/ui/suggestions.tsx +++ b/src/ui/suggestions.tsx @@ -24,20 +24,19 @@ function SuggestionList({ suggestions, activeSuggestionIdx }: { suggestions: Sug return ( - {suggestions.map((suggestion, idx) => { - const bgColor = idx === activeSuggestionIdx ? ActiveSuggestionBackgroundColor : undefined; + {suggestions + .map((suggestion, idx) => { + const bgColor = idx === activeSuggestionIdx ? ActiveSuggestionBackgroundColor : undefined; - const name = suggestion.name; - if (name.length === 0) return <>; - - return ( - - - {name.padEnd(SuggestionWidth - BorderWidth, " ")} - - - ); - })} + return ( + + + {suggestion.name.padEnd(SuggestionWidth - BorderWidth, " ")} + + + ); + }) + .filter((node) => node !== undefined)} );