Skip to content
Merged
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
2 changes: 2 additions & 0 deletions hooks/command_not_found/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
some `config.hooks.command_not_found` hooks:
- `did_you_mean.nu`: gives a list of 3 external commands that are close to the "not found" one
48 changes: 48 additions & 0 deletions hooks/command_not_found/did_you_mean.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# example:
# ```nu
# > got
# Error: nu::shell::external_command
#
# × External command failed
# ╭─[entry #1:1:1]
# 1 │ got
# · ─┬─
# · ╰── did you mean 'get'?
# ╰────
# help: No such file or directory (os error 2)
#
# did you mean?
# dot
# git
# go
# ```
{|cmd|
let commands_in_path = (
$env.PATH | each {|directory|
if ($directory | path exists) {
ls $directory | get name | path parse | update parent "" | path join
}
}
| flatten
| wrap cmd
)

let closest_commands = (
$commands_in_path
| insert distance {|it|
$it.cmd | str distance $cmd
}
| uniq
| sort-by distance
| get cmd
| first 3
)

let pretty_commands = (
$closest_commands | each {|cmd|
$" (ansi {fg: "default" attr: "di"})($cmd)(ansi reset)"
}
)

$"\ndid you mean?\n($pretty_commands | str join "\n")"
}