diff --git a/book/hooks.md b/book/hooks.md index 72c44aab908..dcfa949d8ff 100644 --- a/book/hooks.md +++ b/book/hooks.md @@ -28,15 +28,11 @@ The steps to evaluate one line in the REPL mode are as follows: To enable hooks, define them in your [config](configuration.md): ```nu -$env.config = { - # ...other config... - - hooks: { - pre_prompt: { print "pre prompt hook" } - pre_execution: { print "pre exec hook" } - env_change: { - PWD: {|before, after| print $"changing directory from ($before) to ($after)" } - } +$env.config.hooks = { + pre_prompt: [{ print "pre prompt hook" }] + pre_execution: [{ print "pre exec hook" }] + env_change: { + PWD: [{|before, after| print $"changing directory from ($before) to ($after)" }] } } ``` @@ -47,38 +43,28 @@ When you change a directory, the `PWD` environment variable changes and the chan Instead of defining just a single hook per trigger, it is possible to define a **list of hooks** which will run in sequence: ```nu -$env.config = { - ...other config... - - hooks: { - pre_prompt: [ - { print "pre prompt hook" } - { print "pre prompt hook2" } - ] - pre_execution: [ - { print "pre exec hook" } - { print "pre exec hook2" } +$env.config.hooks = { + pre_prompt: [ + { print "pre prompt hook" } + { print "pre prompt hook2" } + ] + pre_execution: [ + { print "pre exec hook" } + { print "pre exec hook2" } + ] + env_change: { + PWD: [ + {|before, after| print $"changing directory from ($before) to ($after)" } + {|before, after| print $"changing directory from ($before) to ($after) 2" } ] - env_change: { - PWD: [ - {|before, after| print $"changing directory from ($before) to ($after)" } - {|before, after| print $"changing directory from ($before) to ($after) 2" } - ] - } } } ``` -Also, it might be more practical to update the existing config with new hooks, instead of defining the whole config from scratch: +Instead of replacing all hooks, you can append a new hook to existing configuration: ```nu -$env.config = ($env.config | upsert hooks { - pre_prompt: ... - pre_execution: ... - env_change: { - PWD: ... - } -}) +$env.config.hooks.pre_execution = $env.config.hooks.pre_execution | append { print "pre exec hook3" } ``` ## Changing Environment diff --git a/de/book/hooks.md b/de/book/hooks.md index 215ff4c6152..8d62ae8fd97 100644 --- a/de/book/hooks.md +++ b/de/book/hooks.md @@ -28,15 +28,11 @@ Die Schritte zur Auswertung einer Zeile im REPL-Modus sind wie folgt: Um Hooks zu aktivieren, werden sie in der [config](configuration.md) definiert: ```nu -$env.config = { - # ...other config... - - hooks: { - pre_prompt: { print "pre prompt hook" } - pre_execution: { print "pre exec hook" } - env_change: { - PWD: {|before, after| print $"changing directory from ($before) to ($after)" } - } +$env.config.hooks = { + pre_prompt: [{ print "pre prompt hook" }] + pre_execution: [{ print "pre exec hook" }] + env_change: { + PWD: [{|before, after| print $"changing directory from ($before) to ($after)" }] } } ``` @@ -47,39 +43,28 @@ Die Änderung löst den Hook aus und tauscht die entsprechenden Werte in `before Anstatt nur einen einzigen Hook pro Trigger zu definieren, ist es möglich, eine *Liste von Hooks* zu definieren, die nacheinander durchlaufen werden: ```nu -$env.config = { - ...other config... - - hooks: { - pre_prompt: [ - { print "pre prompt hook" } - { print "pre prompt hook2" } - ] - pre_execution: [ - { print "pre exec hook" } - { print "pre exec hook2" } +$env.config.hooks = { + pre_prompt: [ + { print "pre prompt hook" } + { print "pre prompt hook2" } + ] + pre_execution: [ + { print "pre exec hook" } + { print "pre exec hook2" } + ] + env_change: { + PWD: [ + {|before, after| print $"changing directory from ($before) to ($after)" } + {|before, after| print $"changing directory from ($before) to ($after) 2" } ] - env_change: { - PWD: [ - {|before, after| print $"changing directory from ($before) to ($after)" } - {|before, after| print $"changing directory from ($before) to ($after) 2" } - ] - } } } ``` -Auch könnte es praktischer sein, die bestehende Konfiguration mit neuen Hooks zu aktualisieren, -anstatt die gesamte Konfiguration von Grund auf neu zu definieren: +Anstatt alle hooks zu ersetzen können neue Hooks der Liste vorhandener Hooks angehängt werden: ```nu -$env.config = ($env.config | upsert hooks { - pre_prompt: ... - pre_execution: ... - env_change: { - PWD: ... - } -}) +$env.config.hooks.pre_execution = $env.config.hooks.pre_execution | append { print "pre exec hook3" } ``` ## Changing Environment diff --git a/zh-CN/book/hooks.md b/zh-CN/book/hooks.md index c8fe38c2a4d..10cd48387d4 100644 --- a/zh-CN/book/hooks.md +++ b/zh-CN/book/hooks.md @@ -8,6 +8,8 @@ - `pre_prompt` : 在命令提示显示之前被触发; - `pre_execution` : 在行输入开始执行前被触发; - `env_change` : 当环境变量发生变化时被触发; +- `display_output` : A block that the output is passed to +- `command_not_found` : Triggered when a command is not found 为了更清晰地阐述,我们可以将 Nushell 的执行周期进行分解。 在 REPL 模式下,评估一行(代码)的步骤如下: @@ -24,15 +26,11 @@ 要想使用钩子需要先在 [配置](configuration.md) 中定义它们: ```nu -$env.config = { - # ...other config... - - hooks: { - pre_prompt: { print "pre prompt hook" } - pre_execution: { print "pre exec hook" } - env_change: { - PWD: {|before, after| print $"changing directory from ($before) to ($after)" } - } +$env.config.hooks = { + pre_prompt: [{ print "pre prompt hook" }] + pre_execution: [{ print "pre exec hook" }] + env_change: { + PWD: [{|before, after| print $"changing directory from ($before) to ($after)" }] } } ``` @@ -43,38 +41,28 @@ $env.config = { 可以为每个触发器只定义一个钩子,也可以定义一个**钩子列表**,让其依次运行: ```nu -$env.config = { - ...other config... - - hooks: { - pre_prompt: [ - { print "pre prompt hook" } - { print "pre prompt hook2" } - ] - pre_execution: [ - { print "pre exec hook" } - { print "pre exec hook2" } +$env.config.hooks = { + pre_prompt: [ + { print "pre prompt hook" } + { print "pre prompt hook2" } + ] + pre_execution: [ + { print "pre exec hook" } + { print "pre exec hook2" } + ] + env_change: { + PWD: [ + {|before, after| print $"changing directory from ($before) to ($after)" } + {|before, after| print $"changing directory from ($before) to ($after) 2" } ] - env_change: { - PWD: [ - {|before, after| print $"changing directory from ($before) to ($after)" } - {|before, after| print $"changing directory from ($before) to ($after) 2" } - ] - } } } ``` -另外,用新的钩子更新现有的配置,而不是从头开始定义整个配置可能更实用: +Instead of replacing all hooks, you can append a new hook to existing configuration: ```nu -$env.config = ($env.config | upsert hooks { - pre_prompt: ... - pre_execution: ... - env_change: { - PWD: ... - } -}) +$env.config.hooks.pre_execution = $env.config.hooks.pre_execution | append { print "pre exec hook3" } ``` ## 修改环境变量