Skip to content

Commit 755d7a8

Browse files
authored
Document custom completion options and styling (#1726)
Document the fact that custom completers can return a record with an options field, as well as the fact that each individual suggestion can include a style, for changing the color of that suggestion.
1 parent aaaf6fe commit 755d7a8

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

book/custom_completions.md

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,52 @@ my-command
2121
The first line defines a custom command which returns a list of three different animals. These are the possible values for the completion.
2222

2323
::: tip
24-
The completion command must return a Nushell `list` of the possible completion values. If a completer does not return a valid list, the default completer will be used. The default completer returns a list of files and subdirectories of the current directory.
25-
26-
To suppress completions for an argument (for example, an `int` that can accept any integer), define a completer that returns an empty list (`[ ]`).
24+
To suppress completions for an argument (for example, an `int` that can accept any integer), define a completer that returns an empty list (`[ ]`). At the moment, invalid values such as `null` will also suppress completions, but this may change in the future.
2725
:::
2826

29-
In the second line of the example above, `string@animals` tells Nushell two things—the shape of the argument for type-checking and the completer which will suggest possible values for the argument.
27+
In the second line, `string@animals` tells Nushell two things—the shape of the argument for type-checking and the completer which will suggest possible values for the argument.
3028

3129
The third line is demonstration of the completion. Type the name of the custom command `my-command`, followed by a space, and then the <kbd>Tab</kbd> key. This displays a menu with the possible completions. Custom completions work the same as other completions in the system, allowing you to type `e` followed by the <kbd>Tab</kbd> key to complete "eel" automatically.
3230

3331
::: tip
3432
When the completion menu is displayed, the prompt changes to include the `|` character by default. This can be changed using `$env.config.menus.marker`.
3533
:::
3634

35+
## Options for Custom Completions
36+
37+
If you want to choose how your completions are filtered and sorted, you can also return a record rather than a list. The list of completion suggestions should be under the `completions` key of this record. Optionally, it can also have, under the `options` key, a record containing the following optional settings:
38+
39+
- `sort` - Set this to `false` to stop Nushell from sorting your completions. By default, this is `true`, and completions are sorted according to `$env.config.completions.sort`.
40+
- `case_sensitive` - Set to `true` for the custom completions to be matched case sensitively, `false` otherwise. Used for overriding `$env.config.completions.case_sensitive`.
41+
- `completion_algorithm` - Set this to either `prefix` or `fuzzy` to choose how your completions are matched against the typed text. Used for overriding `$env.config.completions.algorithm`.
42+
- `positional` - When prefix matching is used, setting this to `false` will use substring matching instead. `true` by default.
43+
44+
Here's an example demonstrating how to set these options:
45+
46+
```nu
47+
def animals [] {
48+
{
49+
options: {
50+
case_sensitive: false,
51+
completion_algorithm: prefix,
52+
positional: false,
53+
sort: false,
54+
},
55+
completions: [cat, rat, bat]
56+
}
57+
}
58+
def my-command [animal: string@animals] { print $animal }
59+
```
60+
61+
Now, if you try to complete `A`, you get the following completions:
62+
63+
```nu
64+
>| my-command A
65+
cat rat bat
66+
```
67+
68+
Because we made matching case-insensitive and used `positional: false`, Nushell will find the substring "a" in all of the completion suggestions. Additionally, because we set `sort: false`, the completions will be left in their original order. This is useful if your completions are already sorted in a particular order unrelated to their text (e.g. by date).
69+
3770
## Modules and Custom Completions
3871

3972
Since completion commands aren't meant to be called directly, it's common to define them in modules.
@@ -125,15 +158,20 @@ export extern "git push" [
125158

126159
Custom completions will serve the same role in this example as in the previous examples. The examples above call into two different custom completions, based on the position the user is currently in.
127160

128-
## Custom Descriptions
161+
## Custom Descriptions and Styles
129162

130-
As an alternative to returning a list of strings, a completion function can also return a list of records with a `value` and `description` field.
163+
As an alternative to returning a list of strings, a completion function can also return a list of records with a `value` field as well as optional `description` and `style` fields. The style can be one of the following:
164+
165+
- A string with the foreground color, either a hex code or a color name such as `yellow`. For a list of valid color names, see `ansi --list`.
166+
- A record with the fields `fg` (foreground color), `bg` (background color), and `attr` (attributes such as underline and bold). This record is in the same format that `ansi --escape` accepts. See the [`ansi`](/commands/docs/ansi) command reference for a list of possible values for the `attr` field.
167+
- The same record, but converted to a JSON string.
131168

132169
```nu
133170
def my_commits [] {
134171
[
135-
{ value: "5c2464", description: "Add .gitignore" },
136-
{ value: "f3a377", description: "Initial commit" }
172+
{ value: "5c2464", description: "Add .gitignore", style: red },
173+
# "attr: ub" => underlined and bolded
174+
{ value: "f3a377", description: "Initial commit", style: { fg: green, bg: "#66078c", attr: ub } }
137175
]
138176
}
139177
```
@@ -149,10 +187,10 @@ def my-command [commit: string@my_commits] {
149187

150188
... be aware that, even though the completion menu will show you something like
151189

152-
```nu
153-
>_ my-command <TAB>
154-
5c2464 Add .gitignore
155-
f3a377 Initial commit
190+
```ansi
191+
>_ [36mmy-command[0m <TAB>
192+
[1;31m5c2464[0m [33mAdd .gitignore[0m
193+
[1;4;48;2;102;7;140;32mf3a377 [0m[33mInitial commit[0m
156194
```
157195

158196
... only the value (i.e., "5c2464" or "f3a377") will be used in the command arguments!

0 commit comments

Comments
 (0)