Skip to content

Commit 566910f

Browse files
committed
fix(console): align help suggestions with demo mode
1 parent fef4e07 commit 566910f

3 files changed

Lines changed: 105 additions & 39 deletions

File tree

runner/console/completer.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ func completeForMode(d prompt.Document, mode HelpMode) []prompt.Suggest {
5454
case HelpModeProvider:
5555
return providerSuggestions(ctx, args, word)
5656
case HelpModeShell:
57-
return shellSuggestions(args, word)
57+
return shellSuggestions(ctx, args, word)
5858
default:
59-
return rootSuggestions(args, word)
59+
return rootSuggestions(ctx, args, word)
6060
}
6161
}
6262

63-
func rootSuggestions(args []string, word string) []prompt.Suggest {
63+
func rootSuggestions(ctx CompletionContext, args []string, word string) []prompt.Suggest {
6464
if len(args) <= 1 {
65-
return prompt.FilterHasPrefix(rootCommandSuggestions, word, true)
65+
return prompt.FilterHasPrefix(commandSuggestionsForContext(ctx), word, true)
6666
}
6767
switch args[0] {
6868
case "help":
69-
return helpSuggestions(args, word)
69+
return helpSuggestions(ctx, args, word)
7070
case "use":
7171
if len(args) == 2 {
7272
return prompt.FilterContains(modules, word, true)
@@ -81,14 +81,11 @@ func rootSuggestions(args []string, word string) []prompt.Suggest {
8181

8282
func providerSuggestions(ctx CompletionContext, args []string, word string) []prompt.Suggest {
8383
if len(args) <= 1 {
84-
if ctx.DemoReplay {
85-
return prompt.FilterHasPrefix(mockCommandSuggestions(), word, true)
86-
}
87-
return prompt.FilterHasPrefix(providerCommandSuggestionsData, word, true)
84+
return prompt.FilterHasPrefix(commandSuggestionsForContext(ctx), word, true)
8885
}
8986
switch args[0] {
9087
case "help":
91-
return helpSuggestions(args, word)
88+
return helpSuggestions(ctx, args, word)
9289
case "use":
9390
if len(args) == 2 {
9491
return prompt.FilterContains(modules, word, true)
@@ -107,13 +104,13 @@ func providerSuggestions(ctx CompletionContext, args []string, word string) []pr
107104
return []prompt.Suggest{}
108105
}
109106

110-
func shellSuggestions(args []string, word string) []prompt.Suggest {
107+
func shellSuggestions(ctx CompletionContext, args []string, word string) []prompt.Suggest {
111108
if len(args) <= 1 {
112-
return prompt.FilterHasPrefix(shellCommandSuggestions, word, true)
109+
return prompt.FilterHasPrefix(commandSuggestionsForContext(ctx), word, true)
113110
}
114111
switch args[0] {
115112
case "help":
116-
return helpSuggestions(args, word)
113+
return helpSuggestions(ctx, args, word)
117114
}
118115
return []prompt.Suggest{}
119116
}
@@ -163,19 +160,20 @@ func completionArgs(d prompt.Document) []string {
163160
return args
164161
}
165162

166-
func helpSuggestions(args []string, word string) []prompt.Suggest {
163+
func helpSuggestions(ctx CompletionContext, args []string, word string) []prompt.Suggest {
167164
if len(args) == 2 {
168-
return prompt.FilterContains(helpTopicSuggestions(), word, true)
165+
return prompt.FilterContains(helpTopicSuggestions(ctx), word, true)
169166
}
170167
if len(args) == 3 && (args[1] == "payload" || args[1] == "metadata") {
171168
return prompt.FilterContains(payloadSuggestions(), word, true)
172169
}
173170
return []prompt.Suggest{}
174171
}
175172

176-
func helpTopicSuggestions() []prompt.Suggest {
177-
suggestions := make([]prompt.Suggest, 0, len(helpTopicOrder))
178-
for _, key := range helpTopicOrder {
173+
func helpTopicSuggestions(ctx CompletionContext) []prompt.Suggest {
174+
keys := helpTopicKeysForContext(ctx)
175+
suggestions := make([]prompt.Suggest, 0, len(keys))
176+
for _, key := range keys {
179177
if topic, ok := helpTopics[key]; ok {
180178
suggestions = append(suggestions, prompt.Suggest{
181179
Text: key,
@@ -186,6 +184,36 @@ func helpTopicSuggestions() []prompt.Suggest {
186184
return suggestions
187185
}
188186

187+
func helpTopicKeysForContext(ctx CompletionContext) []string {
188+
keys := make([]string, 0, len(helpTopicOrder))
189+
seen := make(map[string]struct{}, len(helpTopicOrder))
190+
191+
appendKey := func(key string) {
192+
if _, ok := helpTopics[key]; !ok {
193+
return
194+
}
195+
if _, ok := seen[key]; ok {
196+
return
197+
}
198+
seen[key] = struct{}{}
199+
keys = append(keys, key)
200+
}
201+
202+
for _, key := range commandNamesForContext(ctx.Mode, ctx.DemoReplay) {
203+
appendKey(key)
204+
}
205+
if ctx.Mode == HelpModeProvider && ctx.DemoReplay {
206+
appendKey("demo")
207+
}
208+
appendKey("payload")
209+
appendKey("metadata")
210+
211+
for _, key := range helpTopicOrder {
212+
appendKey(key)
213+
}
214+
return keys
215+
}
216+
189217
func optionSuggestions(ctx CompletionContext) []prompt.Suggest {
190218
keys := []string{utils.Payload, utils.Metadata}
191219
seen := map[string]struct{}{

runner/console/completion_data.go

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,28 @@ var commandSuggestionDescriptions = map[string]string{
4646
"shell": "open an authorized instance shell",
4747
}
4848

49-
var rootCommandSuggestions = buildCommandSuggestions([]string{
49+
var rootCommandNames = []string{
5050
"help",
5151
"use",
5252
"sessions",
5353
"note",
5454
"clear",
5555
"exit",
56-
})
56+
}
5757

58-
var shellCommandSuggestions = buildCommandSuggestions([]string{
58+
var shellCommandNames = []string{
5959
"help",
6060
"clear",
6161
"exit",
62-
})
62+
}
6363

6464
var sessionsCommandSuggestions = []prompt.Suggest{
6565
{Text: "-i", Description: "interact with a cached session by ID"},
6666
{Text: "-k", Description: "delete a cached session by ID"},
6767
{Text: "-c", Description: "check one cached session or all sessions"},
6868
}
6969

70-
var providerCommandSuggestionsData = buildCommandSuggestions([]string{
70+
var providerCommandNames = []string{
7171
"help",
7272
"show",
7373
"set",
@@ -79,7 +79,7 @@ var providerCommandSuggestionsData = buildCommandSuggestions([]string{
7979
"use",
8080
"clear",
8181
"exit",
82-
})
82+
}
8383

8484
var showTopicSuggestionsData = []prompt.Suggest{
8585
{Text: "options", Description: "display provider configuration"},
@@ -191,6 +191,7 @@ func completionContextForMode(mode HelpMode) CompletionContext {
191191
ctx.Provider = ""
192192
ctx.Payload = ""
193193
ctx.InstanceID = ""
194+
ctx.DemoReplay = false
194195
case HelpModeShell:
195196
if ctx.Payload == "" {
196197
ctx.Payload = "instance-cmd-check"
@@ -211,17 +212,44 @@ func buildCommandSuggestions(commands []string) []prompt.Suggest {
211212
return suggestions
212213
}
213214

214-
func mockCommandSuggestions() []prompt.Suggest {
215-
return buildCommandSuggestions([]string{
216-
"help",
217-
"show",
218-
"set",
219-
"run",
220-
"shell",
221-
"note",
222-
"clear",
223-
"exit",
224-
})
215+
func commandNamesForContext(mode HelpMode, demoReplay bool) []string {
216+
switch mode {
217+
case HelpModeProvider:
218+
return providerCommandNamesForState(demoReplay)
219+
case HelpModeShell:
220+
return append([]string(nil), shellCommandNames...)
221+
default:
222+
return append([]string(nil), rootCommandNames...)
223+
}
224+
}
225+
226+
func providerCommandNamesForState(demoReplay bool) []string {
227+
if !demoReplay {
228+
return append([]string(nil), providerCommandNames...)
229+
}
230+
231+
names := make([]string, 0, len(providerCommandNames))
232+
for _, name := range providerCommandNames {
233+
switch name {
234+
case "demo", "note", "use":
235+
continue
236+
}
237+
names = append(names, name)
238+
}
239+
return names
240+
}
241+
242+
func commandSuggestionsForContext(ctx CompletionContext) []prompt.Suggest {
243+
return buildCommandSuggestions(commandNamesForContext(ctx.Mode, ctx.DemoReplay))
244+
}
245+
246+
func commandAvailable(mode HelpMode, demoReplay bool, command string) bool {
247+
for _, name := range commandNamesForContext(mode, demoReplay) {
248+
if name == command {
249+
return true
250+
}
251+
}
252+
return false
225253
}
226254

227255
func noteSuggestions(args []string, word string) []prompt.Suggest {

runner/console/help_topics.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ func renderProviderHelp(ctx HelpContext) {
445445
required = []string{"No provider is currently selected."}
446446
}
447447
writeLines(&b, "Required options:", required)
448+
writeLines(&b, "Available commands:", []string{
449+
commandListSummary(HelpModeProvider, ctx.DemoReplay),
450+
})
448451
writeLines(&b, "Next recommended commands:", providerRecommendedCommands(ctx))
449452
writeLines(&b, "Responsible use:", []string{
450453
"Validate only in owned, lab, or explicitly authorized environments.",
@@ -614,6 +617,9 @@ func providerRequiredOptionLines() []string {
614617
}
615618

616619
func providerRecommendedCommands(ctx HelpContext) []string {
620+
canExit := commandAvailable(HelpModeProvider, ctx.DemoReplay, "exit")
621+
canDemo := commandAvailable(HelpModeProvider, ctx.DemoReplay, "demo")
622+
617623
if config == nil {
618624
return []string{
619625
"use <provider>",
@@ -632,13 +638,13 @@ func providerRecommendedCommands(ctx HelpContext) []string {
632638
}
633639
if len(missing) > 0 {
634640
commands := []string{"show options"}
635-
if ctx.DemoReplay {
641+
if canExit {
636642
commands = append(commands, "exit")
637643
}
638644
for _, key := range missing {
639645
commands = append(commands, fmt.Sprintf("set %s <value>", key))
640646
}
641-
if !ctx.DemoReplay {
647+
if canDemo {
642648
commands = append(commands, "demo")
643649
}
644650
commands = append(commands,
@@ -653,10 +659,10 @@ func providerRecommendedCommands(ctx HelpContext) []string {
653659
"set payload <payload-name>",
654660
"help payload <payload-name>",
655661
}
656-
if ctx.DemoReplay {
662+
if canExit {
657663
commands = append([]string{"exit"}, commands...)
658664
}
659-
if !ctx.DemoReplay {
665+
if canDemo {
660666
commands = append(commands, "demo")
661667
}
662668
if ctx.Payload == "instance-cmd-check" {
@@ -731,6 +737,10 @@ func demoReplayStatus(enabled bool) string {
731737
return "disabled"
732738
}
733739

740+
func commandListSummary(mode HelpMode, demoReplay bool) string {
741+
return strings.Join(commandNamesForContext(mode, demoReplay), ", ")
742+
}
743+
734744
func providerHelpModeLabel(ctx HelpContext) string {
735745
if ctx.DemoReplay {
736746
return "provider[mock]"

0 commit comments

Comments
 (0)