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
54 changes: 20 additions & 34 deletions book/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)" }]
}
}
```
Expand All @@ -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
Expand Down
55 changes: 20 additions & 35 deletions de/book/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)" }]
}
}
```
Expand All @@ -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
Expand Down
56 changes: 22 additions & 34 deletions zh-CN/book/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 模式下,评估一行(代码)的步骤如下:
Expand All @@ -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)" }]
}
}
```
Expand All @@ -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" }
```

## 修改环境变量
Expand Down