mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-06-20 01:19:23 +02:00
Add condition field to custom command prompts
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.
The template expression is a string pre- and suffixed with double curly
braces - {{}}.
Form keys can be reused, a guard ensures that skipped prompts do not
reset already set form keys with an empty string. This allows the
conditional flow to remind a user to set a key that was left empty
because additional conditions want that key to be set. This removes the
need to have additional if checks in the command that uses the form
keys.
This commit is contained in:
@@ -102,6 +102,7 @@ These fields are applicable to all prompts.
|
||||
| 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 |
|
||||
|
||||
### Input
|
||||
|
||||
@@ -319,6 +320,41 @@ Here's an example using a command but not specifying anything else: so each line
|
||||
command: 'ls'
|
||||
```
|
||||
|
||||
### Conditional prompts
|
||||
|
||||
Here's an example of a conditional prompt:
|
||||
|
||||
```yml
|
||||
customCommands:
|
||||
- key: 'a'
|
||||
context: 'localBranches'
|
||||
prompts:
|
||||
- type: 'menu'
|
||||
title: 'How do you want to create the branch?'
|
||||
key: 'Method'
|
||||
options:
|
||||
- value: 'simple'
|
||||
name: 'Simple'
|
||||
description: 'just a branch name'
|
||||
- value: 'prefix'
|
||||
name: 'With prefix'
|
||||
description: 'with a category prefix'
|
||||
- type: 'menu'
|
||||
title: 'Branch prefix'
|
||||
key: 'Prefix'
|
||||
condition: '{{ eq .Form.Method "prefix" }}'
|
||||
options:
|
||||
- value: 'feature/'
|
||||
- value: 'hotfix/'
|
||||
- value: 'release/'
|
||||
- type: 'input'
|
||||
title: 'Branch name'
|
||||
key: 'Name'
|
||||
command: "git checkout -b '{{.Form.Prefix}}{{.Form.Name}}'"
|
||||
```
|
||||
|
||||
In this example the 'Branch prefix' menu only appears if the user chose 'With prefix'. Otherwise it is skipped and `.Form.Prefix` defaults to empty string.
|
||||
|
||||
## Placeholder values
|
||||
|
||||
Your commands can contain placeholder strings using Go's [template syntax](https://jan.newmarch.name/golang/template/chapter-template.html). The template syntax is pretty powerful, letting you do things like conditionals if you want, but for the most part you'll simply want to be accessing the fields on the following objects:
|
||||
|
||||
Reference in New Issue
Block a user