Home Explore Blog CI



nushell

4th chunk of `book/hooks.md`
22e7bdcb823a263a5699afba46c2f2cca598eb6d727a0a9b0000000100000dee
                    and '/path/to/target/dir' in ($before | default "")
                    and 'test-env' in (overlay list))
            }
            code: "overlay hide test-env --keep-env [ PWD ]"
        }
    ]
})
```

### Filtering or Diverting Command Output

You can use the `display_output` hook to redirect the output of commands.
You should define a block that works on all value types.
The output of external commands is not filtered through `display_output`.

This hook can display the output in a separate window,
perhaps as rich HTML text. Here is the basic idea of how to do that:

```nu
$env.config = ($env.config | upsert hooks {
    display_output: { to html --partial --no-color | save --raw /tmp/nu-output.html }
})
```

You can view the result by opening `file:///tmp/nu-output.html` in
a web browser.
Of course this isn't very convenient unless you use
a browser that automatically reloads when the file changes.
Instead of the [`save`](/commands/docs/save.md) command, you would normally customize this
to send the HTML output to a desired window.

### Changing how Output is Displayed

You can change to default behavior of how output is displayed by using the `display_output` hook.
Here is an example that changes the default display behavior to show a table 1 layer deep if the terminal is wide enough, or collapse otherwise:

```nu
$env.config = ($env.config | upsert hooks {
    display_output: {if (term size).columns >= 100 { table -ed 1 } else { table }}
})
```

### `command_not_found` Hook in _Arch Linux_

The following hook uses the `pkgfile` command, to find which packages commands belong to in _Arch Linux_.

```nu
$env.config = {
    ...other config...

    hooks: {
        ...other hooks...

        command_not_found: {
            |cmd_name| (
                try {
                    let pkgs = (pkgfile --binaries --verbose $cmd_name)
                    if ($pkgs | is-empty) {
                        return null
                    }
                    (
                        $"(ansi $env.config.color_config.shape_external)($cmd_name)(ansi reset) " +
                        $"may be found in the following packages:\n($pkgs)"
                    )
                }
            )
        }
    }
}
```

### `command_not_found` Hook in _NixOS_

NixOS comes with the command `command-not-found`. We only need to plug it in the nushell hook:

```nu
$env.config.hooks.command_not_found = {
  |command_name|
  print (command-not-found $command_name | str trim)
}
```


### `command_not_found` Hook in _Windows_

The following hook uses the `ftype` command, to find program paths in _Windows_ that might be relevant to the user for `alias`-ing.

```nu
$env.config = {
    ...other config...

    hooks: {
        ...other hooks...

        command_not_found: {
            |cmd_name| (
                try {
                    let attrs = (
                        ftype | find $cmd_name | to text | lines | reduce -f [] { |line, acc|
                            $line | parse "{type}={path}" | append $acc
                        } | group-by path | transpose key value | each { |row|
                            { path: $row.key, types: ($row.value | get type | str join ", ") }
                        }
                    )
                    let len = ($attrs | length)

                    if $len == 0 {
                        return null
                    } else {
                        return ($attrs | table --collapse)
                    }
                }
            )
        }
    }
}
```

Title: Nushell Hook Examples: Output Display, Command Not Found Handling
Summary
This section provides advanced examples of using Nushell hooks, including customizing output display with the `display_output` hook to show tables based on terminal width, and implementing `command_not_found` hooks for Arch Linux (using `pkgfile`), NixOS (using `command-not-found`), and Windows (using `ftype`) to provide users with suggestions for missing commands.