Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions copier/_user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ class Question:
If it is a boolean, it is used directly. If it is a str, it is
converted to boolean using a parser similar to YAML, but only for
boolean values.

use_shortcuts:
Condition that, if `True`, will use `use_shortcuts` in `select` question,
allowing for selection via automatically numbered shortcut. Will be
deactivated if `use_search_filter` is `True`.
Comment on lines +208 to +210
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer describing only the behavior of the enabled feature and omitting a reference to the underlying implementation. Also, I'd prefer documenting mutual exclusiveness with multiselect (raising a validation error when combined) rather than silent deactivation.

Suggested change
Condition that, if `True`, will use `use_shortcuts` in `select` question,
allowing for selection via automatically numbered shortcut. Will be
deactivated if `use_search_filter` is `True`.
Condition that, if `True`, allows selecting choice question items via
number shortcuts. Mutually exclusive with `multiselect`.


use_search_filter:
Condition that, if `True`, uses `use_search_filter` in `checkbox`/`select`
question while deactivating `use_jk_keys`, allowing for selection via
filtering.
Comment on lines +213 to +215
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer describing only the behavior of the enabled feature and omitting a reference to the underlying implementation.

Suggested change
Condition that, if `True`, uses `use_search_filter` in `checkbox`/`select`
question while deactivating `use_jk_keys`, allowing for selection via
filtering.
Condition that, if `True`, enables filtering choice question items by
typing a search string. Disables j/k navigation, as "j" and "k" can be part
of a prefix and therefore cannot be used for navigation.

"""

var_name: str
Expand All @@ -221,6 +231,8 @@ class Question:
type: str = Field(default="", validate_default=True)
validator: str = ""
when: str | bool = True
use_shortcuts: bool = False
use_search_filter: bool = False

@field_validator("var_name")
@classmethod
Expand Down Expand Up @@ -419,6 +431,13 @@ def _validate(answer: str) -> str | Literal[True]:
result["default"] = False
if self.choices:
questionary_type = "checkbox" if self.multiselect else "select"

if self.use_search_filter:
result["use_search_filter"] = True
result["use_jk_keys"] = False
elif self.use_shortcuts and questionary_type == "select":
result["use_shortcuts"] = True

choices = self._formatted_choices
# Select default choices for a multiselect question.
if self.multiselect and isinstance(
Expand Down
155 changes: 155 additions & 0 deletions docs/configuring.md
Copy link
Member

Choose a reason for hiding this comment

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

Kudos for the beautiful prompt visualization using HTML in Markdown including the Unicode icon for the question type! 🏆 This looks great! 🤩

Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,163 @@ Supported keys:
Some array: "[str, keeps, this, as, a, str]"
```

- **use_shortcuts**: When set to `true`, numbers the choices while allowing for cursor
selection by pressing the corresponding number. Only works when `multiselect` is
`false`.
Comment on lines +189 to +190
Copy link
Member

Choose a reason for hiding this comment

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

I'd mention also the mutual exclusiveness with multiselect.

Suggested change
selection by pressing the corresponding number. Only works when `multiselect` is
`false`.
selection by pressing the corresponding number. Mutually exclusive with
`multiselect`.


!!! example

```yaml title="copier.yml"
language:
type: str
help: Which programming language do you use?
use_shortcuts: true
choices:
- python
- node
- c
- c++
- rust
- zig
- asm
```

Will result in:

<pre>
<span style="font-weight:bold">🎤 Which programming language do you use?</span>
(Use shortcuts or arrow keys)
» 1) python
2) node
3) c
4) c++
5) rust
6) zig
7) asm
</pre>

Pressing `5` gives:

<pre>
<span style="font-weight:bold">🎤 Which programming language do you use?</span>
(Use shortcuts or arrow keys)
1) python
2) node
3) c
4) c++
» 5) rust
6) zig
7) asm
</pre>

- **multiselect**: When set to `true`, allows multiple choices. The answer will be a
`list[T]` instead of a `T` where `T` is of type `type`.

- **use_search_filter**: When set to `true`, . Also deactivates the use of `j`/`k`
Copy link
Member

Choose a reason for hiding this comment

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

The description of the effect of the feature is missing after the comma.

keys for navigation, as these are captured as prompts for the search filter.

!!! note

If `multiselect` is `true`, you cannot use `Space` in the search as this would actually just still select the option. If it is `false`, the `Space` character can be used in the search filter.
Copy link
Member

Choose a reason for hiding this comment

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

  • If we enable the pymdownx.keys extension, we'll be able to use pretty keyboard keys rendering in the documentation. WDYT?
  • Adding a comma before the "as" because it's used for a causal conjunction. 🤓
  • Rephrasing "[...] actually just still [...]" a little. 🤓
  • And just a bit more simplification. 🤓
Suggested change
If `multiselect` is `true`, you cannot use `Space` in the search as this would actually just still select the option. If it is `false`, the `Space` character can be used in the search filter.
If `multiselect` is `true`, you cannot use ++space++ in the search, as this
would also select the choice item. If it is `false`, ++space++ can be used.


!!! note

If `use_shortcuts` & `use_search_filter` are both `true`, then only `use_search_filter` is activated.
Comment on lines +246 to +249
Copy link
Member

Choose a reason for hiding this comment

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

I'd remove this note and mention the mutual exclusiveness above.

Suggested change
!!! note
If `use_shortcuts` & `use_search_filter` are both `true`, then only `use_search_filter` is activated.


!!! example

```yaml title="copier.yml"
language:
type: str
help: Which programming language do you use?
use_search_filter: true
choices:
- python
- node
- c
- c++
- rust
- zig
- asm
- a new language
- a good one
- an average one
- a not so good one
Comment on lines +259 to +269
Copy link
Member

Choose a reason for hiding this comment

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

Can we stick with the same choices as in the example above? I think the list is already long enough with those choices, and typing o will filter python and node to show two remaining choices. I think the example showing the search filters an and ago isn't necessary – people will get the idea – while making the documentation quite lengthy.

```

<pre>
<span style="font-weight:bold">🎤 Which programming language do you use?</span>
(Use arrow keys, type to filter)
» python
node
c
c++
rust
zig
asm
a new language
a good one
an average one
a not so good one

</pre>

---


Comment on lines +290 to +291
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

Typing `c`:

<pre>
<span style="font-weight:bold">🎤 Which programming language do you use?</span>
(Use arrow keys, type to filter)
» c
c++


/ <span style="color:green;font-weight:bold">c</span>...
</pre>

---

Typing `an`:

<pre>
<span style="font-weight:bold">🎤 Which programming language do you use?</span>
(Use arrow keys, type to filter)
» a new language
an average one

/ <span style="color:green;font-weight:bold">an</span>...
</pre>

---

Typing `ago`

<pre>
<span style="font-weight:bold">🎤 Which programming language do you use?</span>
(Use arrow keys, type to filter)
» python
node
c
c++
rust
zig
asm
a new language
a good one
an average one
a not so good one

/ <span style="color:red;font-weight:bold">ago</span>...
</pre>

When the filter fails, all options are displayed.

---

You can use `Backspace` to modify the search filter.
Copy link
Member

Choose a reason for hiding this comment

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

If we enable the pymdownx.keys extension, we'll be able to use pretty keyboard keys rendering in the documentation. WDYT?

Suggested change
You can use `Backspace` to modify the search filter.
You can use ++backspace++ to modify the search filter.


- **default**: Leave empty to force the user to answer. Provide a default to save them
from typing it if it's quite common. When using `choices`, the default must be the
choice _value_, not its _key_, and it must match its _type_. If values are quite
Expand Down
Loading
Loading