Skip to content

Add condition field to custom command prompts#5364

Open
mrt181 wants to merge 1 commit intojesseduffield:masterfrom
mrt181:add-conditional-prompts
Open

Add condition field to custom command prompts#5364
mrt181 wants to merge 1 commit intojesseduffield:masterfrom
mrt181:add-conditional-prompts

Conversation

@mrt181
Copy link
Copy Markdown

@mrt181 mrt181 commented Mar 12, 2026

When building multi-step custom command forms, some prompts are only relevant depending on earlier answers. Without conditional logic, users must dismiss irrelevant prompts manually.

Prompts now accept a condition field with a template expression evaluated against prior form values. Skipped prompts default to an empty string.

PR Description

Please check if the PR fulfills these requirements

  • Cheatsheets are up-to-date (run go generate ./...)
  • Code has been formatted (see here)
  • Tests have been added/updated (see here for the integration test guide)
  • Text is internationalised (see here)
  • If a new UserConfig entry was added, make sure it can be hot-reloaded (see here)
  • Docs have been updated if necessary
  • You've read through your own file changes for silly mistakes etc

@mrt181 mrt181 force-pushed the add-conditional-prompts branch from cfd9c66 to e934c7c Compare March 12, 2026 14:37
@mrt181 mrt181 force-pushed the add-conditional-prompts branch from e934c7c to 4040756 Compare March 16, 2026 14:31
@mrt181 mrt181 force-pushed the add-conditional-prompts branch 2 times, most recently from 78c0266 to a51be39 Compare March 25, 2026 23:53
When building multi-step custom command forms, some prompts are only
relevant depending on earlier answers. Without conditional logic,
users must dismiss irrelevant prompts manually.

Prompts now accept a `condition` field with a template expression
evaluated against prior form values. Skipped prompts default to
an empty string.
@mrt181 mrt181 force-pushed the add-conditional-prompts branch from a51be39 to b117f12 Compare March 27, 2026 20:37
Copy link
Copy Markdown
Collaborator

@stefanhaller stefanhaller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks reasonable overall. A few things below.

if resolved {
return showPrompt()
}
if _, exists := form[prompt.Key]; !exists {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the !exists condition here? How can form[prompt.Key] be set when we get here, and why would we want to keep its value in that case?

expr = strings.TrimPrefix(expr, "{{")
expr = strings.TrimSuffix(expr, "}}")
}
ifTemplate := fmt.Sprintf("{{ if %s }}1{{ end }}", expr)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not an expert at go templates, but this looks really hacky to me. Isn't there a cleaner way to evaluate a boolean template expression?

| type | One of 'input', 'confirm', 'menu', 'menuFromCommand' | yes |
| title | The title to display in the popup panel | no |
| key | Used to reference the entered value from within the custom command. E.g. a prompt with `key: 'Branch'` can be referred to as `{{.Form.Branch}}` in the command | yes |
| condition | A Go template expression; if it resolves to empty string or `false`, the prompt is skipped. See [Conditional prompts](#conditional-prompts) | no |
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All changes in this file need to be moved to docs-master.

Comment on lines +238 to +244
},
"condition": {
"type": "string",
"description": "A Go template expression evaluated against the current form state. If it resolves to empty string or 'false', the prompt is skipped.",
"examples": [
"{{ eq .Form.Choice \"yes\" }}"
]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be here, we only want to touch schema-master/config.json. Also, we never manually edit it; if you want examples to appear in the schema, you need to add jsonschema:"example=..." to the entry in user_config.go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants