nixos/home/modules/shell/nushell/config/hooks/did_you_mean.nu
2025-11-16 01:03:19 +01:00

48 lines
1 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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")"
}