Skip to content

Commit 76175ae

Browse files
authored
Move Zoxide completer to cookbook (#1934)
1 parent bc293c2 commit 76175ae

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

.vuepress/configs/sidebar/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export const sidebarEn: SidebarConfig = {
154154
'/cookbook/parsing',
155155
'/cookbook/foreign_shell_scripts',
156156
'/cookbook/pattern_matching',
157+
'/cookbook/custom_completers',
157158
'/cookbook/external_completers',
158159
'/cookbook/modules',
159160
'/cookbook/files',

book/custom_completions.md

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,6 @@ cat rat bat
6969

7070
Because we made matching case-insensitive, 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).
7171

72-
## Another Practical Example - Zoxide Path Completions
73-
74-
[Zoxide](https://github.com/ajeetdsouza/zoxide) allows easily jumping between visited folders in the system. It's possible to autocomplete matching folders with this completer:
75-
76-
```nu
77-
def "nu-complete zoxide path" [context: string] {
78-
let parts = $context | split row " " | skip 1
79-
{
80-
options: {
81-
sort: false,
82-
completion_algorithm: prefix,
83-
positional: false,
84-
case_sensitive: false,
85-
},
86-
completions: (zoxide query --list --exclude $env.PWD -- ...$parts | lines),
87-
}
88-
}
89-
90-
def --env --wrapped z [...rest: string@"nu-complete zoxide path"] {
91-
__zoxide_z ...$rest
92-
}
93-
```
94-
9572
## Modules and Custom Completions
9673

9774
Since completion commands aren't meant to be called directly, it's common to define them in modules.

cookbook/custom_completers.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: Custom Completers
3+
---
4+
5+
# Custom Completers
6+
7+
## Zoxide Path Completions
8+
9+
[Zoxide](https://github.com/ajeetdsouza/zoxide) allows easily jumping between visited folders in the system. It's possible to autocomplete matching folders with this completer:
10+
11+
```nu
12+
def "nu-complete zoxide path" [context: string] {
13+
let parts = $context | split row " " | skip 1
14+
{
15+
options: {
16+
sort: false,
17+
completion_algorithm: substring,
18+
case_sensitive: false,
19+
},
20+
completions: (^zoxide query --list --exclude $env.PWD -- ...$parts | lines),
21+
}
22+
}
23+
24+
def --env --wrapped z [...rest: string@"nu-complete zoxide path"] {
25+
__zoxide_z ...$rest
26+
}
27+
```
28+
29+
Do note that the above completer probably won't work with multiple keywords because each completion suggestion is a full path. Something like `z nu <TAB>` might provide `/home/user/nushell` as a suggestion, and if you select this suggestion, your commandline will be replaced with `z nu /home/user/nushell` rather than `z /home/user/nushell`. Running `z nu /home/user/nushell` will now fail.
30+
31+
Below is a more convoluted completer that provides odd-looking suggestions but does work with multiple keywords.
32+
33+
```nu
34+
def "nu-complete zoxide path" [context: string] {
35+
let parts = $context | str trim --left | split row " " | skip 1 | each { str downcase }
36+
let completions = (
37+
^zoxide query --list --exclude $env.PWD -- ...$parts
38+
| lines
39+
| each { |dir|
40+
if ($parts | length) <= 1 {
41+
$dir
42+
} else {
43+
let dir_lower = $dir | str downcase
44+
let rem_start = $parts | drop 1 | reduce --fold 0 { |part, rem_start|
45+
($dir_lower | str index-of --range $rem_start.. $part) + ($part | str length)
46+
}
47+
{
48+
value: ($dir | str substring $rem_start..),
49+
description: $dir
50+
}
51+
}
52+
})
53+
{
54+
options: {
55+
sort: false,
56+
completion_algorithm: substring,
57+
case_sensitive: false,
58+
},
59+
completions: $completions,
60+
}
61+
}
62+
```

0 commit comments

Comments
 (0)