You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: book/custom_completions.md
+50-12Lines changed: 50 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,19 +21,52 @@ my-command
21
21
The first line defines a custom command which returns a list of three different animals. These are the possible values for the completion.
22
22
23
23
::: 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.
27
25
:::
28
26
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.
30
28
31
29
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.
32
30
33
31
::: tip
34
32
When the completion menu is displayed, the prompt changes to include the `|` character by default. This can be changed using `$env.config.menus.marker`.
35
33
:::
36
34
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:
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
+
37
70
## Modules and Custom Completions
38
71
39
72
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" [
125
158
126
159
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.
127
160
128
-
## Custom Descriptions
161
+
## Custom Descriptions and Styles
129
162
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.
0 commit comments