nushell: Add command_not_found hook

This commit is contained in:
Erwin Boskma 2023-04-19 10:58:03 +02:00
parent 982bcaccd6
commit 470831a100
Signed by: erwin
SSH key fingerprint: SHA256:CyeNoWXd3kjX2Nwu6pDxxdS7OqmPVOy0NavA/KU/ntU
2 changed files with 43 additions and 4 deletions

View file

@ -86,9 +86,9 @@ let dark_theme = {
}
# External completer example
let carapace_completer = {|spans|
carapace $spans.0 nushell $spans | from json
}
# let carapace_completer = {|spans|
# carapace $spans.0 nushell $spans | from json
# }
# The default config record. This is where much of your global configuration is setup.
@ -183,7 +183,7 @@ let-env config = {
external: {
enable: true # set to false to prevent nushell looking into $env.PATH to find more suggestions, `false` recommended for WSL users as this look up my be very slow
max_results: 100 # setting it lower can improve completion performance at the cost of omitting some options
completer: $carapace_completer # check 'carapace_completer' above as an example
completer: null # $carapace_completer # check 'carapace_completer' above as an example
}
}
filesize: {
@ -222,6 +222,21 @@ let-env config = {
display_output: {||
if (term size).columns >= 100 { table -e } else { table }
}
command_not_found: { |cmdname| (
try {
let candidates = (open $env.command_not_found_db | query db $"select package from Programs where system = '($env.NIX_SYSTEM)' and name = '($cmdname)'")
if ($candidates | is-empty) {
return null
}
let packages = ($candidates | each {|pkg| $"\tnix-shell -p ($pkg.package)" } | str join "\n")
let multiple = if ($candidates | length) > 1 { " one of the following" } else { "" }
(
$"The program (ansi $env.config.color_config.shape_external)($cmdname)(ansi reset) " +
$"is not in your PATH. You can make it available in an ephemeral shell by typing($multiple):\n" +
$"($packages)"
)
}
)}
}
menus: [ # Configuration for default nushell menus
# Note the lack of source parameter

View file

@ -18,6 +18,30 @@ in
package = pkgs.nushell.override { additionalFeatures = p: p ++ [ "dataframe" ]; };
configFile.source = ./config.nu;
envFile.source = ./env.nu;
extraEnv = ''
let-env command_not_found_db = ${config.programs.command-not-found.dbPath}
let-env NIX_SYSTEM = "${pkgs.system}"
'';
# extraConfig = ''
# let-env config = ($env.config | update hooks ($env.config.hooks | default [] command_not_found))
# let-env config = ($env.config | update hooks.command_not_found ($env.config.hooks.command_not_found | append { |cmdname| (
# let candidates = (open ${config.programs.command-not-found.dbPath} | query db $"select package from Programs where system = '${pkgs.system}' and name = '($cmdname)'")
# if ($candidates | is-empty) {
# return null
# }
# let packages = ($candidates | each {|pkg| $"\tnix-shell -p ($pkg.package)" } | str join "\n")
# let multiple = if ($candidates | length) > 1 { " one of the following" } else { "" }
# (
# $"The program (ansi $env.config.color_config.shape_external)($cmdname)(ansi reset) " +
# $"is not in your PATH. You can make it available in an ephemeral shell by typing($multiple):\n" +
# $"($packages)"
# )
# )
# }
# )
# )
# '';
};
home.packages = with pkgs; [